2

I want to resize the table "ProductList" on my worksheet "Final" depending on the input from the source table "SourceTable" on the worksheet "Input" (fields derived from source table helped with formulas).

Eg.: Firstly, I want to refresh source table, then I want to resize the Table "ProductList" that has the range F1:J4 located on the sheet "Final" accordingly to the source table on the worksheet "Input" that has the current range A1:D7 growing monthly just by adding rows.

enter image description here

Help will be greatly appreciated.

VBasic2008
  • 44,888
  • 5
  • 17
  • 28
benjin
  • 65
  • 5
  • When you say "Refresh source table" ... where is that data coming from? If you use PowerQuery, you can construct the Id column as a custom column. – DS_London Jan 31 '22 at 11:15
  • Yes, it is direct load from SQL to PQ. I consider to construct Id column already in SQL. – benjin Feb 01 '22 at 08:28

2 Answers2

1

Resize an Excel Table

  • This will adjust the number of rows of the source table to the number of rows of the destination table.
  • If the destination table has more rows than the source table, the excessive rows will be deleted.
  • If the destination table contains formulas and has fewer rows than the source table, the newly added cells will update accordingly.
Option Explicit

Sub SizeTable()
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    Dim sws As Worksheet: Set sws = wb.Worksheets("Input")
    Dim stbl As ListObject: Set stbl = sws.ListObjects("Input")
    Dim srCount As Long: srCount = stbl.Range.Rows.Count
    
    Dim dws As Worksheet: Set dws = wb.Worksheets("Final")
    Dim dtbl As ListObject: Set dtbl = dws.ListObjects("ProductList")
    Dim drCount As Long: drCount = dtbl.Range.Rows.Count
    
    If drCount > srCount Then
        dtbl.Range.Resize(drCount - srCount).Offset(srCount).Delete
    End If
    
    dtbl.Resize dtbl.Range.Resize(srCount)
    
End Sub
VBasic2008
  • 44,888
  • 5
  • 17
  • 28
  • Please, how can I add the refresh script of the source table into your suggested script? – benjin Feb 01 '22 at 21:01
  • I didn't see your script, but you could put it at the beginning of my code (just add the line `YourScriptName`) or in another sub: `Sub SizeFinal() : YourSrciptName : SizeTable : End Sub` (The colons (`:`) denote a new line). – VBasic2008 Feb 01 '22 at 21:30
1

The OP (see comments) is using PowerQuery to extract a table from a SQL source. An alternative to re-sizing the table via VBA is to simply amend the original PowerQuery code to include the additional Id column. Then re-sizing will happen automatically when the query is refreshed.

In the PowerQuery Editor, you can add in a Custom column which contains a formula.

enter image description here

You can see the equation is:

=[CoCd] & "_" & Number.ToText([LSCH]) & "_" & Number.ToText([Material]) & "_" & Number.ToText([Month])

The string concatenation operator '&' only works on strings, so since my example data has numbers, I am using Number.ToText() to convert. Depending on the exact datatypes in the SQL source, this might not be necessary.

Next, in the Editor, drag the newly created 'Id' column from right to left. This will generate a 'Reordered Columns' line in the 'Applied Steps' list.

Finally, 'Close & Load' from the Home menu, and you should get this output in your sheet:

enter image description here

If you just want a subset of your SourceTable query in a table called ProductList, then you can set up a PowerQuery internally within the workbook to produce the new table from the source table, and then refresh that (you can set up a dependency on SourceTable so that will be refreshed first).

DS_London
  • 3,644
  • 1
  • 7
  • 24