[VBA] Thisworkbook.sheets Problem

Safety

New Member
Joined
Feb 2, 2011
Messages
17
Hey everyone,

I'm trying to define two ranges composed of non-contiguous columns located on 2 separate sheets (Sheet1 and Sheet2). The script does work with one sheet but it ignores the ThisWorkbook.Worksheets() located in the second With statement and simply selects the range on the same Sheet defined in the first With statement.

I used ".Select" to get a quick visual confirmation of what the script was doing. They will be removed in the final version.

Any advice would be greatly appreciated!

Thanks!
Code:
Sub SelectionTest()
    Dim SelectOne As Range, SelectTwo As Range
        
    With ThisWorkbook.Worksheets("Sheet1")
        StartRange = "A2"
        MidRange = "C2"
            
            Set a = Range(StartRange, Range(StartRange).End(xlDown))
            Set b = Range(MidRange, Range(MidRange).End(xlDown))
        
        Set SelectOne = Union(a, b)
        SelectOne.Select
    End With
        
    With ThisWorkbook.Worksheets("Sheet2")
        TratsRange = "A2"
        DimRange = "C2"
        DneRange = "E2"
            
            Set d = Range(TratsRange, Range(TratsRange).End(xlDown))
            Set e = Range(DimRange, Range(DimRange).End(xlDown))
            Set f = Range(DneRange, Range(DneRange).End(xlDown))
        
        Set SelectTwo = Union(d, e, f)
        SelectTwo.Select
    End With
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hello. I think that you need before "With ThisWorkbook.Worksheets("Sheet2")" select Worksheets("Sheet2")" like this: "Worksheets("Sheet2").select"
 
Upvote 0
When using a With structure, you have to qualilfy the sheet that was designated in the with..
Notice the Red periods below..

Rich (BB code):
    With ThisWorkbook.Worksheets("Sheet1")
        StartRange = "A2"
        MidRange = "C2"
 
            Set a = .Range(StartRange, .Range(StartRange).End(xlDown))
            Set b = .Range(MidRange, .Range(MidRange).End(xlDown))
 
        Set SelectOne = Union(a, b)
        SelectOne.Select
    End With

Otherwise, VBA defaults to use whatever sheet happens to be active at the time.
 
Upvote 0
Thanks to both of you for the help!

@Jon: The edit you suggested did solve the problem but it also introduced a Run-Time error '1004' in the second to last line "ListTwo.Select". Would you happen to know why this is happening? Thanks again!

Code:
Sub SelectionTest()
    Dim ListOne As Range, ListTwo As Range
    
    With ThisWorkbook.Worksheets("Sheet1")
        StartRange = "A2"
        MidRange = "C2"
            
            Set a = .Range(StartRange, .Range(StartRange).End(xlDown))
            Set b = .Range(MidRange, .Range(MidRange).End(xlDown))
        
        Set ListOne = Union(a, b)
        ListOne.Select
    End With
        
    With ThisWorkbook.Worksheets("Sheet2")
        TratsRange = "A2"
        DimRange = "C2"
        DneRange = "E2"
            
            Set d = .Range(TratsRange, .Range(TratsRange).End(xlDown))
            Set e = .Range(DimRange, .Range(DimRange).End(xlDown))
            Set f = .Range(DneRange, .Range(DneRange).End(xlDown))
        
        Set ListTwo = Union(d, e, f)
        ListTwo.Select
    End With
End Sub
 
Upvote 0
You can't select a range on a sheet, if that sheet is not currently Active.

You said you had select in there just for testing purposes, you intended to remove it later anyway, so I wouldn't worry about it.

If the first section set ListOne correctly, then the 2nd one will Set ListTwo correctly as well..
 
Upvote 0

Forum statistics

Threads
1,216,807
Messages
6,132,819
Members
449,760
Latest member
letonuslepus

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