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!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
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
 
Upvote 0

Forum statistics

Threads
1,215,220
Messages
6,123,694
Members
449,117
Latest member
Aaagu

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