Designate a file location based on a value in certain cell
I can change the filename based on a certain cell value. I am trying to change the file location based off the value in a certain cell.
There is a drop down in "B6" and a folder for each option available.
Here is my current code:
Sub SFVTEST()
Dim filepath
filepath = """T:Restricted - DepartmentGLA_ShortcutsReagent and Column _ Validation" & Range("B6") & "" & Range("T26") & ".csv"""
ActiveWorkbook.SaveAs Filename:=filepath,_ FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
Getting a
"Run-time error '1004': Method 'SaveAs' of object'_Workbook' failed"
Line of Code with error
excel vba
add a comment |
I can change the filename based on a certain cell value. I am trying to change the file location based off the value in a certain cell.
There is a drop down in "B6" and a folder for each option available.
Here is my current code:
Sub SFVTEST()
Dim filepath
filepath = """T:Restricted - DepartmentGLA_ShortcutsReagent and Column _ Validation" & Range("B6") & "" & Range("T26") & ".csv"""
ActiveWorkbook.SaveAs Filename:=filepath,_ FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
Getting a
"Run-time error '1004': Method 'SaveAs' of object'_Workbook' failed"
Line of Code with error
excel vba
add a comment |
I can change the filename based on a certain cell value. I am trying to change the file location based off the value in a certain cell.
There is a drop down in "B6" and a folder for each option available.
Here is my current code:
Sub SFVTEST()
Dim filepath
filepath = """T:Restricted - DepartmentGLA_ShortcutsReagent and Column _ Validation" & Range("B6") & "" & Range("T26") & ".csv"""
ActiveWorkbook.SaveAs Filename:=filepath,_ FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
Getting a
"Run-time error '1004': Method 'SaveAs' of object'_Workbook' failed"
Line of Code with error
excel vba
I can change the filename based on a certain cell value. I am trying to change the file location based off the value in a certain cell.
There is a drop down in "B6" and a folder for each option available.
Here is my current code:
Sub SFVTEST()
Dim filepath
filepath = """T:Restricted - DepartmentGLA_ShortcutsReagent and Column _ Validation" & Range("B6") & "" & Range("T26") & ".csv"""
ActiveWorkbook.SaveAs Filename:=filepath,_ FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
Getting a
"Run-time error '1004': Method 'SaveAs' of object'_Workbook' failed"
Line of Code with error
excel vba
excel vba
edited Dec 21 '18 at 14:59
Community♦
11
11
asked Nov 21 '18 at 19:03
Steve LSteve L
106
106
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
It looks like the error is in switching between strings and variables. Strings are surrounded by double quotes, variables aren't. And when switching between the two, use an '&'.
Sub SaveForValidation()
'
' SaveforValidation Macro
'
' File Name Formula Found in T26 in white text.
ActiveWorkbook.SaveAs Filename:= _
"T:Restricted - DepartmentGLA_ShortcutsReagent and Column
Validation" & Range("B6") & "" & Range("T26") & ".csv" _
, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
It might make sense to build the filename in a variable to make testing easier, such as:
Sub SaveForValidation()
Dim filepath
filepath = "T:Restricted - DepartmentGLA_ShortcutsReagent and Column Validation" & Range("B6") & "" & Range("T26") & ".csv"
ActiveWorkbook.SaveAs Filename:=filepath, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
4
This still won't compile though if"T:Restricted - DepartmentGLA_ShortcutsReagent and Column Validation"
is on multiple lines.
– BigBen
Nov 21 '18 at 19:17
Now getting a "Run-time error '424': Object required.
– Steve L
Nov 21 '18 at 20:03
It is flagging the object required on the (AciveWorkbook.SaveAs Filename:=filepath, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False)
– Steve L
Nov 21 '18 at 20:24
In your comment above, you have AciveWorkbook (missing a T in Active). I'm assuming that problem isn't in your code?
– extensionhelp
Nov 21 '18 at 20:25
Well it isn't anymore :D... Now getting Run-time error '1004': Method 'SaveAs' of object'_Workbook' failed
– Steve L
Nov 21 '18 at 20:30
|
show 19 more comments
The above code you have the file name to be saved as .csv but you want it to be a macro-enabled workbook...
Sub SaveForValidation()
Dim filepath As String
filepath = "T:Restricted - DepartmentGLA_ShortcutsReagent and Column Validation" & Range("B6") & "" & Range("T26") & ".xlsm"
ActiveWorkbook.SaveAs Filename:=filepath, FileFormat:=52, CreateBackup:=False
End Sub
I personally use file format # instead of words. 52 = .xlsm
For other types, see below
51 = xlOpenXMLWorkbook (without macro’s in 2007, .xlsx)
52 = xlOpenXMLWorkbookMacroEnabled (with or without macro’s in 2007, .xlsm)
50 = xlExcel12 (Excel Binary Workbook in 2007 with or without macro’s, .xlsb)
56 = xlExcel8 (97-2003 format in Excel 2007, .xls)
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418935%2fdesignate-a-file-location-based-on-a-value-in-certain-cell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It looks like the error is in switching between strings and variables. Strings are surrounded by double quotes, variables aren't. And when switching between the two, use an '&'.
Sub SaveForValidation()
'
' SaveforValidation Macro
'
' File Name Formula Found in T26 in white text.
ActiveWorkbook.SaveAs Filename:= _
"T:Restricted - DepartmentGLA_ShortcutsReagent and Column
Validation" & Range("B6") & "" & Range("T26") & ".csv" _
, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
It might make sense to build the filename in a variable to make testing easier, such as:
Sub SaveForValidation()
Dim filepath
filepath = "T:Restricted - DepartmentGLA_ShortcutsReagent and Column Validation" & Range("B6") & "" & Range("T26") & ".csv"
ActiveWorkbook.SaveAs Filename:=filepath, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
4
This still won't compile though if"T:Restricted - DepartmentGLA_ShortcutsReagent and Column Validation"
is on multiple lines.
– BigBen
Nov 21 '18 at 19:17
Now getting a "Run-time error '424': Object required.
– Steve L
Nov 21 '18 at 20:03
It is flagging the object required on the (AciveWorkbook.SaveAs Filename:=filepath, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False)
– Steve L
Nov 21 '18 at 20:24
In your comment above, you have AciveWorkbook (missing a T in Active). I'm assuming that problem isn't in your code?
– extensionhelp
Nov 21 '18 at 20:25
Well it isn't anymore :D... Now getting Run-time error '1004': Method 'SaveAs' of object'_Workbook' failed
– Steve L
Nov 21 '18 at 20:30
|
show 19 more comments
It looks like the error is in switching between strings and variables. Strings are surrounded by double quotes, variables aren't. And when switching between the two, use an '&'.
Sub SaveForValidation()
'
' SaveforValidation Macro
'
' File Name Formula Found in T26 in white text.
ActiveWorkbook.SaveAs Filename:= _
"T:Restricted - DepartmentGLA_ShortcutsReagent and Column
Validation" & Range("B6") & "" & Range("T26") & ".csv" _
, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
It might make sense to build the filename in a variable to make testing easier, such as:
Sub SaveForValidation()
Dim filepath
filepath = "T:Restricted - DepartmentGLA_ShortcutsReagent and Column Validation" & Range("B6") & "" & Range("T26") & ".csv"
ActiveWorkbook.SaveAs Filename:=filepath, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
4
This still won't compile though if"T:Restricted - DepartmentGLA_ShortcutsReagent and Column Validation"
is on multiple lines.
– BigBen
Nov 21 '18 at 19:17
Now getting a "Run-time error '424': Object required.
– Steve L
Nov 21 '18 at 20:03
It is flagging the object required on the (AciveWorkbook.SaveAs Filename:=filepath, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False)
– Steve L
Nov 21 '18 at 20:24
In your comment above, you have AciveWorkbook (missing a T in Active). I'm assuming that problem isn't in your code?
– extensionhelp
Nov 21 '18 at 20:25
Well it isn't anymore :D... Now getting Run-time error '1004': Method 'SaveAs' of object'_Workbook' failed
– Steve L
Nov 21 '18 at 20:30
|
show 19 more comments
It looks like the error is in switching between strings and variables. Strings are surrounded by double quotes, variables aren't. And when switching between the two, use an '&'.
Sub SaveForValidation()
'
' SaveforValidation Macro
'
' File Name Formula Found in T26 in white text.
ActiveWorkbook.SaveAs Filename:= _
"T:Restricted - DepartmentGLA_ShortcutsReagent and Column
Validation" & Range("B6") & "" & Range("T26") & ".csv" _
, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
It might make sense to build the filename in a variable to make testing easier, such as:
Sub SaveForValidation()
Dim filepath
filepath = "T:Restricted - DepartmentGLA_ShortcutsReagent and Column Validation" & Range("B6") & "" & Range("T26") & ".csv"
ActiveWorkbook.SaveAs Filename:=filepath, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
It looks like the error is in switching between strings and variables. Strings are surrounded by double quotes, variables aren't. And when switching between the two, use an '&'.
Sub SaveForValidation()
'
' SaveforValidation Macro
'
' File Name Formula Found in T26 in white text.
ActiveWorkbook.SaveAs Filename:= _
"T:Restricted - DepartmentGLA_ShortcutsReagent and Column
Validation" & Range("B6") & "" & Range("T26") & ".csv" _
, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
It might make sense to build the filename in a variable to make testing easier, such as:
Sub SaveForValidation()
Dim filepath
filepath = "T:Restricted - DepartmentGLA_ShortcutsReagent and Column Validation" & Range("B6") & "" & Range("T26") & ".csv"
ActiveWorkbook.SaveAs Filename:=filepath, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
edited Nov 21 '18 at 20:28
answered Nov 21 '18 at 19:07
extensionhelpextensionhelp
2437
2437
4
This still won't compile though if"T:Restricted - DepartmentGLA_ShortcutsReagent and Column Validation"
is on multiple lines.
– BigBen
Nov 21 '18 at 19:17
Now getting a "Run-time error '424': Object required.
– Steve L
Nov 21 '18 at 20:03
It is flagging the object required on the (AciveWorkbook.SaveAs Filename:=filepath, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False)
– Steve L
Nov 21 '18 at 20:24
In your comment above, you have AciveWorkbook (missing a T in Active). I'm assuming that problem isn't in your code?
– extensionhelp
Nov 21 '18 at 20:25
Well it isn't anymore :D... Now getting Run-time error '1004': Method 'SaveAs' of object'_Workbook' failed
– Steve L
Nov 21 '18 at 20:30
|
show 19 more comments
4
This still won't compile though if"T:Restricted - DepartmentGLA_ShortcutsReagent and Column Validation"
is on multiple lines.
– BigBen
Nov 21 '18 at 19:17
Now getting a "Run-time error '424': Object required.
– Steve L
Nov 21 '18 at 20:03
It is flagging the object required on the (AciveWorkbook.SaveAs Filename:=filepath, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False)
– Steve L
Nov 21 '18 at 20:24
In your comment above, you have AciveWorkbook (missing a T in Active). I'm assuming that problem isn't in your code?
– extensionhelp
Nov 21 '18 at 20:25
Well it isn't anymore :D... Now getting Run-time error '1004': Method 'SaveAs' of object'_Workbook' failed
– Steve L
Nov 21 '18 at 20:30
4
4
This still won't compile though if
"T:Restricted - DepartmentGLA_ShortcutsReagent and Column Validation"
is on multiple lines.– BigBen
Nov 21 '18 at 19:17
This still won't compile though if
"T:Restricted - DepartmentGLA_ShortcutsReagent and Column Validation"
is on multiple lines.– BigBen
Nov 21 '18 at 19:17
Now getting a "Run-time error '424': Object required.
– Steve L
Nov 21 '18 at 20:03
Now getting a "Run-time error '424': Object required.
– Steve L
Nov 21 '18 at 20:03
It is flagging the object required on the (AciveWorkbook.SaveAs Filename:=filepath, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False)
– Steve L
Nov 21 '18 at 20:24
It is flagging the object required on the (AciveWorkbook.SaveAs Filename:=filepath, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False)
– Steve L
Nov 21 '18 at 20:24
In your comment above, you have AciveWorkbook (missing a T in Active). I'm assuming that problem isn't in your code?
– extensionhelp
Nov 21 '18 at 20:25
In your comment above, you have AciveWorkbook (missing a T in Active). I'm assuming that problem isn't in your code?
– extensionhelp
Nov 21 '18 at 20:25
Well it isn't anymore :D... Now getting Run-time error '1004': Method 'SaveAs' of object'_Workbook' failed
– Steve L
Nov 21 '18 at 20:30
Well it isn't anymore :D... Now getting Run-time error '1004': Method 'SaveAs' of object'_Workbook' failed
– Steve L
Nov 21 '18 at 20:30
|
show 19 more comments
The above code you have the file name to be saved as .csv but you want it to be a macro-enabled workbook...
Sub SaveForValidation()
Dim filepath As String
filepath = "T:Restricted - DepartmentGLA_ShortcutsReagent and Column Validation" & Range("B6") & "" & Range("T26") & ".xlsm"
ActiveWorkbook.SaveAs Filename:=filepath, FileFormat:=52, CreateBackup:=False
End Sub
I personally use file format # instead of words. 52 = .xlsm
For other types, see below
51 = xlOpenXMLWorkbook (without macro’s in 2007, .xlsx)
52 = xlOpenXMLWorkbookMacroEnabled (with or without macro’s in 2007, .xlsm)
50 = xlExcel12 (Excel Binary Workbook in 2007 with or without macro’s, .xlsb)
56 = xlExcel8 (97-2003 format in Excel 2007, .xls)
add a comment |
The above code you have the file name to be saved as .csv but you want it to be a macro-enabled workbook...
Sub SaveForValidation()
Dim filepath As String
filepath = "T:Restricted - DepartmentGLA_ShortcutsReagent and Column Validation" & Range("B6") & "" & Range("T26") & ".xlsm"
ActiveWorkbook.SaveAs Filename:=filepath, FileFormat:=52, CreateBackup:=False
End Sub
I personally use file format # instead of words. 52 = .xlsm
For other types, see below
51 = xlOpenXMLWorkbook (without macro’s in 2007, .xlsx)
52 = xlOpenXMLWorkbookMacroEnabled (with or without macro’s in 2007, .xlsm)
50 = xlExcel12 (Excel Binary Workbook in 2007 with or without macro’s, .xlsb)
56 = xlExcel8 (97-2003 format in Excel 2007, .xls)
add a comment |
The above code you have the file name to be saved as .csv but you want it to be a macro-enabled workbook...
Sub SaveForValidation()
Dim filepath As String
filepath = "T:Restricted - DepartmentGLA_ShortcutsReagent and Column Validation" & Range("B6") & "" & Range("T26") & ".xlsm"
ActiveWorkbook.SaveAs Filename:=filepath, FileFormat:=52, CreateBackup:=False
End Sub
I personally use file format # instead of words. 52 = .xlsm
For other types, see below
51 = xlOpenXMLWorkbook (without macro’s in 2007, .xlsx)
52 = xlOpenXMLWorkbookMacroEnabled (with or without macro’s in 2007, .xlsm)
50 = xlExcel12 (Excel Binary Workbook in 2007 with or without macro’s, .xlsb)
56 = xlExcel8 (97-2003 format in Excel 2007, .xls)
The above code you have the file name to be saved as .csv but you want it to be a macro-enabled workbook...
Sub SaveForValidation()
Dim filepath As String
filepath = "T:Restricted - DepartmentGLA_ShortcutsReagent and Column Validation" & Range("B6") & "" & Range("T26") & ".xlsm"
ActiveWorkbook.SaveAs Filename:=filepath, FileFormat:=52, CreateBackup:=False
End Sub
I personally use file format # instead of words. 52 = .xlsm
For other types, see below
51 = xlOpenXMLWorkbook (without macro’s in 2007, .xlsx)
52 = xlOpenXMLWorkbookMacroEnabled (with or without macro’s in 2007, .xlsm)
50 = xlExcel12 (Excel Binary Workbook in 2007 with or without macro’s, .xlsb)
56 = xlExcel8 (97-2003 format in Excel 2007, .xls)
answered Nov 21 '18 at 21:34
alowflyingpigalowflyingpig
1479
1479
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418935%2fdesignate-a-file-location-based-on-a-value-in-certain-cell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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