I have a Table containing a list of Names.
I would like to update a secondary table to have headers that correspond to this list of names (starting from column 2).
Example if my original List contained a,b,c. I would like my secondary table to have column headers of "blank",a,b,c
With Range("Original Table[Names]")
Range("Secondary Table").HeaderRowRange.Value = .Value
End With
The above gives me errors, however, i can't seem to figure out how to solve this
Use Transpose, and Resize.
With Range("Original Table[Names]")
Range("Secondary Table").HeaderRowRange.Cells(2).Resize(,.Rows.Count).Value = Application.Transpose(.Value)
End With
Perhaps a personal preference, but I'd work with ListObjects here:
Dim origTable As ListObject
Set origTable = ThisWorkbook.Worksheets("insertname").ListObjects("Original Table")
Dim secTable As ListObject
Set secTable = ThisWorkbook.Worksheets("insertname").ListObjects("Secondary Table")
With origTable.ListColumns("Names").DataBodyRange
secTable.HeaderRowRange.Cells(2).Resize(,.Rows.Count).Value = Application.Transpose(.Value)
End With
Also, note that a column header can't be blank in a table.
Related
I have two tables in two different worksheets and I want to copy a column from one to another.
The tables can be of different size at but any column I copy from the src table will have a matching header in the destination table.
My understanding is that I have to resize the destination table to have the same number of rows as the source table. But after that I should be able to assign the destination range to equal the source range.
The code below runs with no errors but doesn't do anything as far as I can tell.
Public Sub SyncListObjects()
Dim tblDest As ListObject
Dim tblSrc As ListObject
Dim mappedCol As ClassTrackedCol
Set tblDest = ThisWorkbook.Sheets("Power BI Data").ListObjects("tblDest")
Set tblSrc = ThisWorkbook.Sheets("Imported Data").ListObjects("tblSrc")
'map the matching columns of the tables into a collection
Set destToSrcColMap = CreateTblToTblColMap(tblDest, tblSrc) 'this works correctly
tblDest.DataBodyRange.ClearContents
'the destination table should have the same number of rows as the src
'but keep it's own number of columns in order to be able to copy
tblDest.Resize tblDest.Range.Resize(tblSrc.ListRows.Count, tblDest.ListColumns.Count)
For Each mappedCol In destToSrcColMap
Set test1 = tblSrc.ListColumns(mappedCol.ColName).DataBodyRange 'this resolves correctly
Set test2 = tblDest.ListColumns(mappedCol.ColName).DataBodyRange 'this resolves correctly
tblDest.ListColumns(mappedCol.ColName).DataBodyRange = _
tblSrc.ListColumns(mappedCol.ColName).DataBodyRange 'this runs but doesn't appear to do anything
Next
End Sub
Fixing 2 issues worked for me:
Be explicit about the value transfer:
tblDest.ListColumns(mappedCol.ColName).DataBodyRange.Value = _
tblSrc.ListColumns(mappedCol.ColName).DataBodyRange.Value
ListRows.Count doesn't include the header, so change the following line:
tblDest.Resize tblDest.Range.Resize(tblSrc.ListRows.Count, tblDest.ListColumns.Count)
to
tblDest.Resize tblDest.Range.Resize(tblSrc.Range.Rows.Count, tblDest.ListColumns.Count)
I have a pivot table with data, I need to copy and past all of the data except for the last row, which I need to delete as it is a sum of totals. The amount of data entries vary by week so I cannot just put the set numbers in. Currently my code looks like this;
Worksheets("Mobile Summary").Range("A4:F482").Copy
Worksheets("Sheet1").Range("A1:F479")
However I need to copy and paste more data from a different pivot table directly under this. Therefore I couldn't just set the rows to 3000 for example. I know there is a code to find the last row, although I'm coming up short as to how to go about solving this problem.
To find a pivot table by its name and remove the last row of the table use the Range.Resize property. This is useful if there is more than one pivot table on a worksheet.
Option Explicit
Sub CopyPivotTableWithoutLastRow()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Tabelle3")
Dim i As Long
For i = 1 To ws.PivotTables.Count 'loop throug all pivot tables
If ws.PivotTables(i).name = "PivotTable1" Then 'determine pivot table by its name
With ws.PivotTables(i).TableRange1 '= full table
.Resize(RowSize:=.Rows.Count - 1).Copy 'resize to remove last row then copy
End With
End If
Next i
End Sub
I am trying to create a Stacked Column Chart with the data in the table below.
I want to Select column A1:A9 and C1:F9. The Selection also needs to be adaptive to different column sizes (i.e, someone adds another Feature). The macro should also work for a table of data, anywhere in the Sheet. As long as the macro originates from the ActiveCell.
How do I not only Select until the column end, but also Select excluding the "Values" column. I am trying to use End and Offset, but I am not sure the best way to do it. And once again, I want to use it on a table that is anywhere in the sheet and then create a Stacked Column Chart from it.
Thanks for your help!
Try this code please. The idea is that you iteratively Union various ranges of the data based on the condition that the header is not 'Values'.
The working assumption is that the CurrentRegion of the ActiveCell will select your table data. Where the definition of CurrentRegion is 'The current region is a range bounded by any combination of blank rows and blank columns. ' - MSDN link
Then the code will append the first column to an output range. After that, the outer columns will only be appended to the output range if the header is not 'Values'.
Dim rngData As Range
Dim intCounter As Integer
Dim rngToSelect As Range
Set rngData = ActiveCell.CurrentRegion
Set rngToSelect = Range(rngData.Cells(1, 1), rngData.Cells(rngData.Rows.Count, 1))
For intCounter = 1 To rngData.Columns.Count
If rngData.Cells(1, intCounter).Value <> "Values" Then
Set rngToSelect = Union(rngToSelect, Range(rngData.Cells(1, intCounter), rngData.Cells(rngData.Rows.Count, intCounter)))
End If
Next intCounter
rngToSelect.Select
I am working with ListObject objects in VBA. In particular I am creating a table dynamically (variable number of rows and columns, and variable column headers). I have the need to sort the table's columns in ascending alphabetical order.
For instance if I have the table:
I want it to be sorted like so,
Is there any way to do this? I have tried to use the Range.Sort method but it seems that this is not allowed if the range is part of a ListObject.
I also tried to record a macro to find code, but found that when I right clicked the table to sort it, the selected range left the header out and I was not able to select the "Sort left to right" option...
Any ideas?
This seems to work. It takes the name of a table (e.g. "Table1"), converts it to a range, sorts it, then reconverts it to a table with the same name:
Sub SortByCol(tableName As String)
Dim sh As Worksheet
Dim myTable As ListObject
Dim myStyle As TableStyle
Dim myRange As Range
Set sh = ActiveSheet
Set myTable = sh.ListObjects(tableName)
Set myStyle = myTable.TableStyle
Set myRange = myTable.Range
myTable.Unlist
On Error Resume Next
myRange.Sort key1:=myRange.Rows(1), Orientation:=xlSortRows
If Err.Number > 0 Then Debug.Print "Range couldn't be sorted"
On Error GoTo 0
sh.ListObjects.Add(xlSrcRange, myRange, , xlYes).Name = tableName
sh.ListObjects(tableName).TableStyle = myStyle
End Sub
On edit: I added a bit of error handling around the sort method call. Experiments showed that my original code didn't preserve the style (if the table didn't have the default style then the result was a weird blend of the default style and the original table style). I added coded to save and then restore the original table style, but I don't know much about table formatting and might have missed some subtleties. At the very least, it seems to preserve the style if it was chosen from Excel's built-in list of table styles.
I have an excel table with one header row and one data body row. I want to count the data body rows. When I'm trying to check how many rows my table has with
Set myWorkSheet= ActiveWorkbook.Worksheets("Sheet1")
Set myTable= myWorkSheet.ListObjects("Table1")
countrows = myTable.ListRows.Count
countrows contains 0. If the has 2 or more rows, it gives the right row number. Why does it say 0 for one row ans is it the best way to count the rows or are there better ones?
EDIT:
Found out whats causing the problem. I use this lines to clear the table before i fill it again:
If tblChart.ListRows.Count >= 1 Then
myTable.DataBodyRange.Delete
End If
After that operation the table looks like I described it. Without it and modifying the table to look like I described the table it worked. But why is it causing this problem?
The ListObject property you are looking for is the .DataBodyRange.
Dim myWorkSheet As Worksheet, myTable As ListObject, countRows As Long
Set myWorkSheet = ActiveWorkbook.Worksheets("Sheet1")
Set myTable = myWorkSheet.ListObjects("Table1")
countRows = myTable.DataBodyRange.Rows.Count
Debug.Print countRows
A comprehensive list of the ListObject properties is available at: ListObject Interface.
Responding late in-case Google search brings user here:
I found this problem as well. DataBodyRange object does not exist unless there are two rows of data. Not just two empty rows, but two rows of data in at least one column.
What I have found to work reliably is to test if DataBodyRange exists:
If Not TableListObject.DataBodyRange is Nothing Then
Debug.Print "Data Rows Count=", TableListObject.DataBodyRange.Rows.Count
Else
Debug.Print "No Data in Table detected"
End if