VBA Loop Through Named Ranges

DixiePiper

New Member
Joined
Oct 19, 2015
Messages
41
Office Version
  1. 365
Platform
  1. Windows
I have an inputs worksheet with certain required fields. My goal was to name these cells and have Excel loop through them to identify any blanks before allowing the user to continue. I found a snippet of code that will list the named ranges on the worksheet but I cannot figure out how to modify it to check if they are blank.

Here's how I'd like it to work:
1. The workbook opens to the Project Data tab that contains 50+ fields. Of those 50, let's say 25 are mandatory and those cells are named (e.g,. "Project.ID", "Project.Title", "Author", etc.).
2. Once the author has filed out the data, they click a button to continue to the next step.
3. This is where I need to look at the named ranges on the Project Data tab and verify that the 25 required fields have data. If it finds an empty range, a message box pops up saying, "Please complete required fields in order to continue." Bonus points if it can activate the blank cell to make it easier for the user to complete.
4. If all the named ranges contain data, the user is allowed to continue to the next step.

For reference, I'm using Excel 365 on Windows 10.

Thank you in advance for any help.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Will this work? I created a list of named ranges on a sheet called setup and named that range NRList

VBA Code:
Sub CheckForBlanks()
  Dim Cel As Range
  Dim NRList As Range
  Dim PDSht As Worksheet
  Dim NR As Range
  
  Set NRList = Sheets("Setup").Range("NRList")
  Set PDSht = Sheets("Project Data")
  
  
  For Each Cel In NRList
    On Error Resume Next
    Set NR = PDSht.Range(Cel.Value)
    If Not NR Is Nothing Then
      If NR.Value = "" Then
        PDSht.Activate
        NR.Select
        MsgBox "This is a required field: " & Cel.Value
        Exit Sub
      End If
    End If
  Next Cel
  
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,206
Members
452,618
Latest member
Tam84

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