Using "With" Statement with Range Variable

techfanatic

New Member
Joined
Jun 30, 2008
Messages
21
Hi Guys,

I'm complete stuck with this. My With statement wouldn't accept my range variable. It keeps on throwing Error 1004. Any help is be appreciated.

If I replace
Code:
With Range(findBlankSupRange)

with the code below, it works
Code:
With wsData.Range("B2:B" & lastAgentRow)

A portion of the code
Code:
    Dim wb As Workbook
    Dim wsData As Worksheet
    Dim findBlankSupRange As Range
    Dim lastAgentRow as Integer

    Set wb = ThisWorkbook
    Set wsData = wb.Worksheets("Data")
    lastAgentRow = wsData.Range("C65536").End(xlUp).Row
    Set findBlankSupRange = wsData.Range("B2:B" & lastAgentRow)
        
    With Range(findBlankSupRange) 'I'm getting error 1004 on this line (Excel 2010)
        Set lastCell = .Cells(.Cells.Count)
    End With

Thank you!
 

Some videos you may like

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,414
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hi Guys,

I'm complete stuck with this. My With statement wouldn't accept my range variable. It keeps on throwing Error 1004. Any help is be appreciated.

If I replace
Code:
With Range(findBlankSupRange)

with the code below, it works
Code:
With wsData.Range("B2:B" & lastAgentRow)

A portion of the code
Code:
    Dim wb As Workbook
    Dim wsData As Worksheet
    Dim findBlankSupRange As Range
    Dim lastAgentRow as Integer

    Set wb = ThisWorkbook
    Set wsData = wb.Worksheets("Data")
    lastAgentRow = wsData.Range("C65536").End(xlUp).Row
    Set findBlankSupRange = wsData.Range("B2:B" & lastAgentRow)
        
    With Range(findBlankSupRange) 'I'm getting error 1004 on this line (Excel 2010)
        Set lastCell = .Cells(.Cells.Count)
    End With

Thank you!
You have set the range so don't use Range(findBlankSupRange). Instead use:
Code:
With findBlankSupRange
'rest of code
 

Watch MrExcel Video

Forum statistics

Threads
1,128,072
Messages
5,628,476
Members
416,320
Latest member
Insightful111

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
Top