Open workbook and set worksheet

drop05

Active Member
Joined
Mar 23, 2021
Messages
285
Office Version
  1. 365
Platform
  1. Windows
Hi, im trying to set a workbook and worksheet in vba so i dont have to keep referencing the sheet name in my code
here is my code
is there a way to make it where i dont have to keep typing "sheet1"

Sub Get_data()

'allow the user to select an excel file

Dim FileToOpen As Variant
Dim FileToPaste As Variant
Dim copy_wb As Workbook
Dim paste_wb As Workbook
Dim Total As Double
Dim Yes As String
Dim Blank As String

Application.ScreenUpdating = False

Blank = "Blank"
Yes = "Yes"
No = "No"


'open workbook
FileToOpen = Application.GetOpenFilename(Title:="Browse for File To Copy Data", FileFilter:="All Files(*.*),*.*")
FileToPaste = Application.GetOpenFilename(Title:="Browse for Paste File", FileFilter:="All Files(*.*),*.*")

'Open the files from client to copy from then the admn tool file to paste to

If FileToOpen <> False Then
Set copy_wb = Application.Workbooks.Open(FileToOpen)
Set paste_wb = Application.Workbooks.Open(FileToPaste)

copy_wb.Worksheets("Sheet1 (Pg 2)").Range("D13:D14").Copy
paste_wb.Worksheets("Sheet C (IS)").Range("D5:D6").PasteSpecial Paste:=xlPasteValues

copy_wb.Worksheets("Sheet1 (Pg 2)").Range("D16:D21").Copy
paste_wb.Worksheets("Sheet C (IS)").Range("D8:D13").PasteSpecial Paste:=xlPasteValues

See how i am doing copy_wb.worksheets("Sheet1 (Pg 2)"). . .
is there a way to make it shorter or set it to something then do the .Range()

thank you!
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try this

VBA Code:
Dim S
Set S=Sheets("Sheet1 (Pg 2)")
copy_wb.Worksheets(S.Range("D13:D14").Copy

etc
 
Upvote 0

Forum statistics

Threads
1,224,809
Messages
6,181,075
Members
453,020
Latest member
mattg2448

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