In VBA, how do I create a range out of the cells above a table?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Update: I solved this with the following snippet of code. Thank you for everyone's help. tableList is a Range object made from a table in the workbook listing details of the various tables in the workbook. The Range object does not require specifying the sheet on which a table is located.
For rowNumber = 1 To tableList.Rows.Count
If tableList.Item(rowNumber, actionColumn).Value = actionType Then
tableName = tableList.Item(rowNumber, nameColumn).Value
Set activeTable = Range(tableName)
With activeTable
.Rows(.Rows.Count + 1).Value = activeTable.Rows(-1).Value
End With
End If
Next
I have formulas in the row right above the header row of my table/range. I want to copy the formula values and paste them to the last (new) row of the table/range. I know how to make the code work except for the reference to the row with the formulas.
This is code that I've tried to use to reference the cell two rows above the first cell in my table (that I set/defined as the range).
Set rangeTopLeft = rangeActive.Cells(1, 1).Offset(-2)
I'm getting a 1004 error when I try to run the VBA code.
In the code I then go on to create a range by resizing rangeTopLeft
and perform the other steps to copy and past values from the formulas to the new row.
I DON'T get an error if I use .Offset(-1)
, but that only gets me to the first column cell in my header row. I'm guessing, there must be something where offset cannot go beyond the boundaries of the range.
Assuming that is so (or something else), How do I get around this?
Thank you in advance.
Ok, please avoid the 'newbieness'. I'll clean it up after I can just get it working.
'
'Paste Last Week's Formula Values to New Rows
Sub PasteValues()
Dim rangeList As Range
Dim rangeActive As Range
Dim rangeToCopy As Range
Dim lastRow As Range
Dim rangeName As String
Dim rowNumber As Integer
Dim dataBeginColumn As Integer
Dim actionColumn As Integer
Dim actionType As String
Dim nameColumn As Integer
Dim dataColumnFirst As Integer
Dim dataColumnLast As Integer
Dim response1 As VbMsgBoxResult
Dim response2 As VbMsgBoxResult
Set rangeList = Range("tTablesDetails").ListObject.DataBodyRange
nameColumn = 1
actionColumn = 7
actionType = "Append"
'Requires user to click "Yes" twice
before pasting values
response1 = MsgBox("Do you want to past last week's formula values to tables of this Workbook?", vbYesNo + vbCritical)
If response1 = vbNo Then Exit Sub
response2 = MsgBox("Are you sure? This action cannot be undone.", vbYesNo + vbCritical)
If response2 = vbNo Then Exit Sub
For rowNumber = 1 To rangeList.Rows.Count
If rangeList.ListObject.DataBodyRange(rowNumber, actionColumn).Value = actionType Then
'get table name from row whose action column equals actiontype
rangeName = rangeList.ListObject.DataBodyRange(rowNumber, nameColumn).Text
Set rangeActive = Range(rangeName)
Set rangeTopLeft = rangeActive.Cells(1, 1).Offset(-2)
Set rangeToCopy = rangeTopLeft.Resize(1, rangeActive.Columns.Count)
Set lastRow = rangeActive.Offset(rangeActive.Rows.Count).Resize(1, rangeActive.Columns.Count)
lastRow = rangeToCopy.Value
End If
Next
MsgBox ("Finished Copying Values to New Rows")
End Sub'
excel vba excel-vba
add a comment |
Update: I solved this with the following snippet of code. Thank you for everyone's help. tableList is a Range object made from a table in the workbook listing details of the various tables in the workbook. The Range object does not require specifying the sheet on which a table is located.
For rowNumber = 1 To tableList.Rows.Count
If tableList.Item(rowNumber, actionColumn).Value = actionType Then
tableName = tableList.Item(rowNumber, nameColumn).Value
Set activeTable = Range(tableName)
With activeTable
.Rows(.Rows.Count + 1).Value = activeTable.Rows(-1).Value
End With
End If
Next
I have formulas in the row right above the header row of my table/range. I want to copy the formula values and paste them to the last (new) row of the table/range. I know how to make the code work except for the reference to the row with the formulas.
This is code that I've tried to use to reference the cell two rows above the first cell in my table (that I set/defined as the range).
Set rangeTopLeft = rangeActive.Cells(1, 1).Offset(-2)
I'm getting a 1004 error when I try to run the VBA code.
In the code I then go on to create a range by resizing rangeTopLeft
and perform the other steps to copy and past values from the formulas to the new row.
I DON'T get an error if I use .Offset(-1)
, but that only gets me to the first column cell in my header row. I'm guessing, there must be something where offset cannot go beyond the boundaries of the range.
Assuming that is so (or something else), How do I get around this?
Thank you in advance.
Ok, please avoid the 'newbieness'. I'll clean it up after I can just get it working.
'
'Paste Last Week's Formula Values to New Rows
Sub PasteValues()
Dim rangeList As Range
Dim rangeActive As Range
Dim rangeToCopy As Range
Dim lastRow As Range
Dim rangeName As String
Dim rowNumber As Integer
Dim dataBeginColumn As Integer
Dim actionColumn As Integer
Dim actionType As String
Dim nameColumn As Integer
Dim dataColumnFirst As Integer
Dim dataColumnLast As Integer
Dim response1 As VbMsgBoxResult
Dim response2 As VbMsgBoxResult
Set rangeList = Range("tTablesDetails").ListObject.DataBodyRange
nameColumn = 1
actionColumn = 7
actionType = "Append"
'Requires user to click "Yes" twice
before pasting values
response1 = MsgBox("Do you want to past last week's formula values to tables of this Workbook?", vbYesNo + vbCritical)
If response1 = vbNo Then Exit Sub
response2 = MsgBox("Are you sure? This action cannot be undone.", vbYesNo + vbCritical)
If response2 = vbNo Then Exit Sub
For rowNumber = 1 To rangeList.Rows.Count
If rangeList.ListObject.DataBodyRange(rowNumber, actionColumn).Value = actionType Then
'get table name from row whose action column equals actiontype
rangeName = rangeList.ListObject.DataBodyRange(rowNumber, nameColumn).Text
Set rangeActive = Range(rangeName)
Set rangeTopLeft = rangeActive.Cells(1, 1).Offset(-2)
Set rangeToCopy = rangeTopLeft.Resize(1, rangeActive.Columns.Count)
Set lastRow = rangeActive.Offset(rangeActive.Rows.Count).Resize(1, rangeActive.Columns.Count)
lastRow = rangeToCopy.Value
End If
Next
MsgBox ("Finished Copying Values to New Rows")
End Sub'
excel vba excel-vba
How israngeActive
assigned, and where is the table?
– Rory
Nov 16 '18 at 16:48
I don't think we have the full picture, but I'd guess that you would wantrangeActive.Rows(1).Offset(-2)
... but we still need to know aboutrangeActive
.
– Andy G
Nov 16 '18 at 16:51
see code above that I added. the rangeActive assignment changes as the code loops the names of tables on various sheets of the workbook.
– Nik
Nov 16 '18 at 20:37
add a comment |
Update: I solved this with the following snippet of code. Thank you for everyone's help. tableList is a Range object made from a table in the workbook listing details of the various tables in the workbook. The Range object does not require specifying the sheet on which a table is located.
For rowNumber = 1 To tableList.Rows.Count
If tableList.Item(rowNumber, actionColumn).Value = actionType Then
tableName = tableList.Item(rowNumber, nameColumn).Value
Set activeTable = Range(tableName)
With activeTable
.Rows(.Rows.Count + 1).Value = activeTable.Rows(-1).Value
End With
End If
Next
I have formulas in the row right above the header row of my table/range. I want to copy the formula values and paste them to the last (new) row of the table/range. I know how to make the code work except for the reference to the row with the formulas.
This is code that I've tried to use to reference the cell two rows above the first cell in my table (that I set/defined as the range).
Set rangeTopLeft = rangeActive.Cells(1, 1).Offset(-2)
I'm getting a 1004 error when I try to run the VBA code.
In the code I then go on to create a range by resizing rangeTopLeft
and perform the other steps to copy and past values from the formulas to the new row.
I DON'T get an error if I use .Offset(-1)
, but that only gets me to the first column cell in my header row. I'm guessing, there must be something where offset cannot go beyond the boundaries of the range.
Assuming that is so (or something else), How do I get around this?
Thank you in advance.
Ok, please avoid the 'newbieness'. I'll clean it up after I can just get it working.
'
'Paste Last Week's Formula Values to New Rows
Sub PasteValues()
Dim rangeList As Range
Dim rangeActive As Range
Dim rangeToCopy As Range
Dim lastRow As Range
Dim rangeName As String
Dim rowNumber As Integer
Dim dataBeginColumn As Integer
Dim actionColumn As Integer
Dim actionType As String
Dim nameColumn As Integer
Dim dataColumnFirst As Integer
Dim dataColumnLast As Integer
Dim response1 As VbMsgBoxResult
Dim response2 As VbMsgBoxResult
Set rangeList = Range("tTablesDetails").ListObject.DataBodyRange
nameColumn = 1
actionColumn = 7
actionType = "Append"
'Requires user to click "Yes" twice
before pasting values
response1 = MsgBox("Do you want to past last week's formula values to tables of this Workbook?", vbYesNo + vbCritical)
If response1 = vbNo Then Exit Sub
response2 = MsgBox("Are you sure? This action cannot be undone.", vbYesNo + vbCritical)
If response2 = vbNo Then Exit Sub
For rowNumber = 1 To rangeList.Rows.Count
If rangeList.ListObject.DataBodyRange(rowNumber, actionColumn).Value = actionType Then
'get table name from row whose action column equals actiontype
rangeName = rangeList.ListObject.DataBodyRange(rowNumber, nameColumn).Text
Set rangeActive = Range(rangeName)
Set rangeTopLeft = rangeActive.Cells(1, 1).Offset(-2)
Set rangeToCopy = rangeTopLeft.Resize(1, rangeActive.Columns.Count)
Set lastRow = rangeActive.Offset(rangeActive.Rows.Count).Resize(1, rangeActive.Columns.Count)
lastRow = rangeToCopy.Value
End If
Next
MsgBox ("Finished Copying Values to New Rows")
End Sub'
excel vba excel-vba
Update: I solved this with the following snippet of code. Thank you for everyone's help. tableList is a Range object made from a table in the workbook listing details of the various tables in the workbook. The Range object does not require specifying the sheet on which a table is located.
For rowNumber = 1 To tableList.Rows.Count
If tableList.Item(rowNumber, actionColumn).Value = actionType Then
tableName = tableList.Item(rowNumber, nameColumn).Value
Set activeTable = Range(tableName)
With activeTable
.Rows(.Rows.Count + 1).Value = activeTable.Rows(-1).Value
End With
End If
Next
I have formulas in the row right above the header row of my table/range. I want to copy the formula values and paste them to the last (new) row of the table/range. I know how to make the code work except for the reference to the row with the formulas.
This is code that I've tried to use to reference the cell two rows above the first cell in my table (that I set/defined as the range).
Set rangeTopLeft = rangeActive.Cells(1, 1).Offset(-2)
I'm getting a 1004 error when I try to run the VBA code.
In the code I then go on to create a range by resizing rangeTopLeft
and perform the other steps to copy and past values from the formulas to the new row.
I DON'T get an error if I use .Offset(-1)
, but that only gets me to the first column cell in my header row. I'm guessing, there must be something where offset cannot go beyond the boundaries of the range.
Assuming that is so (or something else), How do I get around this?
Thank you in advance.
Ok, please avoid the 'newbieness'. I'll clean it up after I can just get it working.
'
'Paste Last Week's Formula Values to New Rows
Sub PasteValues()
Dim rangeList As Range
Dim rangeActive As Range
Dim rangeToCopy As Range
Dim lastRow As Range
Dim rangeName As String
Dim rowNumber As Integer
Dim dataBeginColumn As Integer
Dim actionColumn As Integer
Dim actionType As String
Dim nameColumn As Integer
Dim dataColumnFirst As Integer
Dim dataColumnLast As Integer
Dim response1 As VbMsgBoxResult
Dim response2 As VbMsgBoxResult
Set rangeList = Range("tTablesDetails").ListObject.DataBodyRange
nameColumn = 1
actionColumn = 7
actionType = "Append"
'Requires user to click "Yes" twice
before pasting values
response1 = MsgBox("Do you want to past last week's formula values to tables of this Workbook?", vbYesNo + vbCritical)
If response1 = vbNo Then Exit Sub
response2 = MsgBox("Are you sure? This action cannot be undone.", vbYesNo + vbCritical)
If response2 = vbNo Then Exit Sub
For rowNumber = 1 To rangeList.Rows.Count
If rangeList.ListObject.DataBodyRange(rowNumber, actionColumn).Value = actionType Then
'get table name from row whose action column equals actiontype
rangeName = rangeList.ListObject.DataBodyRange(rowNumber, nameColumn).Text
Set rangeActive = Range(rangeName)
Set rangeTopLeft = rangeActive.Cells(1, 1).Offset(-2)
Set rangeToCopy = rangeTopLeft.Resize(1, rangeActive.Columns.Count)
Set lastRow = rangeActive.Offset(rangeActive.Rows.Count).Resize(1, rangeActive.Columns.Count)
lastRow = rangeToCopy.Value
End If
Next
MsgBox ("Finished Copying Values to New Rows")
End Sub'
excel vba excel-vba
excel vba excel-vba
edited Nov 19 '18 at 6:29
Ferdinando
5891518
5891518
asked Nov 16 '18 at 16:43
NikNik
62
62
How israngeActive
assigned, and where is the table?
– Rory
Nov 16 '18 at 16:48
I don't think we have the full picture, but I'd guess that you would wantrangeActive.Rows(1).Offset(-2)
... but we still need to know aboutrangeActive
.
– Andy G
Nov 16 '18 at 16:51
see code above that I added. the rangeActive assignment changes as the code loops the names of tables on various sheets of the workbook.
– Nik
Nov 16 '18 at 20:37
add a comment |
How israngeActive
assigned, and where is the table?
– Rory
Nov 16 '18 at 16:48
I don't think we have the full picture, but I'd guess that you would wantrangeActive.Rows(1).Offset(-2)
... but we still need to know aboutrangeActive
.
– Andy G
Nov 16 '18 at 16:51
see code above that I added. the rangeActive assignment changes as the code loops the names of tables on various sheets of the workbook.
– Nik
Nov 16 '18 at 20:37
How is
rangeActive
assigned, and where is the table?– Rory
Nov 16 '18 at 16:48
How is
rangeActive
assigned, and where is the table?– Rory
Nov 16 '18 at 16:48
I don't think we have the full picture, but I'd guess that you would want
rangeActive.Rows(1).Offset(-2)
... but we still need to know about rangeActive
.– Andy G
Nov 16 '18 at 16:51
I don't think we have the full picture, but I'd guess that you would want
rangeActive.Rows(1).Offset(-2)
... but we still need to know about rangeActive
.– Andy G
Nov 16 '18 at 16:51
see code above that I added. the rangeActive assignment changes as the code loops the names of tables on various sheets of the workbook.
– Nik
Nov 16 '18 at 20:37
see code above that I added. the rangeActive assignment changes as the code loops the names of tables on various sheets of the workbook.
– Nik
Nov 16 '18 at 20:37
add a comment |
2 Answers
2
active
oldest
votes
Use the built-in properties of a ListObject
, specifically the HeaderRowRange
.
And no need to resize and then copy/paste values, you can simply do a value transfer from the row above the HeaderRowRange
to a newly added ListRow
.
Perhaps something like this:
Sub Test()
Dim myTable As ListObject
Set myTable = Sheet1.ListObjects("Table1")
Dim formulaRange As Range
Set formulaRange = myTable.HeaderRowRange.Offset(-1)
myTable.ListRows.Add.Range.Value = formulaRange.Value
End Sub
Code added above
– Nik
Nov 16 '18 at 20:33
add a comment |
Sub SO()
Dim lst As ListObject
Set lst = ActiveSheet.ListObjects("Table1")
With lst.DataBodyRange
.Rows(.Rows.Count + 1).Value = .Rows(1).Offset(-2).Value
End With
End Sub
1
@BigBen I have corrected code once again - now everything works as expected: values from formulas are written to the end of table. 😉
– JohnyL
Nov 16 '18 at 19:37
thank you. will this work if I am looping through different table names therefor working with tables on different sheets as I loop? ...I have a number of tables in the workbook on different sheets that I'm repeating this process on.
– Nik
Nov 16 '18 at 20:43
@Nik Yes, this will work on any table as far as formulas are always above table's header. You just need to adapt my code to use correct sheet (and table name, if it's required).
– JohnyL
Nov 16 '18 at 20:47
So, I will need to define the sheet for each table as I loop through them? I can add a column to my "table name lookup list" that contains the sheet name it's on. Was hoping to get around this. I thought that using Range() you didn't have to tell VBA where the table is located....
– Nik
Nov 16 '18 at 20:55
@Nik I will need to define the sheet for each table as I loop through them? Yes, since everyListObject
belongs toWorksheet
. I can add a column to my... You can get list of tables' names from anything you want (including hard-coding). I thought that using Range() you didn't have to tell VBA where the table is located And do you imagine how to do it?ListObjects
is for very this thing - locating tables. You could, of course, store locations of tables, but it's not worth it - useListObjects
instead.
– JohnyL
Nov 17 '18 at 7:31
|
show 1 more 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%2f53342095%2fin-vba-how-do-i-create-a-range-out-of-the-cells-above-a-table%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
Use the built-in properties of a ListObject
, specifically the HeaderRowRange
.
And no need to resize and then copy/paste values, you can simply do a value transfer from the row above the HeaderRowRange
to a newly added ListRow
.
Perhaps something like this:
Sub Test()
Dim myTable As ListObject
Set myTable = Sheet1.ListObjects("Table1")
Dim formulaRange As Range
Set formulaRange = myTable.HeaderRowRange.Offset(-1)
myTable.ListRows.Add.Range.Value = formulaRange.Value
End Sub
Code added above
– Nik
Nov 16 '18 at 20:33
add a comment |
Use the built-in properties of a ListObject
, specifically the HeaderRowRange
.
And no need to resize and then copy/paste values, you can simply do a value transfer from the row above the HeaderRowRange
to a newly added ListRow
.
Perhaps something like this:
Sub Test()
Dim myTable As ListObject
Set myTable = Sheet1.ListObjects("Table1")
Dim formulaRange As Range
Set formulaRange = myTable.HeaderRowRange.Offset(-1)
myTable.ListRows.Add.Range.Value = formulaRange.Value
End Sub
Code added above
– Nik
Nov 16 '18 at 20:33
add a comment |
Use the built-in properties of a ListObject
, specifically the HeaderRowRange
.
And no need to resize and then copy/paste values, you can simply do a value transfer from the row above the HeaderRowRange
to a newly added ListRow
.
Perhaps something like this:
Sub Test()
Dim myTable As ListObject
Set myTable = Sheet1.ListObjects("Table1")
Dim formulaRange As Range
Set formulaRange = myTable.HeaderRowRange.Offset(-1)
myTable.ListRows.Add.Range.Value = formulaRange.Value
End Sub
Use the built-in properties of a ListObject
, specifically the HeaderRowRange
.
And no need to resize and then copy/paste values, you can simply do a value transfer from the row above the HeaderRowRange
to a newly added ListRow
.
Perhaps something like this:
Sub Test()
Dim myTable As ListObject
Set myTable = Sheet1.ListObjects("Table1")
Dim formulaRange As Range
Set formulaRange = myTable.HeaderRowRange.Offset(-1)
myTable.ListRows.Add.Range.Value = formulaRange.Value
End Sub
edited Nov 16 '18 at 18:36
answered Nov 16 '18 at 18:19
BigBenBigBen
7,1252719
7,1252719
Code added above
– Nik
Nov 16 '18 at 20:33
add a comment |
Code added above
– Nik
Nov 16 '18 at 20:33
Code added above
– Nik
Nov 16 '18 at 20:33
Code added above
– Nik
Nov 16 '18 at 20:33
add a comment |
Sub SO()
Dim lst As ListObject
Set lst = ActiveSheet.ListObjects("Table1")
With lst.DataBodyRange
.Rows(.Rows.Count + 1).Value = .Rows(1).Offset(-2).Value
End With
End Sub
1
@BigBen I have corrected code once again - now everything works as expected: values from formulas are written to the end of table. 😉
– JohnyL
Nov 16 '18 at 19:37
thank you. will this work if I am looping through different table names therefor working with tables on different sheets as I loop? ...I have a number of tables in the workbook on different sheets that I'm repeating this process on.
– Nik
Nov 16 '18 at 20:43
@Nik Yes, this will work on any table as far as formulas are always above table's header. You just need to adapt my code to use correct sheet (and table name, if it's required).
– JohnyL
Nov 16 '18 at 20:47
So, I will need to define the sheet for each table as I loop through them? I can add a column to my "table name lookup list" that contains the sheet name it's on. Was hoping to get around this. I thought that using Range() you didn't have to tell VBA where the table is located....
– Nik
Nov 16 '18 at 20:55
@Nik I will need to define the sheet for each table as I loop through them? Yes, since everyListObject
belongs toWorksheet
. I can add a column to my... You can get list of tables' names from anything you want (including hard-coding). I thought that using Range() you didn't have to tell VBA where the table is located And do you imagine how to do it?ListObjects
is for very this thing - locating tables. You could, of course, store locations of tables, but it's not worth it - useListObjects
instead.
– JohnyL
Nov 17 '18 at 7:31
|
show 1 more comment
Sub SO()
Dim lst As ListObject
Set lst = ActiveSheet.ListObjects("Table1")
With lst.DataBodyRange
.Rows(.Rows.Count + 1).Value = .Rows(1).Offset(-2).Value
End With
End Sub
1
@BigBen I have corrected code once again - now everything works as expected: values from formulas are written to the end of table. 😉
– JohnyL
Nov 16 '18 at 19:37
thank you. will this work if I am looping through different table names therefor working with tables on different sheets as I loop? ...I have a number of tables in the workbook on different sheets that I'm repeating this process on.
– Nik
Nov 16 '18 at 20:43
@Nik Yes, this will work on any table as far as formulas are always above table's header. You just need to adapt my code to use correct sheet (and table name, if it's required).
– JohnyL
Nov 16 '18 at 20:47
So, I will need to define the sheet for each table as I loop through them? I can add a column to my "table name lookup list" that contains the sheet name it's on. Was hoping to get around this. I thought that using Range() you didn't have to tell VBA where the table is located....
– Nik
Nov 16 '18 at 20:55
@Nik I will need to define the sheet for each table as I loop through them? Yes, since everyListObject
belongs toWorksheet
. I can add a column to my... You can get list of tables' names from anything you want (including hard-coding). I thought that using Range() you didn't have to tell VBA where the table is located And do you imagine how to do it?ListObjects
is for very this thing - locating tables. You could, of course, store locations of tables, but it's not worth it - useListObjects
instead.
– JohnyL
Nov 17 '18 at 7:31
|
show 1 more comment
Sub SO()
Dim lst As ListObject
Set lst = ActiveSheet.ListObjects("Table1")
With lst.DataBodyRange
.Rows(.Rows.Count + 1).Value = .Rows(1).Offset(-2).Value
End With
End Sub
Sub SO()
Dim lst As ListObject
Set lst = ActiveSheet.ListObjects("Table1")
With lst.DataBodyRange
.Rows(.Rows.Count + 1).Value = .Rows(1).Offset(-2).Value
End With
End Sub
edited Nov 16 '18 at 19:32
answered Nov 16 '18 at 19:12
JohnyLJohnyL
3,73811025
3,73811025
1
@BigBen I have corrected code once again - now everything works as expected: values from formulas are written to the end of table. 😉
– JohnyL
Nov 16 '18 at 19:37
thank you. will this work if I am looping through different table names therefor working with tables on different sheets as I loop? ...I have a number of tables in the workbook on different sheets that I'm repeating this process on.
– Nik
Nov 16 '18 at 20:43
@Nik Yes, this will work on any table as far as formulas are always above table's header. You just need to adapt my code to use correct sheet (and table name, if it's required).
– JohnyL
Nov 16 '18 at 20:47
So, I will need to define the sheet for each table as I loop through them? I can add a column to my "table name lookup list" that contains the sheet name it's on. Was hoping to get around this. I thought that using Range() you didn't have to tell VBA where the table is located....
– Nik
Nov 16 '18 at 20:55
@Nik I will need to define the sheet for each table as I loop through them? Yes, since everyListObject
belongs toWorksheet
. I can add a column to my... You can get list of tables' names from anything you want (including hard-coding). I thought that using Range() you didn't have to tell VBA where the table is located And do you imagine how to do it?ListObjects
is for very this thing - locating tables. You could, of course, store locations of tables, but it's not worth it - useListObjects
instead.
– JohnyL
Nov 17 '18 at 7:31
|
show 1 more comment
1
@BigBen I have corrected code once again - now everything works as expected: values from formulas are written to the end of table. 😉
– JohnyL
Nov 16 '18 at 19:37
thank you. will this work if I am looping through different table names therefor working with tables on different sheets as I loop? ...I have a number of tables in the workbook on different sheets that I'm repeating this process on.
– Nik
Nov 16 '18 at 20:43
@Nik Yes, this will work on any table as far as formulas are always above table's header. You just need to adapt my code to use correct sheet (and table name, if it's required).
– JohnyL
Nov 16 '18 at 20:47
So, I will need to define the sheet for each table as I loop through them? I can add a column to my "table name lookup list" that contains the sheet name it's on. Was hoping to get around this. I thought that using Range() you didn't have to tell VBA where the table is located....
– Nik
Nov 16 '18 at 20:55
@Nik I will need to define the sheet for each table as I loop through them? Yes, since everyListObject
belongs toWorksheet
. I can add a column to my... You can get list of tables' names from anything you want (including hard-coding). I thought that using Range() you didn't have to tell VBA where the table is located And do you imagine how to do it?ListObjects
is for very this thing - locating tables. You could, of course, store locations of tables, but it's not worth it - useListObjects
instead.
– JohnyL
Nov 17 '18 at 7:31
1
1
@BigBen I have corrected code once again - now everything works as expected: values from formulas are written to the end of table. 😉
– JohnyL
Nov 16 '18 at 19:37
@BigBen I have corrected code once again - now everything works as expected: values from formulas are written to the end of table. 😉
– JohnyL
Nov 16 '18 at 19:37
thank you. will this work if I am looping through different table names therefor working with tables on different sheets as I loop? ...I have a number of tables in the workbook on different sheets that I'm repeating this process on.
– Nik
Nov 16 '18 at 20:43
thank you. will this work if I am looping through different table names therefor working with tables on different sheets as I loop? ...I have a number of tables in the workbook on different sheets that I'm repeating this process on.
– Nik
Nov 16 '18 at 20:43
@Nik Yes, this will work on any table as far as formulas are always above table's header. You just need to adapt my code to use correct sheet (and table name, if it's required).
– JohnyL
Nov 16 '18 at 20:47
@Nik Yes, this will work on any table as far as formulas are always above table's header. You just need to adapt my code to use correct sheet (and table name, if it's required).
– JohnyL
Nov 16 '18 at 20:47
So, I will need to define the sheet for each table as I loop through them? I can add a column to my "table name lookup list" that contains the sheet name it's on. Was hoping to get around this. I thought that using Range() you didn't have to tell VBA where the table is located....
– Nik
Nov 16 '18 at 20:55
So, I will need to define the sheet for each table as I loop through them? I can add a column to my "table name lookup list" that contains the sheet name it's on. Was hoping to get around this. I thought that using Range() you didn't have to tell VBA where the table is located....
– Nik
Nov 16 '18 at 20:55
@Nik I will need to define the sheet for each table as I loop through them? Yes, since every
ListObject
belongs to Worksheet
. I can add a column to my... You can get list of tables' names from anything you want (including hard-coding). I thought that using Range() you didn't have to tell VBA where the table is located And do you imagine how to do it? ListObjects
is for very this thing - locating tables. You could, of course, store locations of tables, but it's not worth it - use ListObjects
instead.– JohnyL
Nov 17 '18 at 7:31
@Nik I will need to define the sheet for each table as I loop through them? Yes, since every
ListObject
belongs to Worksheet
. I can add a column to my... You can get list of tables' names from anything you want (including hard-coding). I thought that using Range() you didn't have to tell VBA where the table is located And do you imagine how to do it? ListObjects
is for very this thing - locating tables. You could, of course, store locations of tables, but it's not worth it - use ListObjects
instead.– JohnyL
Nov 17 '18 at 7:31
|
show 1 more 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%2f53342095%2fin-vba-how-do-i-create-a-range-out-of-the-cells-above-a-table%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
How is
rangeActive
assigned, and where is the table?– Rory
Nov 16 '18 at 16:48
I don't think we have the full picture, but I'd guess that you would want
rangeActive.Rows(1).Offset(-2)
... but we still need to know aboutrangeActive
.– Andy G
Nov 16 '18 at 16:51
see code above that I added. the rangeActive assignment changes as the code loops the names of tables on various sheets of the workbook.
– Nik
Nov 16 '18 at 20:37