Setting multiple cell reference

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
My workbook as 2 worksheets, codenames are Data and Input.

In Input, I have some cell references, such as:

Code:
A1
A2
A10

The code below looks at the cells in the worksheet Data.

Code:
    Dim CheckRng As Range
    
    With Input
    
        Set CheckRng = .Range(.Cells(1, 6), .Cells(10, 6))
    
    End With
    
    Dim CheckRngElement As Range
    
    For Each CheckRngElement In CheckRng
        
        If Data.Range(CheckRngElement).Value = vbNullString Then

            ' do something

        End If

    Next

This code works.

However, if one of the cell reference is multiple, it fails.

Code:
A1:A10
A11
B20:B30,B40:B50



How can I adapt my code so that even if the user enters a multiple cell reference, such as A1:A10, it would work?


Thanks
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
You need to do another loop eg:

Code:
Set sh = Sheets("Sheet1")
Set Data = Sheets("Sheet2")

Set CheckRng = sh.Range(sh.Cells(1, 6), sh.Cells(10, 6))

For Each CheckRngElement In CheckRng
    If Len(CheckRngElement.Value) > 0 Then
        Set rng = Data.Range(CheckRngElement.Value)
        For Each c In rng
            If Len(c) > 0 Then
                MsgBox c.Value
            End If
        Next
    End If
Next
 
Upvote 0
You need to do another loop eg:

Code:
Set sh = Sheets("Sheet1")
Set Data = Sheets("Sheet2")

Set CheckRng = sh.Range(sh.Cells(1, 6), sh.Cells(10, 6))

For Each CheckRngElement In CheckRng
    If Len(CheckRngElement.Value) > 0 Then
        Set rng = Data.Range(CheckRngElement.Value)
        For Each c In rng
            If Len(c) > 0 Then
                MsgBox c.Value
            End If
        Next
    End If
Next

Thanks, that's exactly what I needed.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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