View Single Post
 
Old 03-08-2019, 02:21 AM
Jelmer Jelmer is offline Windows Vista Office 2016
Novice
 
Join Date: May 2017
Location: Netherlands/UK
Posts: 15
Jelmer is on a distinguished road
Default

I now have the below and it is working, however, it does not recognize the visible rows correctly and it does not stop automatically (it will stop because there is already a file called '-CRS.xlsx' in the output folder. What am I doing wrong?

Code:
 
Sub Create_Workbook_From_Template_and_Save()
Dim rng As Range
Dim x As Integer
Set rng = Workbooks("CRS Data Input").Worksheets("Data").ListObjects("Table_query").Range.SpecialCells(xlCellTypeVisible)

x = 0

For x = 0 To rng.Rows.Count
Dim Flname As String
Dim Rev As String
Dim Title As String
Dim Subcontractor As String
Flname = "B" & (2 + x)
Rev = "F" & (2 + x)
Title = "E" & (2 + x)
Subcontractor = "I" & (2 + x)

'Open new workbook from template
Workbooks.Open FileName:=ThisWorkbook.Path & "\CRS Template.xlsx", Editable:=False
'Copy data from data sheet to template

Workbooks("CRS Data Input").Worksheets("Data").Range(Flname).Copy Destination:=Workbooks("CRS Template").Worksheets("CRS").Range("K1")
Workbooks("CRS Data Input").Worksheets("Data").Range(Rev).Copy Destination:=Workbooks("CRS Template").Worksheets("CRS").Range("N1")
Workbooks("CRS Data Input").Worksheets("Data").Range(Title).Copy Destination:=Workbooks("CRS Template").Worksheets("CRS").Range("K2")
Workbooks("CRS Data Input").Worksheets("Data").Range(Subcontractor).Copy Destination:=Workbooks("CRS Template").Worksheets("CRS").Range("K3")
'Save template in Output folder with custom filename
Dim FileName As String
FileName1 = Range("K1").Text
Workbooks("CRS Template").Close SaveChanges:=True, FileName:=ThisWorkbook.Path & "\Output\" & FileName1 & "-CRS.xlsx"
Next x
End Sub

Last edited by Pecoflyer; 03-08-2019 at 04:41 AM. Reason: Removed unnecessary quotes
Reply With Quote