VBA - copy tab to new workbook and save

RobynLouise

New Member
Joined
Jan 6, 2017
Messages
20
Hi

I have workbooks saved for each of our distribution centres, each workbook contains multiple tabs detailing different things. I want to extract one particular tab out of each of these worksheets, there are 80+ so don't want to do it manually! Using existing VBA that I have tweaked, I've come up with the following. It's not working (I am a beginner) and I'm looking for some help to get it working! This VBA is in a sheet which essentially houses the list of distribution centres. I have named each distribution centre name cell "file 1, file 2" etc

Sub Test1()


TotalCentres = Range("TotalCentres") (this is a countif of how many are in the list)


For bLoop = 1 To TotalCentres

Workbooks.Open Filename:="H:\Template0.xlsm"

' Wait time
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 8
WaitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait WaitTime


Workbooks.Open Filename:= _
"xxxxxxxxxxxxxxxxxxxxxxxxx\profiles" & Range("File" & bLoop) & ".xlsx" _
, Password:="xxx", UpdateLinks:=0

Worksheets("Employment").Activate
Sheets(“Employment”).Copy After.= _
Workbooks(“Template0.xlsm”).Sheets(1)

' Wait time
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 20
WaitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait WaitTime




Workbooks("Template0.xlsm").Activate
Sheets("Employment”).Select
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False


Application.DisplayAlerts = False
Sheets(1).Delete
Application.DisplayAlerts = True


ActiveWorkbook.SaveAs _
Filename:="xxxxxxxxxxx\profiles\Employment records" & cll_centre & ".xlsm", _
Password:="xxx"



ActiveWorkbook.Close savechanges:=False

' Wait time
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 3
WaitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait WaitTime


Next cll
End With
End Sub



Am I missing something obvious? Is there an easier way to do this (for a beginner!)
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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