How to set a ListObject equal to another ListObject? - excel

I would like to set a ListObject to be an exact copy of another ListObject just like how you can set a range to be an exact copy of another range. Is this possible?
I've tried the following but it did not work:
Set destinationWorksheet.ListObjects("tblPriorTable") = sourceWorksheet.ListObjects("Table1")
It is important that the "tblPriorTable" ListObject not be deleted, because I have formulas in my workbook which reference that ListObject and those formulas break when the ListObject gets deleted. I just want to essentially overwrite the contents of "tblPriorTable" to be equal to another ListObject without ever deleting "tblPriorTable".

You can use a procedure like this:
Sub Copy_Contents(FromTable As ListObject, ToTable As ListObject)
With ToTable
If Not .DataBodyRange Is Nothing Then
.DataBodyRange.ClearContents
End If
.Range(2, 1).Resize(FromTable.ListRows.Count, FromTable.ListColumns.Count).Value = FromTable.DataBodyRange.Value
End With
End Sub
Note that it assumes your two listobjects have the same number of columns.

The ListObject does not have a .Copy method, but a Range does:
Sub lksdhfs()
Dim r As Range
Set r = ActiveSheet.ListObjects(1).Range
r.Copy Range("g30")
End Sub
NOTE:
The copy produces another ListObject, not just a pile of cells.Make sure the two tables do not overlap.

Related

What is the proper syntax to reference a table by index number in a VBA script

I'm trying to put together a very simple VBA script to clear cell contents within a specified table column range ([Front Straddle]:[Front Option]), of all tables within a specified worksheet. This script will only live within the "VolJump" worksheet, which contains an arbitrary number of identically formatted, differently named tables. Because of this, I felt the best approach was to reference the tables by the index number.
This is where I'm running into issues with the proper referencing/nesting within the 'Range' function, shown below. Any help is greatly appreciated.
Sub ClearCells()
Dim i As Long
Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets("VolJump")
If sh.ListObjects.Count > 0 Then
For i = 1 To sh.ListObjects.Count
Range("Activesheet.ListObjects(1)[[Front Straddle]:[Front Option]]").Select
Selection.ClearContents
Next i
End If
End Sub
Clear Contents of Table Columns Range
Option Explicit
Sub ClearCells()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("VolJump")
Dim tbl As ListObject
For Each tbl In ws.ListObjects
ws.Range(tbl.Name & "[[Front Straddle]:[Front Option]]").ClearContents
Next tbl
End Sub
Just using the ListObjects:
Sub ClearColumns()
Dim lo As ListObject
Dim ColNum1 As Long, ColNum2 As Long
For Each lo In ThisWorkbook.Worksheets("Sheet1").ListObjects
'Get the index numbers of the start and end columns.
ColNum1 = lo.ListColumns("Front Straddle").Index
ColNum2 = lo.ListColumns("Front Option").Index
'Resize the range from the start column to the end column and clear it.
lo.DataBodyRange.Columns(ColNum1).Resize(, ColNum2 - ColNum1 + 1).ClearContents
Next lo
End Sub

Adding a new row with data to excel sheet using VBA

I am adding a new row to a table but want to then add the data to that row that I just added. I am thinking something like this, but not sure how to add each columns data to that new row. My table has 4 columns named "store" "emp#" "date" & "amt". I have specific data that I will put in each column. I simplified the code, as there is a whole lot more to the macro, but just stuck on this part. Thank you for you help.
Dim rt_ws As Worksheet
Dim rt_tbl As ListObject
Set rt_ws = ThisWorkbook.Worksheets("RT Clock Hours")
Set rt_tbl = rt_ws.ListObjects("rt_hours")
With rt_table.ListRows.Add
. `this is where I am not sure what to do`
.
.
End Sub
Try this code
Sub Test()
Dim ws As Worksheet, tbl As ListObject
Set ws = ThisWorkbook.Worksheets("RT Clock Hours")
Set tbl = ws.ListObjects("rt_hours")
With tbl.ListRows.Add
.Range = Array("Store1", "1530", "05/03/2020", "Amt1")
End With
End Sub

Delete used range from every worksheet in workbook

i have this test dataset:
i have it on multiple sheets, but its always different range - more columns, more rows etc. Beneath this "header" are always some blank rows.
I would like to loop throug all sheets and select this header using End(xlDown) and End(xlToRight).
I am trying to do this with the following code:
Sub WorksheetLoop()
Dim ws As Worksheet
Dim rng As Range
For Each ws In ActiveWorkbook.Worksheets
Set rng = Range("A1", Range("A1").End(xlDown).End(xlToRight))
rng.Delete
Next ws
End Sub
This macro deletes everything on first worksheet and nothing happens on any other sheet.
I tried using ws.rng but then i get an object error.
Can you please tell me what am i doing wrong?
I am studying some VBA material and trying to make changes, but i always end up with an error.
Thanks
This error is very common.
You must add a sheet reference to every instance of Range (and Cells) as otherwise your range can be straddling two sheets (the specified one and the active sheet, which is implied in the absence of anything else), which causes an error.
Set rng = ws.Range("A1", ws.Range("A1").End(xlDown).End(xlToRight))
In this particular case, you could also consider this variation:
Set rng = ws.Range("A1").currentregion
CurrentRegion.

Excel VBA - Set Range Filtered table Visible Cells

I am using this code to set a range of filtered table to visible cells only.
Set rngMyRange = Selection.SpecialCells(xlCellTypeVisible)
However, there is a strange bug if only one cell is selected, that is, the code selects all used range in the filtered table.
If the selection is greater than one cell, then the code works as expected. Is there a way around this?
Usually using "Selection" is not a good practice; I am guessing you just need to get the range of the visible cells for that you can easily use this:
Sub GetVisibleRangeOnly()
Dim tbl As ListObject
Dim rng As Range
'change the name of the table and worksheet as you need
Set tbl = Worksheets("Sheet1").ListObjects("Table1")
'Note: if there is no visible cell after filtraton rng IS NOTHING will be TRUE
Set rng = tbl.DataBodyRange.SpecialCells(xlCellTypeVisible)
End Sub

excel VBA for resizing ranges refered with listobject ranges

I what to set a range into a variable to use in:
Private Sub Worksheet_Change(ByVal Target As Range)
I want to refer to a cell which is 3 cells of top of a particular column of a list object.
this way:
set rOverlap = intersect(SRTbl.ListColumns(firstConceptColumn).DataBodyRange.iTem(1).offset(-3, 0).resize(0,3),target)
where firstconceptcolumn=9 (or whatever)
This would not work.
I do it in this way because If I ever decide to add columns at the beginning of the table the code will not need to be chage.
The first row of the listobject is not row1 but row5.
The first 5 rows trigger subroutines when the values change (upon entering data an pressing return)
I also tried it using headerRowRange with:
Set rOverlap = Intersect(SRTbl.HeaderRowRange(firstConceptColumn).offsset(-3, 0).Resize(, 4), Target)
which would not work neither.
Actually as a more fundamental question I am thinking what kind of data is:
SRTbl.HeaderRowRange(firstConceptColumn)
Is that a range?
why cant you use the following:?
SRTbl.HeaderRowRange("name")
Thanks a lot
PS: Everything what I say on stackoverflow is to resize the listobject itself.
PS2: I dont understand neither if there is a difference between these two guys:
.Resize Range("A1:B10")
and
.Resize(1,10)
Jose Ferro
Here is one way:
Option Explicit
Public Sub test()
Dim SRTbl As ListObject, header As Range, ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet2")
Set SRTbl = ws.ListObjects(1)
Set header = SRTbl.ListColumns("Name").Range(1, 1)
Debug.Print header.Offset(-3).Resize(1, 2).Address
End Sub
Here is another:
Option Explicit
Public Sub test()
Dim SRTbl As ListObject, header As Range, ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet2")
Set SRTbl = ws.ListObjects(1)
Set header = SRTbl.HeaderRowRange.Find("Name")
If Not header Is Nothing Then
If header.Row > 3 Then
header.Offset(-3) = "Tada"
End If
End If
End Sub
To refer to whole header width with offset of -3 rows:
SRTbl.HeaderRowRange.Offset(-3)
To simply resize found cell:
header.Offset(-3).Resize(1,2)
To resize to end of table from found cell
Debug.Print header.Resize(1, ws.Range(Split(SRTbl.HeaderRowRange.Address, ":")(1)).Column - header.Column + 1).Address

Resources