New to VBA: Code review

poncho12345678

New Member
Joined
Feb 16, 2016
Messages
4
New to VBA, the following code works perfectly, however I would like to condense the copy section using a loop, but not sure how to go about it; I'm learning so any other tips on improving it will be well received; researched on here for well over a day, but have ended up confusing myself.


Code:
Sub ImportMonthlyData_Click()
ImportMonthlyData.Caption = ThisWorkbook.Sheets("LABELS").Range("E3")
ImportMonthlyData.BackColor = vbGreen
Workbooks.Open "FEB2016.xlsm"
Workbooks("FEB2016.xlsm").Sheets("JP").Range("C1:FA45").Copy Workbooks("DASHBOARD.xlsm").Sheets("JP").Range("C1:FA45")
Workbooks("FEB2016.xlsm").Sheets("LO").Range("C1:FA45").Copy Workbooks("DASHBOARD.xlsm").Sheets("LO").Range("C1:FA45")
Workbooks("FEB2016.xlsm").Sheets("CF").Range("C1:FA45").Copy Workbooks("DASHBOARD.xlsm").Sheets("CF").Range("C1:FA45")
Workbooks("FEB2016.xlsm").Activate
ActiveWorkbook.Close SaveChanges:=False
Workbooks("DASHBOARD.xlsm").Activate
Worksheets("MAIN").Activate
Range("A1").Select
ActiveWorkbook.Save
End Sub

Any help would be appreciated.
 
New to VBA, the following code works perfectly, however I would like to condense the copy section using a loop, but not sure how to go about it; I'm learning so any other tips on improving it will be well received; researched on here for well over a day, but have ended up confusing myself.
....
Any help would be appreciated.

test well , if I understand correctly , you would like something like this

Code:
Sub ImportMonthlyData_Click() Dim x1 As Long, xSheets As Variant
 xSheets = Array("JP", "LO", "CF")
 
 ImportMonthlyData.Caption = ThisWorkbook.Sheets("LABELS").Range("E3")
 ImportMonthlyData.BackColor = vbGreen
 Workbooks.Open "FEB2016.xlsm"
 For x1 = 0 To UBound(xSheets)
   Workbooks("FEB2016.xlsm").Sheets(xSheets(x1)).Range("C1:FA45").Copy Workbooks("DASHBOARD.xlsm").Sheets(xSheets(x1)).Range("C1:FA45")
  'Workbooks("FEB2016.xlsm").Sheets("JP").Range("C1:FA45").Copy Workbooks("DASHBOARD.xlsm").Sheets("JP").Range("C1:FA45")
  'Workbooks("FEB2016.xlsm").Sheets("LO").Range("C1:FA45").Copy Workbooks("DASHBOARD.xlsm").Sheets("LO").Range("C1:FA45")
  'Workbooks("FEB2016.xlsm").Sheets("CF").Range("C1:FA45").Copy Workbooks("DASHBOARD.xlsm").Sheets("CF").Range("C1:FA45")
 Next x1
 Workbooks("FEB2016.xlsm").Activate
 ActiveWorkbook.Close SaveChanges:=False
 Workbooks("DASHBOARD.xlsm").Activate
 Worksheets("MAIN").Activate
 Range("A1").Select
 ActiveWorkbook.Save
End Sub
 
Upvote 0
So if I was to declare say:
wb as String
wb = "FEB2016.xlsm"

I could then use:

Workbooks(wb).Sheets(xSheets(x1)).Range("C1:FA45").Copy Workbooks("DASHBOARD.xlsm").Sheets(xSheets(x1)).Range("C1:FA45")

then I'd only have to change one occurrence per new macro?
 
Upvote 0
yes sure , if the destination sheet is always the same , you have to use the same macro to copy you other months . You should see the file structure and data , in order to optimize the macro

Code:
Sub ImportMonthlyData_Click() Dim x1 As Long, xSheets As Variant, xFiles As Variant
 xSheets = Array("JP", "LO", "CF")
 xFiles = "FEB2016.xlsm"
 ImportMonthlyData.Caption = ThisWorkbook.Sheets("LABELS").Range("E3")
 ImportMonthlyData.BackColor = vbGreen
 Workbooks.Open xFiles '"FEB2016.xlsm"
 For x1 = 0 To UBound(xSheets)
  Workbooks(xFiles).Sheets(xSheets(x1)).Range("C1:FA45").Copy Workbooks("DASHBOARD.xlsm").Sheets(xSheets(x1)).Range("C1:FA45")
 Next x1
 Workbooks(xFiles).Activate
 ActiveWorkbook.Close SaveChanges:=False
 Workbooks("DASHBOARD.xlsm").Activate
 Worksheets("MAIN").Activate
 Range("A1").Select
 ActiveWorkbook.Save
End Sub
 
Upvote 0

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