Create a new workbook from an Excel template located in a different folder











up vote
0
down vote

favorite












I have an Excel 2016 spreadsheet which contains the details of quotes sent to Customers (1 row for each quote containing cells with the Customer Name, Quote Date, Quote Number etc). I am trying to design a macro to reside in this spreadsheet that will create a new workbook based on an existing Excel template (UK - Quote Sheet.xltm), populate certain fields in the new workbook and save it with a filename name constructed from elements of quote entry. For example, if the spreadsheet contains an entry of 19/11/18 in the Date Cell, ABC in Customer Name field and Q1234 in the Quote Number field, the new file will be given the name "Q1234 - ABC (19.1.18).xlsm" and the Date, Customer Name and Quote Number will be inserted into the appropriate Worksheet cells in the new workbook. I have been able to get the macro to generate the appropriate filename and populate the cells in the new workbook but only by using absolute file paths for both the location of the template and the location of where the new workbook is to be stored. This is fine so long as the folder paths are the same on every PC I need to run the macro but this isn't the case so the macro fails when it's run on a PC where the file paths are different. Whilst the folder structure on every PC will be:



..DocumentsQuotesQuotes Database for the location of Quotes details spreadsheet
..DocumentsTemplatesQuotes for the location of the template file
..DocumentsQuotesAmendable Quotes for location of the new workbook



the location of this folder tree will vary by PC.



The following code works for when the above folder tree resides inside C:VD Operations but I need to modify so that it works no matter where the folder tree resides e.g. it could be under D:WorkVD Operations:



Sub CreateQuote()   'Generate a new quote using UK - Quote Sheet Template

Dim qContact, qNo, qDate, qCustomer As String
Dim qFilename As String
Dim qDay, qMonth, qYear As String
Dim qNewWorkbook As Workbook
Dim qDest As Worksheet

' Get quote details from appropriate entry in Quote Database

qContact = Cells(ActiveCell.Row, 13).Value
qNo = Cells(ActiveCell.Row, 1).Value
qCustomer = Cells(ActiveCell.Row, 12).Value
qDate = Cells(ActiveCell.Row, 3).Value
qDay = Day(qDate)
qMonth = Month(qDate)
qYear = Right(Year(qDate), 2)

Set qNewWorkbook = Workbooks.Open(Filename:="C:VD OperationsDocumentsTemplatesQuotesUK - QUOTE SHEET.xltm", _
Editable:=False)

' Construct quote file name

qFilename = "C:VD OperationsDocumentsQuotesAmendable Quotes" & qNo & " - " & qCustomer & " (" & qDay & "." & qMonth & "." & qYear & ").xlsm"

' Update Cover sheet in quote with values from Quote Database

Set qDest = qNewWorkbook.Sheets("Cover Sheet")
qDest.Range("QuoteNo") = qNo
qDest.Range("Customer") = qCustomer
qDest.Range("Contact") = qContact
qDest.Range("QuoteDate") = qDate

' Save the quote
qNewWorkbook.SaveAs Filename:=qFilename, FileFormat:=52
End Sub


I know I can find the path of the Quote details spreadsheet but not sure how to use this to calculate the path to the other files.










share|improve this question
























  • ThisWorkbook.Path will give you the location of the macro workbook, so you can use `..` to go "up" from there and then back down into the folder you need.
    – Tim Williams
    Nov 19 at 23:54

















up vote
0
down vote

favorite












I have an Excel 2016 spreadsheet which contains the details of quotes sent to Customers (1 row for each quote containing cells with the Customer Name, Quote Date, Quote Number etc). I am trying to design a macro to reside in this spreadsheet that will create a new workbook based on an existing Excel template (UK - Quote Sheet.xltm), populate certain fields in the new workbook and save it with a filename name constructed from elements of quote entry. For example, if the spreadsheet contains an entry of 19/11/18 in the Date Cell, ABC in Customer Name field and Q1234 in the Quote Number field, the new file will be given the name "Q1234 - ABC (19.1.18).xlsm" and the Date, Customer Name and Quote Number will be inserted into the appropriate Worksheet cells in the new workbook. I have been able to get the macro to generate the appropriate filename and populate the cells in the new workbook but only by using absolute file paths for both the location of the template and the location of where the new workbook is to be stored. This is fine so long as the folder paths are the same on every PC I need to run the macro but this isn't the case so the macro fails when it's run on a PC where the file paths are different. Whilst the folder structure on every PC will be:



..DocumentsQuotesQuotes Database for the location of Quotes details spreadsheet
..DocumentsTemplatesQuotes for the location of the template file
..DocumentsQuotesAmendable Quotes for location of the new workbook



the location of this folder tree will vary by PC.



The following code works for when the above folder tree resides inside C:VD Operations but I need to modify so that it works no matter where the folder tree resides e.g. it could be under D:WorkVD Operations:



Sub CreateQuote()   'Generate a new quote using UK - Quote Sheet Template

Dim qContact, qNo, qDate, qCustomer As String
Dim qFilename As String
Dim qDay, qMonth, qYear As String
Dim qNewWorkbook As Workbook
Dim qDest As Worksheet

' Get quote details from appropriate entry in Quote Database

qContact = Cells(ActiveCell.Row, 13).Value
qNo = Cells(ActiveCell.Row, 1).Value
qCustomer = Cells(ActiveCell.Row, 12).Value
qDate = Cells(ActiveCell.Row, 3).Value
qDay = Day(qDate)
qMonth = Month(qDate)
qYear = Right(Year(qDate), 2)

Set qNewWorkbook = Workbooks.Open(Filename:="C:VD OperationsDocumentsTemplatesQuotesUK - QUOTE SHEET.xltm", _
Editable:=False)

' Construct quote file name

qFilename = "C:VD OperationsDocumentsQuotesAmendable Quotes" & qNo & " - " & qCustomer & " (" & qDay & "." & qMonth & "." & qYear & ").xlsm"

' Update Cover sheet in quote with values from Quote Database

Set qDest = qNewWorkbook.Sheets("Cover Sheet")
qDest.Range("QuoteNo") = qNo
qDest.Range("Customer") = qCustomer
qDest.Range("Contact") = qContact
qDest.Range("QuoteDate") = qDate

' Save the quote
qNewWorkbook.SaveAs Filename:=qFilename, FileFormat:=52
End Sub


I know I can find the path of the Quote details spreadsheet but not sure how to use this to calculate the path to the other files.










share|improve this question
























  • ThisWorkbook.Path will give you the location of the macro workbook, so you can use `..` to go "up" from there and then back down into the folder you need.
    – Tim Williams
    Nov 19 at 23:54















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have an Excel 2016 spreadsheet which contains the details of quotes sent to Customers (1 row for each quote containing cells with the Customer Name, Quote Date, Quote Number etc). I am trying to design a macro to reside in this spreadsheet that will create a new workbook based on an existing Excel template (UK - Quote Sheet.xltm), populate certain fields in the new workbook and save it with a filename name constructed from elements of quote entry. For example, if the spreadsheet contains an entry of 19/11/18 in the Date Cell, ABC in Customer Name field and Q1234 in the Quote Number field, the new file will be given the name "Q1234 - ABC (19.1.18).xlsm" and the Date, Customer Name and Quote Number will be inserted into the appropriate Worksheet cells in the new workbook. I have been able to get the macro to generate the appropriate filename and populate the cells in the new workbook but only by using absolute file paths for both the location of the template and the location of where the new workbook is to be stored. This is fine so long as the folder paths are the same on every PC I need to run the macro but this isn't the case so the macro fails when it's run on a PC where the file paths are different. Whilst the folder structure on every PC will be:



..DocumentsQuotesQuotes Database for the location of Quotes details spreadsheet
..DocumentsTemplatesQuotes for the location of the template file
..DocumentsQuotesAmendable Quotes for location of the new workbook



the location of this folder tree will vary by PC.



The following code works for when the above folder tree resides inside C:VD Operations but I need to modify so that it works no matter where the folder tree resides e.g. it could be under D:WorkVD Operations:



Sub CreateQuote()   'Generate a new quote using UK - Quote Sheet Template

Dim qContact, qNo, qDate, qCustomer As String
Dim qFilename As String
Dim qDay, qMonth, qYear As String
Dim qNewWorkbook As Workbook
Dim qDest As Worksheet

' Get quote details from appropriate entry in Quote Database

qContact = Cells(ActiveCell.Row, 13).Value
qNo = Cells(ActiveCell.Row, 1).Value
qCustomer = Cells(ActiveCell.Row, 12).Value
qDate = Cells(ActiveCell.Row, 3).Value
qDay = Day(qDate)
qMonth = Month(qDate)
qYear = Right(Year(qDate), 2)

Set qNewWorkbook = Workbooks.Open(Filename:="C:VD OperationsDocumentsTemplatesQuotesUK - QUOTE SHEET.xltm", _
Editable:=False)

' Construct quote file name

qFilename = "C:VD OperationsDocumentsQuotesAmendable Quotes" & qNo & " - " & qCustomer & " (" & qDay & "." & qMonth & "." & qYear & ").xlsm"

' Update Cover sheet in quote with values from Quote Database

Set qDest = qNewWorkbook.Sheets("Cover Sheet")
qDest.Range("QuoteNo") = qNo
qDest.Range("Customer") = qCustomer
qDest.Range("Contact") = qContact
qDest.Range("QuoteDate") = qDate

' Save the quote
qNewWorkbook.SaveAs Filename:=qFilename, FileFormat:=52
End Sub


I know I can find the path of the Quote details spreadsheet but not sure how to use this to calculate the path to the other files.










share|improve this question















I have an Excel 2016 spreadsheet which contains the details of quotes sent to Customers (1 row for each quote containing cells with the Customer Name, Quote Date, Quote Number etc). I am trying to design a macro to reside in this spreadsheet that will create a new workbook based on an existing Excel template (UK - Quote Sheet.xltm), populate certain fields in the new workbook and save it with a filename name constructed from elements of quote entry. For example, if the spreadsheet contains an entry of 19/11/18 in the Date Cell, ABC in Customer Name field and Q1234 in the Quote Number field, the new file will be given the name "Q1234 - ABC (19.1.18).xlsm" and the Date, Customer Name and Quote Number will be inserted into the appropriate Worksheet cells in the new workbook. I have been able to get the macro to generate the appropriate filename and populate the cells in the new workbook but only by using absolute file paths for both the location of the template and the location of where the new workbook is to be stored. This is fine so long as the folder paths are the same on every PC I need to run the macro but this isn't the case so the macro fails when it's run on a PC where the file paths are different. Whilst the folder structure on every PC will be:



..DocumentsQuotesQuotes Database for the location of Quotes details spreadsheet
..DocumentsTemplatesQuotes for the location of the template file
..DocumentsQuotesAmendable Quotes for location of the new workbook



the location of this folder tree will vary by PC.



The following code works for when the above folder tree resides inside C:VD Operations but I need to modify so that it works no matter where the folder tree resides e.g. it could be under D:WorkVD Operations:



Sub CreateQuote()   'Generate a new quote using UK - Quote Sheet Template

Dim qContact, qNo, qDate, qCustomer As String
Dim qFilename As String
Dim qDay, qMonth, qYear As String
Dim qNewWorkbook As Workbook
Dim qDest As Worksheet

' Get quote details from appropriate entry in Quote Database

qContact = Cells(ActiveCell.Row, 13).Value
qNo = Cells(ActiveCell.Row, 1).Value
qCustomer = Cells(ActiveCell.Row, 12).Value
qDate = Cells(ActiveCell.Row, 3).Value
qDay = Day(qDate)
qMonth = Month(qDate)
qYear = Right(Year(qDate), 2)

Set qNewWorkbook = Workbooks.Open(Filename:="C:VD OperationsDocumentsTemplatesQuotesUK - QUOTE SHEET.xltm", _
Editable:=False)

' Construct quote file name

qFilename = "C:VD OperationsDocumentsQuotesAmendable Quotes" & qNo & " - " & qCustomer & " (" & qDay & "." & qMonth & "." & qYear & ").xlsm"

' Update Cover sheet in quote with values from Quote Database

Set qDest = qNewWorkbook.Sheets("Cover Sheet")
qDest.Range("QuoteNo") = qNo
qDest.Range("Customer") = qCustomer
qDest.Range("Contact") = qContact
qDest.Range("QuoteDate") = qDate

' Save the quote
qNewWorkbook.SaveAs Filename:=qFilename, FileFormat:=52
End Sub


I know I can find the path of the Quote details spreadsheet but not sure how to use this to calculate the path to the other files.







excel vba excel-vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 7:15









Pᴇʜ

19.8k42650




19.8k42650










asked Nov 19 at 22:54









Jeff H

13




13












  • ThisWorkbook.Path will give you the location of the macro workbook, so you can use `..` to go "up" from there and then back down into the folder you need.
    – Tim Williams
    Nov 19 at 23:54




















  • ThisWorkbook.Path will give you the location of the macro workbook, so you can use `..` to go "up" from there and then back down into the folder you need.
    – Tim Williams
    Nov 19 at 23:54


















ThisWorkbook.Path will give you the location of the macro workbook, so you can use `..` to go "up" from there and then back down into the folder you need.
– Tim Williams
Nov 19 at 23:54






ThisWorkbook.Path will give you the location of the macro workbook, so you can use `..` to go "up" from there and then back down into the folder you need.
– Tim Williams
Nov 19 at 23:54














1 Answer
1






active

oldest

votes

















up vote
0
down vote













For this folder structure (for example):



C:
_Stuff
test
tmp1 '<< macro file is stored here
tmp2 '<< need to load a file "test.xlsx" from here


You can do this:



  ThisWorkbook.Path                        '>> C:_Stufftesttmp1

ThisWorkbook.Path & "..tmp2test.xlsx" '>> C:_Stufftesttmp2test.xlsx


so the .. takes you "up" to C:_Stufftest and from there you can go "down" to tmp2






share|improve this answer





















  • Thanks - I thought there would be a simple solution just didn't know how to navigate through the folder structure!
    – Jeff H
    Nov 20 at 9:44











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383812%2fcreate-a-new-workbook-from-an-excel-template-located-in-a-different-folder%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













For this folder structure (for example):



C:
_Stuff
test
tmp1 '<< macro file is stored here
tmp2 '<< need to load a file "test.xlsx" from here


You can do this:



  ThisWorkbook.Path                        '>> C:_Stufftesttmp1

ThisWorkbook.Path & "..tmp2test.xlsx" '>> C:_Stufftesttmp2test.xlsx


so the .. takes you "up" to C:_Stufftest and from there you can go "down" to tmp2






share|improve this answer





















  • Thanks - I thought there would be a simple solution just didn't know how to navigate through the folder structure!
    – Jeff H
    Nov 20 at 9:44















up vote
0
down vote













For this folder structure (for example):



C:
_Stuff
test
tmp1 '<< macro file is stored here
tmp2 '<< need to load a file "test.xlsx" from here


You can do this:



  ThisWorkbook.Path                        '>> C:_Stufftesttmp1

ThisWorkbook.Path & "..tmp2test.xlsx" '>> C:_Stufftesttmp2test.xlsx


so the .. takes you "up" to C:_Stufftest and from there you can go "down" to tmp2






share|improve this answer





















  • Thanks - I thought there would be a simple solution just didn't know how to navigate through the folder structure!
    – Jeff H
    Nov 20 at 9:44













up vote
0
down vote










up vote
0
down vote









For this folder structure (for example):



C:
_Stuff
test
tmp1 '<< macro file is stored here
tmp2 '<< need to load a file "test.xlsx" from here


You can do this:



  ThisWorkbook.Path                        '>> C:_Stufftesttmp1

ThisWorkbook.Path & "..tmp2test.xlsx" '>> C:_Stufftesttmp2test.xlsx


so the .. takes you "up" to C:_Stufftest and from there you can go "down" to tmp2






share|improve this answer












For this folder structure (for example):



C:
_Stuff
test
tmp1 '<< macro file is stored here
tmp2 '<< need to load a file "test.xlsx" from here


You can do this:



  ThisWorkbook.Path                        '>> C:_Stufftesttmp1

ThisWorkbook.Path & "..tmp2test.xlsx" '>> C:_Stufftesttmp2test.xlsx


so the .. takes you "up" to C:_Stufftest and from there you can go "down" to tmp2







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 0:11









Tim Williams

84.3k96583




84.3k96583












  • Thanks - I thought there would be a simple solution just didn't know how to navigate through the folder structure!
    – Jeff H
    Nov 20 at 9:44


















  • Thanks - I thought there would be a simple solution just didn't know how to navigate through the folder structure!
    – Jeff H
    Nov 20 at 9:44
















Thanks - I thought there would be a simple solution just didn't know how to navigate through the folder structure!
– Jeff H
Nov 20 at 9:44




Thanks - I thought there would be a simple solution just didn't know how to navigate through the folder structure!
– Jeff H
Nov 20 at 9:44


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383812%2fcreate-a-new-workbook-from-an-excel-template-located-in-a-different-folder%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen