vba to refer to a sheet name based on a cell's content

cjcass

Well-known Member
Joined
Oct 27, 2011
Messages
683
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I'm looking for some vba that will enable me to refer to a wksheet name based on the contents of a cell.
My existing code is as follows:

Sheet15.Select
Range("A7:CU10000").Select
Selection.Copy


Sheet19.Select


Range("A7").Select

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A7").Select

I'm looking to replace the code 'Sheet19.Select' in the middle with the name of a wksheet that will be typed into cell B4. So, as an example, if I want the data to be pasted into a wksheet named '23' (the tab name), I will type the number '23' into B4 and then run the macro.
Hope this makes sense and you can help, many thanks.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
this declares 'sh' as a worksheet object, then sets the variable value as a worksheet named for the value in sheet19, cell B4.
Code:
Sub t()
Dim sh As Worksheet
Set sh = Sheets(Sheets("Sheet19").Range("B4").Value)
With sh
    MsgBox .Name
End With
End Sub
 
Upvote 0
ok, can you show me where this fits into my existing code please, thanks.
 
Upvote 0
ok, can you show me where this fits into my existing code please, thanks.

You tell me which sheet you intend to use cell B4 to enter the sheet name into.
 
Last edited:
Upvote 0
Code:
Dim sh As Worksheet
Set sh = Sheets(Sheets("Data").Range("B4").Value)
 Sheet15.Range("A7:CU10000").Copy
 sh.Range("A7"),Select

It would be more efficient if you learned to write the code without using Seclect and Activate.
 
Last edited:
Upvote 0
If you want to paste the copied data into that sheet at cell A7 then
Code:
Dim sh As Worksheet
Set sh = Sheets(Sheets("Data").Range("B4").Value)
 Sheet15.Range("A7:CU10000").Copy sh.Range("A7") 'assumes code name for sheet is used
If you have formulas and only want to paste values into the sheet then
Code:
Dim sh As Worksheet
Set sh = Sheets(Sheets("Data").Range("B4").Value)
 Sheet15.Range("A7:CU10000").Copy 'assumes code name for sheet is used
 sh.Range("A7"),PasteSpecial xlPasteValues

Notice there are no Activate or Select statements used. Also, if you ran the code as a macro, there would be no flicker and flash.
 
Upvote 0
Hi,
Thanks for your work on this, almost there...
I need the code 'sh.Range("A7")' to refer to the worksheet's tab name as opposed to it's code name if possible, thanks.
 
Upvote 0
Hi,
Thanks for your work on this, almost there...
I need the code 'sh.Range("A7")' to refer to the worksheet's tab name as opposed to it's code name if possible, thanks.

Show me what you put in cell B4. I understood that you used numbers for names. In VBA sheets can be referred to as Sheets(1), using the collection index number, or Sheet1, which is the code name, or Sheets("Sheet1"), which uses a string name. Believe it or not, these three references can be three entirely different sheets in the same workbook. The code I used for the 'sh' variable assumed that you were using a string name in cell B4. If you are using the index number then the code would be slightly different. If you have the full reference, eg. Sheet19 or Sheets("19"). then I need to know exactly what is in that cell B4.
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

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