Using the sheet name stored in a cell in the VBA code

Hello,
I am trying to use the name of the sheet that is stored in a cell in the rest of code, but having an issue with my code in line 5:

Sub CopyRange()

Dim ws As Worksheet

ws = ThisWorkbook.Worksheets(ActiveSheet.Range("B2").Value)

ws.Range("A1:Z100").Copy

ActiveSheet.Range("B10").PasteSpecial xlPasteValues

End Sub

Open in new window

Dmitriy KritskiyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
Change this line to..

ws = ThisWorkbook.Worksheets(ActiveSheet.Range("B2").Value)

Open in new window


To this..

Set ws = ThisWorkbook.Worksheets(ActiveSheet.Range("B2").Value)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dmitriy KritskiyAuthor Commented:
Still getting subscript out of range error in this line
0
Saurabh Singh TeotiaCommented:
Try adding Double quotes like this...

Set ws = ThisWorkbook.Worksheets(""& ActiveSheet.Range("B2").Value &"")

Open in new window


If you still get an error can you post your sample workbook...
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Martin LissOlder than dirtCommented:
The code name of sheets are Sheet1, Sheet2, etc. Have you changed the name of a sheet you want to access? If so B2 should have the name of the sheet, rather than the code name.
0
Martin LissOlder than dirtCommented:
If you are using the code name then you can do this.

Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets
    If UCase(ws.CodeName) = UCase(ActiveSheet.Range("B2").Value) Then
        ' If you need it
        'Set ws = Sheets(ws.Index)
        Exit For
    End If
Next

ws.Range("A1:Z100").Copy

ActiveSheet.Range("B10").PasteSpecial xlPasteValues

Open in new window

0
Roy CoxGroup Finance ManagerCommented:
You need to use Set with a worksheet.

Sub CopyRange()

Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets(ActiveSheet.Range("B2").Value)

ws.Range("A1:Z100").Copy

ActiveSheet.Range("B10").PasteSpecial xlPasteValues

End Sub

Open in new window

0
Martin LissOlder than dirtCommented:
@Roy: Did you see post IDs 40721098?
0
Roy CoxGroup Finance ManagerCommented:
No I missed that one, must have scrolled too fast.
0
Dmitriy KritskiyAuthor Commented:
ok when I create a sample workbook, it works so the issue is somewhere in my actual workbook. I will figure it out and accept the solution that works.
0
Saurabh Singh TeotiaCommented:
Dmitriy,

What i will do in order to move this code full proof is rather then referring to activesheet..I will give proper sheet name to pick this value from..

Saurabh...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.