Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-08-2019, 02:21 AM
Jelmer Jelmer is offline Create new workbooks from template based on sheet with data Windows Vista Create new workbooks from template based on sheet with data Office 2016
Novice
Create new workbooks from template based on sheet with data
 
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
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy data from one sheet to another based on a certain criteria shina67 Excel Programming 2 12-28-2016 07:32 AM
Create new workbooks from template based on sheet with data Mailmerge create a coloured box based on data haydencohen Mail Merge 1 11-25-2015 03:32 PM
Create new workbooks from template based on sheet with data Create custom quote based on template torma156 Word 3 09-06-2015 11:04 AM
Create new workbooks from template based on sheet with data Create a Bar with a slider that move based on data jgallet Excel 8 08-21-2015 03:41 PM
How to create a data validation based on another cell's value?? cfreezy Excel 1 06-18-2015 09:51 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:20 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft