Changing VBA code into VBA Arrays

  • Thread starter Thread starter Legacy 93538
  • Start date Start date
L

Legacy 93538

Guest
Hi

I needs some help with arrays. I never use arrays and i dont understand them. I need to turn the following three pieces of code into three arrays but i have no idea how:

Code:
wbNew.Sheets("MTH").Range("B2:FF4000").Copy
wbGCT.Sheets("MTH").Range("B2:FF4000").PasteSpecial
wbNew.Sheets("MTHPrevious").Range("B2:FF4000").Copy
wbGCT.Sheets("MTHPrevious").Range("B2:FF4000").PasteSpecial
wbNew.Sheets("YTD").Range("B2:FF3000").Copy
wbGCT.Sheets("YTD").Range("B2:FF3000").PasteSpecial
wbNew.Sheets("YTDPrevious").Range("B2:FF3000").Copy
wbGCT.Sheets("YTDPrevious").Range("B2:FF3000").PasteSpecial
wbNew.Sheets("R12").Range("B2:FF3000").Copy
wbGCT.Sheets("R12").Range("B2:FF3000").PasteSpecial
wbNew.Sheets("R12Previous").Range("B2:FF3000").Copy
wbGCT.Sheets("R12Previous").Range("B2:FF3000").PasteSpecial

Code:
Sheets("Graphing").Select: Range("A1").Select: Sheets("Settings").Select: Range("A1").Select
Sheets("R12Previous").Select: Range("A1").Select: Sheets("R12").Select: Range("A1").Select
Sheets("YTDPrevious").Select: Range("A1").Select: Sheets("YTD").Select: Range("A1").Select
Sheets("MTHPrevious").Select: Range("A1").Select: Sheets("MTH").Select: Range("A1").Select
Sheets("Charts").Select: Range("A1").Select: Sheets("Home").Select: Range("A1").Select

Code:
Sheets("Graphing").Visible = xlSheetVeryHidden
Sheets("Settings").Visible = xlSheetVeryHidden
Sheets("R12Previous").Visible = xlSheetVeryHidden
Sheets("R12").Visible = xlSheetVeryHidden
Sheets("YTDPrevious").Visible = xlSheetVeryHidden
Sheets("YTD").Visible = xlSheetVeryHidden
Sheets("MTHPrevious").Visible = xlSheetVeryHidden
Sheets("MTH").Visible = xlSheetVeryHidden

Can anyone please help me?

Thanks

Jessicaseymour
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I never use arrays and i dont understand them. I need to turn the following three pieces of code into three arrays but i have no idea how:

Then why do you want to do this, and what do you mean exactly?
 
Upvote 0
Unfortunatly i have to, its the way my company write VBA they use arrays alot and i must comply.

Sorry i should explain i wrote the three bits of code and i have been asked to change them so they use arrays so it loops through an array instead of repeating the same line of code.
 
Upvote 0
For instance:

Code:
    For Each sht In Array("Graphing", "Settings", "R12Previous", "R12", "YTDPrevious", "YTD", "MTHPrevious", "MTH")
        Sheets(sht).Visible = xlSheetVeryHidden
    Next

Wigi
 
Upvote 0
For example:
Code:
Dim varSheets, varSheet
varSheets = Array("MTH", "MTHPrevious", "YTD")
for each varsheet in varsheets
   wbNew.Sheets(varsheet).Range("B2:FF4000").Copy
wbGCT.Sheets(varsheet).Range("B2:FF4000").PasteSpecial
next varsheet
 
Upvote 0
Hi

Thanks that works!

But how can i use that for the code below:

Code:
wbNew.Sheets("MTH").Range("B2:FF4000").Copy
wbGCT.Sheets("MTH").Range("B2:FF4000").PasteSpecial

wbNew.Sheets("MTHPrevious").Range("B2:FF4000").Copy
wbGCT.Sheets("MTHPrevious").Range("B2:FF4000").PasteSpecial

wbNew.Sheets("YTD").Range("B2:FF3000").Copy
wbGCT.Sheets("YTD").Range("B2:FF3000").PasteSpecial

wbNew.Sheets("YTDPrevious").Range("B2:FF3000").Copy
wbGCT.Sheets("YTDPrevious").Range("B2:FF3000").PasteSpecial

wbNew.Sheets("R12").Range("B2:FF3000").Copy
wbGCT.Sheets("R12").Range("B2:FF3000").PasteSpecial

wbNew.Sheets("R12Previous").Range("B2:FF3000").Copy
wbGCT.Sheets("R12Previous").Range("B2:FF3000").PasteSpecial
 
Upvote 0
Hi

Thanks to everyone who helped me!! all working now thanks so much!!!

I greatly appricate everyones help!!!!!!
 
Upvote 0

Forum statistics

Threads
1,224,561
Messages
6,179,522
Members
452,923
Latest member
JackiG

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