VBA Macro using Loop and SaveAs for two workbooks

Hyking

New Member
Joined
Apr 5, 2013
Messages
1
Hi
Can someone help a VBA newbie on creating a macro to Loop thru a Sourcedata workbook copying data into a Template workbook, and then SaveAs a new workbook for each piece of data copied. I am creating a new workbook for each service ID and have created the file name in a "LookupData"cell .
The macro works if I include the SourceData in the Template workbook but I don't want to send out the saved spreadsheets with the SourceData.
Here is what I have drafted so far but am having trouble with Activating the two different workbooks at the critical time to save and then return to the Template to run the next loop. I open the Template first and run the macro from there.
Let me know if you require more info. Regards Terry.

Sub SaveSARfiles()
Workbooks.Open Filename:=ThisWorkbook.Path & "\SARdata.xls"

Dim a As Long
Workbooks("SARdata.xls").Sheets("SourceData").Activate

For a = 3 To 55
If Cells(a, 2) <> "" Then
Workbooks("Template.xls").Sheets("CoverSheet").Cells(26, 2) = Workbooks("SARdata").Sheets("SourceData").Cells(a, 2)
'save worksheet using Filename
Workbooks("Template.xls").Sheets("CoverSheet").Select

Dim MyCurrPath As String
MyCurrPath = ThisWorkbook.Path
ThisFile = Sheets("LookupData").Cells(1, 1)
ActiveWorkbook.SaveAs Filename:=MyCurrPath & "\" & ThisFile

ElseIf Cells(a, 2) = "" Then
Exit Sub
End If
Next a

End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Welcome to the board,

Please surround vba in code tags. See my signature line. It helps when copying and pasting into the VBA editor, so we don't have to reformat the code to read/edit it.

Thanks,
RJ
 
Upvote 0

Forum statistics

Threads
1,214,620
Messages
6,120,554
Members
448,970
Latest member
kennimack

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