Error selecting all active cells in a sheet
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Haven't done any programming in 10 years and are now trying to automate some administrative work. I need to create a CSV file from a certain data set.
Running below code stand alone using this code to select the range:
Set rng = Sheets("HAOD").Range("A2", Cells(Range("a1000000").End(xlUp).row, Range("xfd1").End(xlToLeft).Column))
works fine but if I call the sub from my main program I get error 1004 and the debugger points to my range selection. I tried using this code for the range selection instead, I use this in a VBA vlookup function I found online and it works perfectly there:
rng = .Range("A2:" & tblEnd & .Cells(.Rows.Count, tblEnd).End(xlUp).row).Value
But when using this it first complains about the .-reference and when I add Sheets("HAOD")
I get the 1004 error again. This time even when running the sub stand alone. What am I doing wrong?
Sub CreateCheckITfile()
Call PopulateHAODtab
Call SaveAsCSV
End Sub
Sub SaveAsCSV()
Dim myFile As String
Dim rng As Range
Dim cellValue As Variant
Dim i As Integer
Dim j As Integer
myFile = Application.DefaultFilePath & "HAOD.csv"
Set rng = Sheets("HAOD").Range("A2", Cells(Range("a1000000").End(xlUp).row, Range("xfd1").End(xlToLeft).Column))
Open myFile For Output Lock Write As #1
For i = 1 To rng.Rows.Count
Dim fileRow As String
fileRow = ""
For j = 1 To rng.Columns.Count
cellValue = rng.Cells(i, j).Value
If j = rng.Columns.Count Then
fileRow = fileRow & cellValue
Print #1, fileRow
Else
fileRow = fileRow & cellValue & ","
End If
Next j
Next i
Close #1
End Sub
excel vba excel-vba
add a comment |
Haven't done any programming in 10 years and are now trying to automate some administrative work. I need to create a CSV file from a certain data set.
Running below code stand alone using this code to select the range:
Set rng = Sheets("HAOD").Range("A2", Cells(Range("a1000000").End(xlUp).row, Range("xfd1").End(xlToLeft).Column))
works fine but if I call the sub from my main program I get error 1004 and the debugger points to my range selection. I tried using this code for the range selection instead, I use this in a VBA vlookup function I found online and it works perfectly there:
rng = .Range("A2:" & tblEnd & .Cells(.Rows.Count, tblEnd).End(xlUp).row).Value
But when using this it first complains about the .-reference and when I add Sheets("HAOD")
I get the 1004 error again. This time even when running the sub stand alone. What am I doing wrong?
Sub CreateCheckITfile()
Call PopulateHAODtab
Call SaveAsCSV
End Sub
Sub SaveAsCSV()
Dim myFile As String
Dim rng As Range
Dim cellValue As Variant
Dim i As Integer
Dim j As Integer
myFile = Application.DefaultFilePath & "HAOD.csv"
Set rng = Sheets("HAOD").Range("A2", Cells(Range("a1000000").End(xlUp).row, Range("xfd1").End(xlToLeft).Column))
Open myFile For Output Lock Write As #1
For i = 1 To rng.Rows.Count
Dim fileRow As String
fileRow = ""
For j = 1 To rng.Columns.Count
cellValue = rng.Cells(i, j).Value
If j = rng.Columns.Count Then
fileRow = fileRow & cellValue
Print #1, fileRow
Else
fileRow = fileRow & cellValue & ","
End If
Next j
Next i
Close #1
End Sub
excel vba excel-vba
Use of dots implies a With statementWith Sheets("HAOD")
Your `rng definition doesn't look right, you have three ranges in there?
– SJR
Nov 23 '18 at 13:35
add a comment |
Haven't done any programming in 10 years and are now trying to automate some administrative work. I need to create a CSV file from a certain data set.
Running below code stand alone using this code to select the range:
Set rng = Sheets("HAOD").Range("A2", Cells(Range("a1000000").End(xlUp).row, Range("xfd1").End(xlToLeft).Column))
works fine but if I call the sub from my main program I get error 1004 and the debugger points to my range selection. I tried using this code for the range selection instead, I use this in a VBA vlookup function I found online and it works perfectly there:
rng = .Range("A2:" & tblEnd & .Cells(.Rows.Count, tblEnd).End(xlUp).row).Value
But when using this it first complains about the .-reference and when I add Sheets("HAOD")
I get the 1004 error again. This time even when running the sub stand alone. What am I doing wrong?
Sub CreateCheckITfile()
Call PopulateHAODtab
Call SaveAsCSV
End Sub
Sub SaveAsCSV()
Dim myFile As String
Dim rng As Range
Dim cellValue As Variant
Dim i As Integer
Dim j As Integer
myFile = Application.DefaultFilePath & "HAOD.csv"
Set rng = Sheets("HAOD").Range("A2", Cells(Range("a1000000").End(xlUp).row, Range("xfd1").End(xlToLeft).Column))
Open myFile For Output Lock Write As #1
For i = 1 To rng.Rows.Count
Dim fileRow As String
fileRow = ""
For j = 1 To rng.Columns.Count
cellValue = rng.Cells(i, j).Value
If j = rng.Columns.Count Then
fileRow = fileRow & cellValue
Print #1, fileRow
Else
fileRow = fileRow & cellValue & ","
End If
Next j
Next i
Close #1
End Sub
excel vba excel-vba
Haven't done any programming in 10 years and are now trying to automate some administrative work. I need to create a CSV file from a certain data set.
Running below code stand alone using this code to select the range:
Set rng = Sheets("HAOD").Range("A2", Cells(Range("a1000000").End(xlUp).row, Range("xfd1").End(xlToLeft).Column))
works fine but if I call the sub from my main program I get error 1004 and the debugger points to my range selection. I tried using this code for the range selection instead, I use this in a VBA vlookup function I found online and it works perfectly there:
rng = .Range("A2:" & tblEnd & .Cells(.Rows.Count, tblEnd).End(xlUp).row).Value
But when using this it first complains about the .-reference and when I add Sheets("HAOD")
I get the 1004 error again. This time even when running the sub stand alone. What am I doing wrong?
Sub CreateCheckITfile()
Call PopulateHAODtab
Call SaveAsCSV
End Sub
Sub SaveAsCSV()
Dim myFile As String
Dim rng As Range
Dim cellValue As Variant
Dim i As Integer
Dim j As Integer
myFile = Application.DefaultFilePath & "HAOD.csv"
Set rng = Sheets("HAOD").Range("A2", Cells(Range("a1000000").End(xlUp).row, Range("xfd1").End(xlToLeft).Column))
Open myFile For Output Lock Write As #1
For i = 1 To rng.Rows.Count
Dim fileRow As String
fileRow = ""
For j = 1 To rng.Columns.Count
cellValue = rng.Cells(i, j).Value
If j = rng.Columns.Count Then
fileRow = fileRow & cellValue
Print #1, fileRow
Else
fileRow = fileRow & cellValue & ","
End If
Next j
Next i
Close #1
End Sub
excel vba excel-vba
excel vba excel-vba
asked Nov 23 '18 at 13:31
Niklas ToralNiklas Toral
1
1
Use of dots implies a With statementWith Sheets("HAOD")
Your `rng definition doesn't look right, you have three ranges in there?
– SJR
Nov 23 '18 at 13:35
add a comment |
Use of dots implies a With statementWith Sheets("HAOD")
Your `rng definition doesn't look right, you have three ranges in there?
– SJR
Nov 23 '18 at 13:35
Use of dots implies a With statement
With Sheets("HAOD")
Your `rng definition doesn't look right, you have three ranges in there?– SJR
Nov 23 '18 at 13:35
Use of dots implies a With statement
With Sheets("HAOD")
Your `rng definition doesn't look right, you have three ranges in there?– SJR
Nov 23 '18 at 13:35
add a comment |
2 Answers
2
active
oldest
votes
All references need to specify the sheet, otherwise they default to the active sheet
Set rng = Sheets("HAOD").Range("A2", Sheets("HAOD").Cells( Sheets("HAOD").Range("a1000000").End(xlUp).row, Sheets("HAOD").Range("xfd1").End(xlToLeft).Column))
Thanks that did it but when I try to do the same thing to the much prettierSet rng = Sheets("HAOD").Range("A2:" & tblEnd & Sheets("HAOD").Cells(Sheets("HAOD").Rows.Count, tblEnd).End(xlUp).row).Value
I still get the 1004 error
– Niklas Toral
Nov 23 '18 at 13:42
What's the actual value oftblEnd
?
– Rory
Nov 23 '18 at 13:46
Damn I missed that it should be "L". Adding that instead gives error 424: Object required error. I'm just trying to find a way to select all active cells in the sheet. Must be some smart way to do it that doesn't require giving the last column etc
– Niklas Toral
Nov 23 '18 at 14:02
Well you could use Set rng = sheets("HADD").usedrange but that is everything from A1 to where ever control -End takes you so you might want to check that before using it
– Harassed Dad
Nov 23 '18 at 14:16
add a comment |
Personally, I prefer to do it in little steps that way it lets me step through and see if everything is working as intended. This is kind of similar to the second version of what you tried, but I thought it would be more clear if the address
property was used instead of tblend
. Using a With
block also helps keep it looking tidier.
Try replacing your set
statement with this:
With Sheets("HAOD")
lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
lastcol = .Cells.Find("*", searchorder:=xlByColumns, searchdirection:=xlPrevious).Column
Set Rng = .Range("A2:" & .Cells(lastrow, lastcol).Address)
End With
Thanks Broato! Very neat and it works like a charm. Thanks to everyone supporting! Appreciate it greatly.
– Niklas Toral
Nov 26 '18 at 7:24
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%2f53447659%2ferror-selecting-all-active-cells-in-a-sheet%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
All references need to specify the sheet, otherwise they default to the active sheet
Set rng = Sheets("HAOD").Range("A2", Sheets("HAOD").Cells( Sheets("HAOD").Range("a1000000").End(xlUp).row, Sheets("HAOD").Range("xfd1").End(xlToLeft).Column))
Thanks that did it but when I try to do the same thing to the much prettierSet rng = Sheets("HAOD").Range("A2:" & tblEnd & Sheets("HAOD").Cells(Sheets("HAOD").Rows.Count, tblEnd).End(xlUp).row).Value
I still get the 1004 error
– Niklas Toral
Nov 23 '18 at 13:42
What's the actual value oftblEnd
?
– Rory
Nov 23 '18 at 13:46
Damn I missed that it should be "L". Adding that instead gives error 424: Object required error. I'm just trying to find a way to select all active cells in the sheet. Must be some smart way to do it that doesn't require giving the last column etc
– Niklas Toral
Nov 23 '18 at 14:02
Well you could use Set rng = sheets("HADD").usedrange but that is everything from A1 to where ever control -End takes you so you might want to check that before using it
– Harassed Dad
Nov 23 '18 at 14:16
add a comment |
All references need to specify the sheet, otherwise they default to the active sheet
Set rng = Sheets("HAOD").Range("A2", Sheets("HAOD").Cells( Sheets("HAOD").Range("a1000000").End(xlUp).row, Sheets("HAOD").Range("xfd1").End(xlToLeft).Column))
Thanks that did it but when I try to do the same thing to the much prettierSet rng = Sheets("HAOD").Range("A2:" & tblEnd & Sheets("HAOD").Cells(Sheets("HAOD").Rows.Count, tblEnd).End(xlUp).row).Value
I still get the 1004 error
– Niklas Toral
Nov 23 '18 at 13:42
What's the actual value oftblEnd
?
– Rory
Nov 23 '18 at 13:46
Damn I missed that it should be "L". Adding that instead gives error 424: Object required error. I'm just trying to find a way to select all active cells in the sheet. Must be some smart way to do it that doesn't require giving the last column etc
– Niklas Toral
Nov 23 '18 at 14:02
Well you could use Set rng = sheets("HADD").usedrange but that is everything from A1 to where ever control -End takes you so you might want to check that before using it
– Harassed Dad
Nov 23 '18 at 14:16
add a comment |
All references need to specify the sheet, otherwise they default to the active sheet
Set rng = Sheets("HAOD").Range("A2", Sheets("HAOD").Cells( Sheets("HAOD").Range("a1000000").End(xlUp).row, Sheets("HAOD").Range("xfd1").End(xlToLeft).Column))
All references need to specify the sheet, otherwise they default to the active sheet
Set rng = Sheets("HAOD").Range("A2", Sheets("HAOD").Cells( Sheets("HAOD").Range("a1000000").End(xlUp).row, Sheets("HAOD").Range("xfd1").End(xlToLeft).Column))
answered Nov 23 '18 at 13:34
Harassed DadHarassed Dad
3,4641612
3,4641612
Thanks that did it but when I try to do the same thing to the much prettierSet rng = Sheets("HAOD").Range("A2:" & tblEnd & Sheets("HAOD").Cells(Sheets("HAOD").Rows.Count, tblEnd).End(xlUp).row).Value
I still get the 1004 error
– Niklas Toral
Nov 23 '18 at 13:42
What's the actual value oftblEnd
?
– Rory
Nov 23 '18 at 13:46
Damn I missed that it should be "L". Adding that instead gives error 424: Object required error. I'm just trying to find a way to select all active cells in the sheet. Must be some smart way to do it that doesn't require giving the last column etc
– Niklas Toral
Nov 23 '18 at 14:02
Well you could use Set rng = sheets("HADD").usedrange but that is everything from A1 to where ever control -End takes you so you might want to check that before using it
– Harassed Dad
Nov 23 '18 at 14:16
add a comment |
Thanks that did it but when I try to do the same thing to the much prettierSet rng = Sheets("HAOD").Range("A2:" & tblEnd & Sheets("HAOD").Cells(Sheets("HAOD").Rows.Count, tblEnd).End(xlUp).row).Value
I still get the 1004 error
– Niklas Toral
Nov 23 '18 at 13:42
What's the actual value oftblEnd
?
– Rory
Nov 23 '18 at 13:46
Damn I missed that it should be "L". Adding that instead gives error 424: Object required error. I'm just trying to find a way to select all active cells in the sheet. Must be some smart way to do it that doesn't require giving the last column etc
– Niklas Toral
Nov 23 '18 at 14:02
Well you could use Set rng = sheets("HADD").usedrange but that is everything from A1 to where ever control -End takes you so you might want to check that before using it
– Harassed Dad
Nov 23 '18 at 14:16
Thanks that did it but when I try to do the same thing to the much prettier
Set rng = Sheets("HAOD").Range("A2:" & tblEnd & Sheets("HAOD").Cells(Sheets("HAOD").Rows.Count, tblEnd).End(xlUp).row).Value
I still get the 1004 error– Niklas Toral
Nov 23 '18 at 13:42
Thanks that did it but when I try to do the same thing to the much prettier
Set rng = Sheets("HAOD").Range("A2:" & tblEnd & Sheets("HAOD").Cells(Sheets("HAOD").Rows.Count, tblEnd).End(xlUp).row).Value
I still get the 1004 error– Niklas Toral
Nov 23 '18 at 13:42
What's the actual value of
tblEnd
?– Rory
Nov 23 '18 at 13:46
What's the actual value of
tblEnd
?– Rory
Nov 23 '18 at 13:46
Damn I missed that it should be "L". Adding that instead gives error 424: Object required error. I'm just trying to find a way to select all active cells in the sheet. Must be some smart way to do it that doesn't require giving the last column etc
– Niklas Toral
Nov 23 '18 at 14:02
Damn I missed that it should be "L". Adding that instead gives error 424: Object required error. I'm just trying to find a way to select all active cells in the sheet. Must be some smart way to do it that doesn't require giving the last column etc
– Niklas Toral
Nov 23 '18 at 14:02
Well you could use Set rng = sheets("HADD").usedrange but that is everything from A1 to where ever control -End takes you so you might want to check that before using it
– Harassed Dad
Nov 23 '18 at 14:16
Well you could use Set rng = sheets("HADD").usedrange but that is everything from A1 to where ever control -End takes you so you might want to check that before using it
– Harassed Dad
Nov 23 '18 at 14:16
add a comment |
Personally, I prefer to do it in little steps that way it lets me step through and see if everything is working as intended. This is kind of similar to the second version of what you tried, but I thought it would be more clear if the address
property was used instead of tblend
. Using a With
block also helps keep it looking tidier.
Try replacing your set
statement with this:
With Sheets("HAOD")
lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
lastcol = .Cells.Find("*", searchorder:=xlByColumns, searchdirection:=xlPrevious).Column
Set Rng = .Range("A2:" & .Cells(lastrow, lastcol).Address)
End With
Thanks Broato! Very neat and it works like a charm. Thanks to everyone supporting! Appreciate it greatly.
– Niklas Toral
Nov 26 '18 at 7:24
add a comment |
Personally, I prefer to do it in little steps that way it lets me step through and see if everything is working as intended. This is kind of similar to the second version of what you tried, but I thought it would be more clear if the address
property was used instead of tblend
. Using a With
block also helps keep it looking tidier.
Try replacing your set
statement with this:
With Sheets("HAOD")
lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
lastcol = .Cells.Find("*", searchorder:=xlByColumns, searchdirection:=xlPrevious).Column
Set Rng = .Range("A2:" & .Cells(lastrow, lastcol).Address)
End With
Thanks Broato! Very neat and it works like a charm. Thanks to everyone supporting! Appreciate it greatly.
– Niklas Toral
Nov 26 '18 at 7:24
add a comment |
Personally, I prefer to do it in little steps that way it lets me step through and see if everything is working as intended. This is kind of similar to the second version of what you tried, but I thought it would be more clear if the address
property was used instead of tblend
. Using a With
block also helps keep it looking tidier.
Try replacing your set
statement with this:
With Sheets("HAOD")
lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
lastcol = .Cells.Find("*", searchorder:=xlByColumns, searchdirection:=xlPrevious).Column
Set Rng = .Range("A2:" & .Cells(lastrow, lastcol).Address)
End With
Personally, I prefer to do it in little steps that way it lets me step through and see if everything is working as intended. This is kind of similar to the second version of what you tried, but I thought it would be more clear if the address
property was used instead of tblend
. Using a With
block also helps keep it looking tidier.
Try replacing your set
statement with this:
With Sheets("HAOD")
lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
lastcol = .Cells.Find("*", searchorder:=xlByColumns, searchdirection:=xlPrevious).Column
Set Rng = .Range("A2:" & .Cells(lastrow, lastcol).Address)
End With
answered Nov 23 '18 at 16:13
Jchang43Jchang43
800213
800213
Thanks Broato! Very neat and it works like a charm. Thanks to everyone supporting! Appreciate it greatly.
– Niklas Toral
Nov 26 '18 at 7:24
add a comment |
Thanks Broato! Very neat and it works like a charm. Thanks to everyone supporting! Appreciate it greatly.
– Niklas Toral
Nov 26 '18 at 7:24
Thanks Broato! Very neat and it works like a charm. Thanks to everyone supporting! Appreciate it greatly.
– Niklas Toral
Nov 26 '18 at 7:24
Thanks Broato! Very neat and it works like a charm. Thanks to everyone supporting! Appreciate it greatly.
– Niklas Toral
Nov 26 '18 at 7:24
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%2f53447659%2ferror-selecting-all-active-cells-in-a-sheet%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
Use of dots implies a With statement
With Sheets("HAOD")
Your `rng definition doesn't look right, you have three ranges in there?– SJR
Nov 23 '18 at 13:35