.select

vbaNewby

Board Regular
Joined
Jan 26, 2011
Messages
138
Hello guru's,

I have the following:
Code:
Set Sh = ThisWorkbook.Sheets("Sheet10")
    With Sh
        Set rng = .Cells(1, .Columns.count).End(xlToLeft)
        Set rng = Range(rng, rng.End(xlToLeft))
    End With
 rng.Copy
 '

 'Sheets("Sheet2").Select
 ThisWorkbook.Sheets("Sheet2").Select

It is failing with out of range error and "select method of worksheet class failed" on either
Code:
 'Sheets("Sheet2").Select
 ThisWorkbook.Sheets("Sheet2").Select

Sheet10 is a hidden tab. Sheet2 is not hidden.

What is wrong?

Thanks in advance.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Is ThisWorkbook the active workbook? If not, you need to activate it before selecting a sheet on it.
 
Upvote 0
You shouldn't need to select. Try like this

Code:
Set Sh = ThisWorkbook.Sheets("Sheet10")
With Sh
    Set Rng = .Cells(1, .Columns.Count).End(xlToLeft)
    Set Rng = Range(Rng, Rng.End(xlToLeft))
End With
Rng.Copy Destination:=Sheets("Sheet2").Range("A1")
 
Upvote 0
You shouldn't need to select. Try like this

Code:
Set Sh = ThisWorkbook.Sheets("Sheet10")
With Sh
    Set Rng = .Cells(1, .Columns.Count).End(xlToLeft)
    Set Rng = Range(Rng, Rng.End(xlToLeft))
End With
Rng.Copy Destination:=Sheets("Sheet2").Range("A1")

Does this paste? What I have is:

Code:
 Set Sh = ThisWorkbook.Sheets("Sheet10")
    With Sh
        Set rng = .Cells(1, .Columns.count).End(xlToLeft)
        Set rng = Range(rng, rng.End(xlToLeft))
    End With
 rng.Copy


 Sheets("Sheet2").Select
 Range("F12").Select
 ActiveSheet.Paste

Does your code replace this? It works fine without hidden tabs!
 
Upvote 0
This will paste into F12 of Sheet2

Code:
Set Sh = ThisWorkbook.Sheets("Sheet10")
With Sh
    Set Rng = .Cells(1, .Columns.Count).End(xlToLeft)
    Set Rng = Range(Rng, Rng.End(xlToLeft))
End With
Rng.Copy Destination:=Sheets("Sheet2").Range("F12")
 
Upvote 0
This will paste into F12 of Sheet2

Code:
Set Sh = ThisWorkbook.Sheets("Sheet10")
With Sh
    Set Rng = .Cells(1, .Columns.Count).End(xlToLeft)
    Set Rng = Range(Rng, Rng.End(xlToLeft))
End With
Rng.Copy Destination:=Sheets("Sheet2").Range("F12")
Thanks VOG. Seems to work. One more question: If I call Workbooks.Open("someFileName")

And then further down my code, I do:

Code:
Sheets("OriginalWorkbookSheet").Select
or
Code:
 ThisWorkbook.Sheets("OriginalWorkbookSheet").Select

I am getting an out or range error. I believe this is because I open someFileName. What i want to do is go back to my original workbook and select a sheet.

How can I adjust this?
 
Upvote 0
If you must select (it is very rarely necessary) then you would need code like

Code:
ThisWorkbook.Activate
Sheets("OriginalWorkbookSheet").Select
Range("B1").Select
 
Upvote 0
If you must select (it is very rarely necessary) then you would need code like

Code:
ThisWorkbook.Activate
Sheets("OriginalWorkbookSheet").Select
Range("B1").Select

Thanks for your help Vog. Kind regards.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,718
Members
452,939
Latest member
WCrawford

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