Loop through Excel Named Range with VBA

Ben Jansen

New Member
Joined
Jun 12, 2020
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi All,
getting into the VBA programming and already managed to make my life a lot easier with some simple coding. This thing is cool to learn!
Now I am trying to level up and make my programming a bit more clean and professional by using NAMED RANGES.

My intention :
in my template I will have a named range with multiple rows and columns.
along the way I might add rows, so the amount of rows is not necessarily static.

I want VBA to loop through a certain Named Range and take action when a certain cell is or isn't filled out
(more specific, if it is not filled out, it should hide the row... Kind of a "clean-up my form code")

So in my head it is like :
'go to first line of named range
'check if column O and P is filled out
' - if it is filled out , go to the next range line
' - if it is not filled out, hide that row and go to the next line
'do this for the whole range

I am working with something similar to below now (VBA beginner code), but as soon as I insert some rows my code is screwed.
(And i have many many ranges programmed)

Any ideas would be appreciated !

VBA Code:
Sub Button3_Click()

For a = 70 To 75
'Hide all Cells where nothing is filled out in column O and P
'under section Mech Pos Indication

    If Worksheets("Valve REPORT").Cells(a, 15).Value = "" And Worksheets("Valve REPORT").Cells(a, 16).Value = "" Then
    
    Worksheets("Valve REPORT").Rows(a).Hidden = True
    
    
    End If
End sub
    
    
Next
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Welcome to the Board!

Have a look at this tutorial here, which shows you how to loop through a named range:

Quite frankly, I don't really like working with dynamic named ranges, if I don't have to. They are susceptible to issues like:
1. If you add data below the last row in your data range, it will miss it (unless you have included some extra blank rows at the bottom of the named range.
2. They can get corrupted, especially if you are deleting rows/columns (I have seen a number of #REF errors in named ranges!)

I prefer to work with ranges dynamically, i.e. having Excel dynamically figure out exactly which ranges to include. For example, if I know that my data starts on row 2, and column A has an entry for every data row I want to include, I will find the last row in column with data, like this:
VBA Code:
Dim lastRow as Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
and then loop through all my rows like this:
VBA Code:
Dim r as Long
For r = 2 to lastRow
    ....
Next r
You can check specific columns within your loop like this (checking column E in this example):
VBA Code:
If Cells(r, "E") = ...

Lastly, depending on what you are doing, sometimes you can avoid loops altogether. The general rule of thumb is to only use loops when absolutely necessary, as loop tend to be slow and are resource hogs. So if there are other ways to do something without using a loop, that is usually preferred.

Hope that helps!
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top