VBA - Cell Reference as Worksheet

cynthi263

New Member
Joined
May 14, 2019
Messages
19
Hello - i have an excel file that has numerous tabs. Each have a name, what i am trying to do is in the main sheet, enter in the tab name so that the info that is copied will paste to the correct tab. see code below - i get an error on the top line. The Tab will change each time i go into the report so i need to be able to enter in my tab name each time and just run the macro.

Sheet2 is my main sheet where the info is at. AE4 is the cell where i have input the tab name

Sub MoveFI()


ws1 = Sheets("Sheet2").Range("AE4").Value
Sheets("Sheet2").Select
Range("A2:B1200").Copy
Sheets("ws1").Select
Range("A2").Select
ActiveSheet.Paste


End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi & welcome to MrExcel.
What is the error number & message that you get?
 
Upvote 0
I adjusted the code a bit and ran it without the first line and specified the ws1 to the name of the tab and it worked fine.

Sub CopyData()


ws1 = Sheets(Sheets("ALL").Range("AE4").Value)


Sheets("ALL").Select
Range("A2:B1200").Copy
Sheets("ws1").Select
Range("A2").Select
ActiveSheet.Paste


End Sub
 
Upvote 0
That code cannot work, try
Code:
Sub CopyData()
   Dim Ws1 As Worksheet
   
   Set Ws1 = Sheets(Sheets("All").Range("A4").Value)
   Sheets("All").Range("A2:B1200").Copy
   Ws1.Range("A2").PasteSpecial
End Sub
 
Upvote 0
Hello - i have an excel file that has numerous tabs. Each have a name, what i am trying to do is in the main sheet, enter in the tab name so that the info that is copied will paste to the correct tab. see code below - i get an error on the top line. The Tab will change each time i go into the report so i need to be able to enter in my tab name each time and just run the macro.

Sheet2 is my main sheet where the info is at. AE4 is the cell where i have input the tab name

Sub MoveFI()


ws1 = Sheets("Sheet2").Range("AE4").Value
Sheets("Sheet2").Select
Range("A2:B1200").Copy
Sheets("ws1").Select 'You should not bring quotation marks
Range("A2").Select
ActiveSheet.Paste


End Sub

Try this

Code:
Sub MoveFI()
    Dim ws1 As String
    
    ws1 = Sheets("Sheet2").Range("AE4").Value
    Sheets("Sheet2").Range("A2:B1200").Copy
    Sheets(ws1).Range("A2").PasteSpecial xlAll
    Application.CutCopyMode = False
End Sub
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,705
Messages
6,173,986
Members
452,541
Latest member
haasro02

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