Probably a really easy question...

largeselection

Active Member
Joined
Aug 4, 2008
Messages
358
Quick question, I want to reference a workbook that i mentioned quite a bit in my macro code. However, I want to be able to change this workbook by just changing one line of code rather than having to go into my rather long code to change it every time.

Here's what I have so far:

Dim MyFile As Workbook

Set MyFile = ActiveWorkbook

Dim MyTarget As Workbook

Set MyTarget = windows("week 28 O.xls")

Then later in the code I have it reference MyTarget and MyFile.

MyFile opens the current workbook that I am creating and I was hoping that I could put MyTarget.Activate into every instance where it occurs and it would reference week 28 o.xls. (originally when I recorded this it had "week 28 O.xls".activate) So what I figured is that rather than having to go in and change the 28 to a 29 in every instance in the code (about 25 of them) I would just set MyTarget = ___ that way if I want to change to week 29 in all those instances I just have to change the 28 to a 29 in the set MyTarget line and then all the references to my target would apply to the new week 29.

However I'm getting an error at the Set MyTarget = windows("week 28 O.xls") line.

I feel like this is probably an easy fix. Please ask any and all questions so I can help clarify.
 
Hi I used that macro and got the correct expected result. I will paste the full code that I have so that you can see everything:

Dim MyFile As Workbook
Set MyFile = ActiveWorkbook

Dim MyTarget As Workbook
Set MyTarget = Workbook("Week 28 O.xls")

MyTarget.Activate
Range("D190").Select
ActiveWindow.SmallScroll Down:=-30
Range("A160:E160").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
MyFile.Activate
ActiveSheet.Paste
Sheets("547").Select
MyTarget.Activate
Sheets("547").Select
Range("E208").Select
ActiveWindow.SmallScroll Down:=-27
Range("A160:E160").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Sheets("549").Select
MyTarget.Activate
Sheets("549").Select
ActiveWindow.SmallScroll Down:=-27
Range("A160:E161").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Sheets("563").Select
MyTarget.Activate
Sheets("563").Select
Range("B193").Select
ActiveWindow.SmallScroll Down:=-24
Range("A160:E161").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Range("E15").Select
Sheets("406").Select
MyTarget.Activate
Sheets("406").Select
Range("E207").Select
ActiveWindow.SmallScroll Down:=-42
Range("A160:E161").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Range("D14").Select
Sheets("412").Select
MyTarget.Activate
Sheets("412").Select
Range("E203").Select
ActiveWindow.SmallScroll Down:=-21
Range("A160:E160").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Range("E18").Select
Sheets("579").Select
MyTarget.Activate
Sheets("579").Select
Range("E201").Select
ActiveWindow.SmallScroll Down:=-24
Range("A160:E161").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Sheets("469").Select
MyTarget.Activate
Sheets("469").Select
Range("E206").Select
ActiveWindow.SmallScroll Down:=-24
Range("A160:E161").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Range("E25").Select
Sheets("509").Select
MyTarget.Activate
Sheets("509").Select
ActiveWindow.SmallScroll Down:=-27
Range("A160:E161").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Sheets("522").Select
MyTarget.Activate
Sheets("522").Select
Range("E201").Select
ActiveWindow.SmallScroll Down:=-27
Range("A160:E160").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Sheets("523").Select
MyTarget.Activate
Sheets("523").Select
Range("E200").Select
ActiveWindow.SmallScroll Down:=-21
Range("A161:E162").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
MyTarget.Activate
ActiveWindow.SmallScroll Down:=-30
Range("A161:E217").Select
ActiveWindow.SmallScroll Down:=-24
Range("A160:E161").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Range("H22").Select
ActiveWindow.SmallScroll Down:=-6
Sheets("525").Select
MyTarget.Activate
Sheets("525").Select
Range("E201").Select
ActiveWindow.SmallScroll Down:=-39
Range("A160:E161").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Sheets("531").Select
MyTarget.Activate
Sheets("531").Select
Range("E203").Select
ActiveWindow.SmallScroll Down:=-24
Range("A160:E160").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Sheets("532").Select
MyTarget.Activate
Sheets("532").Select
Range("E201").Select
ActiveWindow.SmallScroll Down:=-33
Range("A160:E160").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Sheets("533").Select
MyTarget.Activate
Sheets("533").Select
Range("E208").Select
ActiveWindow.SmallScroll Down:=-27
Range("A160:E161").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Sheets("534").Select
MyTarget.Activate
Sheets("534").Select
Range("E207").Select
ActiveWindow.SmallScroll Down:=-30
Range("A160:E161").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Sheets("536").Select
MyTarget.Activate
Sheets("536").Select
Range("E203").Select
ActiveWindow.SmallScroll Down:=-27
Range("A160:E161").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Sheets("537").Select
MyTarget.Activate
Sheets("537").Select
ActiveWindow.SmallScroll Down:=-30
Range("A160:E161").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Sheets("538").Select
MyTarget.Activate
Sheets("538").Select
Range("C198").Select
ActiveWindow.SmallScroll Down:=-24
Range("A160:E160").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
ActiveWindow.SmallScroll Down:=-3
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Sheets("066").Select
MyTarget.Activate
Sheets("066").Select
Range("E206").Select
ActiveWindow.SmallScroll Down:=-33
Range("A160:E160").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Sheets("254").Select
MyTarget.Activate
Sheets("254").Select
Range("A160:E160").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Sheets("491").Select
MyTarget.Activate
Sheets("491").Select
ActiveWindow.SmallScroll Down:=-27
Range("A160:E160").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Range("F26").Select
MyTarget.Activate
Sheets("498").Select
ActiveWindow.ScrollWorkbookTabs Sheets:=1
ActiveWindow.ScrollWorkbookTabs Sheets:=5
MyFile.Activate
Sheets("498").Select
MyTarget.Activate
Range("D197").Select
ActiveWindow.SmallScroll Down:=-21
Range("A160:E161").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Sheets("512").Select
MyTarget.Activate
Sheets("512").Select
Range("E196").Select
ActiveWindow.SmallScroll Down:=-21
Range("A160:E161").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Sheets("504").Select
MyTarget.Activate
Sheets("504").Select
Range("E199").Select
ActiveWindow.SmallScroll Down:=-33
Range("A160:E160").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Sheets("507").Select
MyTarget.Activate
Sheets("507").Select
Range("E202").Select
ActiveWindow.SmallScroll Down:=-33
Range("A160:E160").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Range("D23").Select
Sheets("564").Select
MyTarget.Activate
Sheets("564").Select
Range("E195").Select
ActiveWindow.SmallScroll Down:=-21
Range("A160:E161").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
MyFile.Activate
Range("A3").Select
ActiveSheet.Paste
Range("G20").Select
ActiveWindow.ScrollWorkbookTabs Position:=xlFirst
Sheets(Array("520", "547", "549", "563", "406", "412", "579", "469", "509", "522", "523", _
"525", "531", "532", "533", "534", "536", "537", "538", "066", "254", "491", "498", "512", _
"564")).Select
Sheets("564").Activate
Sheets(Array("504", "507")).Select Replace:=False
Rows("4:4").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Rows("8:8").Select
Selection.Delete Shift:=xlUp
Range("D3").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWindow.SmallScroll Down:=-33
Selection.Cut
Range("B3").Select
Selection.Insert Shift:=xlToRight
Range("E3").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWindow.SmallScroll Down:=-27
Selection.Cut
Range("D3").Select
Selection.Insert Shift:=xlToRight
Range("F11").Select
Sheets(Array("520", "547", "549", "563", "406", "412", "579", "469", "509", "522", "523", _
"525", "531", "532", "533", "534", "536", "537", "538", "066", "254", "491", "498", "512", _
"504")).Select
Sheets("522").Activate
Sheets(Array("507", "564")).Select Replace:=False
Sheets(Array("520", "547", "549", "563", "406", "412", "579", "469", "509", "522", "523", _
"525", "531", "532", "533", "534", "536", "537", "538", "066", "254", "491", "498", "512", _
"504")).Select
Sheets("522").Activate
Sheets(Array("507", "564")).Select Replace:=False
Range("G41").Select
Sheets(Array("520", "547", "549", "563", "406", "412", "579", "469", "509", "522", "523", _
"525", "531", "532", "533", "534", "536", "537", "538", "066", "254", "491", "498", "512", _
"504")).Select
Sheets("523").Activate
Sheets(Array("507", "564")).Select Replace:=False
Range("F33").Select
Sheets(Array("520", "547", "549", "563", "406", "412", "579", "469", "509", "522", "523", _
"525", "531", "532", "533", "534", "536", "537", "538", "066", "254", "491", "498", "512", _
"504")).Select
Sheets("523").Activate
Sheets(Array("507", "564")).Select Replace:=False
Sheets(Array("520", "547", "549", "563", "406", "412", "579", "469", "509", "522", "523", _
"525", "531", "532", "533", "534", "536", "537", "538", "066", "254", "491", "498", "512", _
"504")).Select
Sheets("525").Activate
Sheets(Array("507", "564")).Select Replace:=False
Sheets(Array("520", "547", "549", "563", "406", "412", "579", "469", "509", "522", "523", _
"525", "531", "532", "533", "534", "536", "537", "538", "066", "254", "491", "498", "512", _
"504")).Select
Sheets("531").Activate
Sheets(Array("507", "564")).Select Replace:=False
Sheets(Array("520", "547", "549", "563", "406", "412", "579", "469", "509", "522", "523", _
"525", "531", "532", "533", "534", "536", "537", "538", "066", "254", "491", "498", "512", _
"504")).Select
Sheets("531").Activate
Sheets(Array("507", "564")).Select Replace:=False
Sheets("520").Select
Range("B23").Select
Sheets("531").Select
Columns("E:E").EntireColumn.AutoFit
Columns("D:D").EntireColumn.AutoFit
Sheets(Array("520", "547", "549", "563", "406", "412", "579", "469", "509", "522", "523", _
"525", "531", "532", "533", "534", "536", "537", "538", "066", "254", "491", "498", "512", _
"504")).Select
Sheets("520").Activate
Sheets(Array("507", "564")).Select Replace:=False
Cells.Select
Cells.EntireColumn.AutoFit
Range("D12").Select
End Sub

Thanks for bearing with me...
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Dim MyFile As Workbook
Set MyFile = ActiveWorkbook

Dim MyTarget As Workbook
Set MyTarget = Workbook("Week 28 O.xls")

You are still missing the S there. Somehow I don't think it's a typo anymore, because you copy pasted that code from your book to the forum.

Set MyTarget = WorkbookS("Week 28 O.xls")
 
Upvote 0
****,

I'm so annoyed! :) That worked perfectly. Somehow when I was tinkering in the code I lost the S. I originally had it in, but I guess during an edit I lost it.

Thanks for all your help everyone!!!! Love this site!
 
Upvote 0

Forum statistics

Threads
1,224,880
Messages
6,181,532
Members
453,054
Latest member
ezzat

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