Excel table not expanding when new data is added - excel

I have an Excel spreadsheet which contains four data tables on the same tab. The final 3 columns in each contain formulae.
I then have a vba code which performs an sql look up within an SQL Server table and returns a set number of columns to update each of the four tables.
For three of these tables, the data is pasted in and the table automatically expands and the formulae is copied to the end of the table.
However, for the first table, the data is pasted in but the table does not expand and hence, the formulae does not get added to the rows outside of the table.
I have searched high and low for a fix but no joy. I have searched the data extraction VBA code with no luck (the code is an exact match) and no joy in the table properties. I have also checked the Options menu for Auto Format As You Type settings but all seems fine.
Does anyone have any ideas for me?
Thanks,
David.
Hi All again,
Would love to know if anyone has an answer for this but, in the mean time I have fudged the code to automatically adjust the size of the table based on the row number for the last cell of data.
Thanks,
David.

I ran into the same issue and it was driving me nuts. Found out that in a few columns I had dragged formulas down pretty far (they were IF statements to return nothing if the first column was empty) so I didn't see them.
So remove any formulas that are 'under' the last row of the table. Should clear up the issue.

The most common cause is due to differing number of, or mis-aligned columns (vertically stacked tables) or differing number of, or mis-aligned rows (horizontally stacked tables).
For simplicity, let us use vertically stacked tables to illustrate what is happening. Consider an upper table with 2 columns and a lower table with 3. If we try to insert a row to the upper table, XL attempts to push all cells beneath the table down, but ONLY the cells directly beneath the table. So in this case cells below the 2 columns will be pushed down. XL quickly realizes that if it pushes just 2 columns down, the third column in the lower table won't be pushed down resulting in skewing the lower table. XL makes no attempt to figure out how to move the lower tables. It just refuses to even try.
The same holds true for adding columns to tables on the left for which there are tables directly to the right. If adding columns to a table would result in skewing rows of a table to the right, XL refuses to help.
To keep this from being a problem we can do a few things:
Keep the largest tables on top and to the left and align all table first columns and first rows.
If only rows are added to tables, don't stack them vertically
And if only columns are added to tables, don't stack them horizontally.

Herewith is an example of enforcing expansion of a table after data was copied over to the bottom of the table. This is done by resizing the table. Originally the table wasn't auto-resized as it has additional calculation columns.
The first line of the code finds the last row of data and the second line states the range to which the table needs to be resized. The third line resizes the table to the new range.
Replace sXXX and tXXX with your sheet name and table name.
Sub ResizeTableXXX()
myLastrowOfData = sXXX.Range("A1000000").End(xlUp).Row
myNewRangeSize = "$A$5:$P$" & myLastrowOfData
sXXX.ListObjects("tXXX").Resize Range(myNewRangeSize)
End Sub
HIH

Related

In excel, how can I automate a sheet to be filled based on conditions of another sheet?

Is there a way in excel to copy the contents of one column to another sheet based on particular values in another column.
I have data which looks like this:
Sheet1:
Sheet2:
I would like to copy the column A of sheet 1 directly onto column A of sheet 2, but only if the value in column B is Y.
I have tried used the match function but am not sure how the best way to do this would be. Is there a way for me to achieve this?
My desired output is:
As discussed in the comments, #Waldorf99 was looking to have a second worksheet that would automatically show a filtered list from the first sheet. I can think of a few ways to do this (array formulas or pivot tables come to mind). The problem with mixing dynamic columns with static values is that the static values would become desynced from the dynamic ones.
In the original example, rows may have a blank value in the condition column in sheet one, and then may be assigned a Y or N at a later date. If a Y is assigned to a row in the middle of other rows, the filtered sheet would shift the existing rows down to make room. The static values would stay where they were, and would become desynced. To demonstrate:
If the above image is the original state of sheet 1 and 2...
...adding a y next to x.1.c would result in sheet two shifting columns A and B of row 2 down, but leaves columns C and D behind (as they are static, and not tied into the first two columns in any way).
One thing that may work as you are wanting are filters. You would only have one sheet, with all of the data manually entered. Then you can add filters, and change them to hide rows temporarily when needed.
To add and use filters:
Start with your data all on one sheet...
Highlight your data...
On the Home tab, select "Format as Table" and choose any style...
This turns your data into a table. You can filter by clicking the drop down in a given columns header row, then deselecting the values you want to hide.
The results are a table that only shows the rows with a 'Y'.
The other rows aren't removed, just hidden. You can always reset or change your filters, to configure which rows are visible.
Note: when working with tables, they will auto expand to account for new rows, so long as you work in the row directly under the table (e.g. don't leave blank rows). You can also manually resize the table at any time by clicking and dragging from the bottom right corner of the table.
There are tons of resources of Excel tables online, and it's a pretty useful tool in Excel.
Hope that achieves what you were looking for.

Return all matches in columns using one criterion (from drop down on another sheet)

I am having two tables, one table (table 1) below is main data, separated into many columns and blocks per 10 rows in one, as you can see A2:A11, A12:A21 and so on (many rows and columns) ... In Column A are unique numbers, but one number per block as a unique, and all other information in other columns can differ, sometimes will be more data sometimes not. The data is always same structured.
What I am looking is to have report / printable sheet where I will have all my columns on one sheet, but in a Column A2 for instance drop down list that will use all unique numbers from main data table and populate all matches on report page, same as it is on picture below. There are many columns that should work automatically when I choose something from "Number" drop down. The trick here is, there must be 10 rows per block also, I have organized it as it should be, but I can't figure out how to populate using functions. So, you can imagine clicking one drop down and getting those lists updated without scrolling endlessly through columns and rows on main data table...
Where I have a problem is that I am getting matches only for first rows but not whole rows (or all rows where matches are) ...
using this function:
=INDEX(Table1[Datum];SMALL(IF(Table1[Nummer]=Printable!$A$2;ROW(Table1[Nummer])-ROW(INDEX(Table1[Nummer];1;1))+1);1))
If someone can help me out, I would be happy!
Try below formula. After putting formula to cell drag down and across as needed. I have tested this formula in Excel2013 and with range (not as table). So, you have to adjust formula for table. I suggest you to first test the formula in single sheet like my screenshot and if it is successful then go for table in Report sheet.
=INDEX(INDEX(B:B,MATCH($G$2,$A:$A,0)):INDEX(B:B,MATCH($G$2,$A:$A,0)+9),ROW(1:1))

Restructuring data in excel

I am trying to condense data in a specific way. I want any occurrences of the number 1 in each column to show up as 1 (regardless of how many times it occurs) with the corresponding site, in the corresponding column. Some sites occur multiple times in the original data, and I want to make it so that only one of each unique site shows up in the resulting data table with a 1 for the corresponding column if there any 1's in the column from the original data.
I would think it would be a vlookup function, but I have tried many different things and I am really stuck on this.
Image of original data and what I am trying to do:
Thank you
This assumes that your data set only contains 1 or blank and this approach uses a Pivot Table with MAX function. Below are details in case anyone doesn't know Pivot Tables.
Select a cell in your data and insert Pivot Table. Note, I added a title for column A, as you need that in the Pivot Table.
Click in the created Pivot Table and the PivotTable Fields dialog should pop up. If not, right click in Pivot Table and select Show Field List.
Drag the Field names (Code, a, b,& c) down to the appropriate blocks below. (Values under Columns will be created for you.)
Click on the drop down arrow next to each field name and select Max. That will rename it to "Max of ...". If that bothers you, then you can type the name you want into the Custom Name field. Note, it will not let you type the same name as the field name, eg a, but it will work if you put a space in front of it.
Given that the Pivot Table would be a lot of work for a large number of columns, here is a formula based approach. Put this formula in cell G2, then drag it down and across to fill your new table.
Note, you will have to populate all codes that you have in column F. And if any new codes are added later you will have to keep this updated. One of the advantages of a Pivot Table is that it will do this for you.
I know that you won't be putting this in these cells, so adjust accordingly. In fact, I would recommend this be in another sheet.
=IF(COUNTIFS($A:$A,$F2,B:B,1)>0,1,0)
COUNTIFS($A:$A,$F2,B:B,1)
This will count each occurrence when the value in column A matches your code $F2 AND the value in column B equals 1.
If that count is >0, then you know that at least one match was found and the IF will return 1, otherwise 0.

Get the Tables 1st row number

I have an excel sheet that contains several tables. I am using Matchto find some entries in each table, but the thing is that the rownumber returned is with respect to the table, but not the whole worksheet. How can I get it with respect to the sheet?
Currently, I am just adding manually the number of rows above =MATCH(value;array;0)+23 (w.r.t to the image above. , but when there are some records entered in the tables above the used one, then everything crashes.
Thanks for the suggestion.
ROW(TABLENAME) will give you the first row of the data so:
=ROW(Table1)-1
or
=ROW(Table1[#Headers])
Will give the first row of the actual table, where Table1 is the name of the structured table:

Is there an easy way to reformat a poorly-formatted tree to a two-column table?

I have a table representing a series of components and their subcomponents, and the subcomponents' respective subcomponents, and so on. It currently looks like a tree (one-to-many relations), but it could change at some point to resemble a graph (many-to-many relations) instead. Unfortunately, it was poorly formatted by its author, and looks something like this:
The above format is poor because there is a lot of data duplication and it is limited to a set number (4) of tiers. I would instead prefer if it looked something like this:
The above format is nice because there is very little data duplication, and it is not limited to a set number of tiers.
In case there is any confusion about what the tables represent, here is a graphical representation of the data:
It is simple enough to convert from the poor format to the nice format, but there are hundreds of root components, and manual data entry would be far too time-consuming and tedious.
I suspect this problem is unique and I am prepared to write some VBA code myself to parse the table into the nice format, but I thought I'd make sure that this wasn't a common problem with a pre-rolled solution before I rolled my own.
Is there a technical term to describe the poor formatting in the first table? Is there an easier way to reformat the data than to write a VBA macro?
This may be a complete aberration but it works for your sample (and at the moment I don’t have time to break it!)
Add an index (and a label for it) and reverse pivot (eg see An excel formula to find a row/column index in array).
Instead of drilling down on the Grand Totals intercept, drill down on each of the totals for the Tiers.
Reassemble the tables side by side, delete all columns except the Value ones and copy table to another area with Paste Special Values. Remove Duplicates on the range. Every time the value in the column immediately to the right does not change, delete and shift the values in the cells to the left. Reorder the columns right to left.
I copied each pair of adjacent columns in the Tier table (Tier 1 & Tier 2, Tier 2 & Tier 3, Tier 3 & Tier 4) and pasted them stacked vertically into a single pair of columns (Subcomponent & Component).
Next, I removed duplicates by selecting both of my new columns and clicking Remove Duplicates in the Data ribbon tab.
Next, I had to remove all rows which contained a blank cell in the Subcomponent column. To do this, I selected both columns again and filtered the data by clicking Filter in the Data ribbon tab. I selected (Blanks) in the Filter menu on the Subcomponent column and deleted all visible rows. I removed the filer by selecting (Select All) in the filter menu.
The resulting table contained many blank rows, so again I removed duplicates, and then manually shifted the data up one row to displace the one remaining blank row.
In the end, it took about a half hour, which is probably less time than it would have taken me to code a macro, and definitely less time than manual data entry.

Resources