how make code dynamic

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » How to make a code dynamic

How to make a code dynamic

resolvedResolved · Medium Priority · Version 365

Sanjhali has attended:
Excel VBA Introduction course

How to make a code dynamic

Hi Guys. i have a code which is not dynamic. it selects column D (column D header is "Weight" )and perform the function. I wanted to know how can i change my code to dynamic i.e select the column with header "Weight" instead of just selection column D as per the below code


Sub deleteblankandzerorow()
Range("D:D").Select
For Each cell In Range("D1:D100")
If ActiveCell = "0" Then
ActiveCell.EntireRow.Delete
ElseIf ActiveCell = "" Then
ActiveCell.EntireRow.Delete
Else
ActiveCell.Offset(1, 0).Select
End If
Next cell
End Sub

RE: How to make a code dynamic

Hi Sanjhali,

Thank you for the forum question.

The first option below if you do not know which row and the second option if you know which row, you have the header Weight.



cells.Find("Weight").EntireColumn.Select

Rows(1).Find("weight").EntireColumn.Select

Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: How to make a code dynamic

Hi Jens. thank you for your prompt reply.

If i know the row then i will use this code: cells.Find("Weight").EntireColumn.Select

However how should i use the for look then which is the 2nd line in my code i.e For Each cell In Range("D1:D100")

RE: How to make a code dynamic

Hi Sanjhali,

For Each cell in selection

Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

Edited on Fri 25 Mar 2022, 13:34

RE: How to make a code dynamic

Hi Jens. thank you. this was super helpful and it worked.

Just one more question related to this please.

If i use for each cell in selection - this is selecting the entire column and the code is running on the entire column which is never ending loop.

How can i just make it run till the last cell which has value in that particular column ("Weight").

RE: How to make a code dynamic

Hi Sanjhali,

I will need to understand something about your data set.

If there is a blank cell in the column where the header is Weight, does it mean that the whole row is blank or only the cell?



Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Removing the Ribbon from view in Excel 2010

At times when you want to view the whole spreadsheet, try double clicking on the ''Home'' tab on the ribbon which will hide the ribbon from view.

View all Excel hints and tips


Server loaded in 0.07 secs.