Excel apply formula after pasting table - excel

Is there a way to create an excel template, which automatically applies a formula if a certain column is inserted? The use case would be:
User copies a table from a desktop program (CSV or Tab separated)
Pastes the table into the excel template
Somehow the column's header gets matched (header's name is fixed)
Multiply the column values with a fixed value (like 1.25)
The problem is that the column could be in different indexes and the formula should apply to all the rows, not just fixed ones, but my main problem is how can I get a copy-paste event and find the column.
Edit.: I can only do this with VBA (never used it before)?

Assuming the column header is constant you could insert a table with "dummy" headers and a calculated column using the header name as a reference
In the example I have put a formula in the Calc column. I've put the Calc in column G assuming your maximum import columns are six and because it's good practice to do your calculations from left to right.
You could move the Calc to column A and reduce the "dummy" columns to just 1 with whatever your desired heading is.
To make a template, delete row 3 and clear out the dummy data, just leaving the calculation(s) and save as a .xltx in your profile template folder (wherever that may be in your versions of Windows/Office).
When you paste in your import data the table will extend down and copy the formula(s) automatically. If you have moved your calculation(s) to the left of the table, the table will resize to the right to accommodate all your imported columns.
While the MyHeader column in your template may be over-written, if it can still be found in `Table2[#Headers] the formula will still work.

Related

Excel Function to Exclude rows based on certain values

I to exclude rows in a excel table based on certain values
For example:
I need to exclude all rows if column A is equal to any of these numbers ( 5840,4302,4432, and so on)
As the table data will be huge to filter only the data that I want.
One way is to exploit Excel Table feature together with the FILTER() spreadsheet function. NB. You will need a relatively recent Excel version for this. Using a Table provides some extra useful functionality (such as automatically adding rows and allowing reference by column name).
The OP's input data may already be a Table, if so, this first step can be skipped.
Put the input and filter list into tables. Excel help page. After the table has been created I have used the Table Design menu (which appears in the menu bar when a cell in the table is selected) to turn off the row banding format and header filters. This is also where you can rename the Tables. I have named them "Input" and "Exclude"
For the filtered output, choose where you want the output to start (cell H3 in my example), and enter a formula to copy the headers: =Input[#Headers]. Of course you can copy and paste the headers manually if you like. Here I've used the Format Painter to copy across the cell formats for the headers.
In the next cell down (H4 in my example), enter this formula: =FILTER(Input,(LEN(Input[ID])>0) * ISERROR(MATCH(Input[ID],Exclude[IDs to exclude],0))).
You should be able to add or delete new rows (right-click in the Table and choose Delete) in both the Input and Exclude tables, and the output should react (if you have Calculation set to Automatic).
NB. The Output range is NOT a Table. Excel doesn't let you convert dynamic ranges into Tables.
EDIT: If you don't want to use Tables, you can simply supply the ranges as the parameters to the FILTER function.
In this example =FILTER(B4:D13,(LEN(B4:B13)>0) *ISERROR(MATCH(B4:B13,F4:F5,0)))

Data in column instead of rows

I am getting data from SQL Server into an Excel sheet. Its a customer table containing 20 records. The columns are Customer Code, Customer Name and Customer Short Name.
What I need is instead of data coming in rows (from row 2 to row 21)... it should come in columns.. i.e. 1st three rows of column A contains respective headers (code, name and short name) and column B first row contains value of code, second row value of name and third row value of short name... same continue to column C, D, E..... till all 20 records..
Is it possible in excel?
In Excel, you can easily transpose the array with your data. Consider the following sample data in the range A1:C4:
You could now select the entire range and copy it via CTRL+C -> Paste -> Transpose (T). A second possibility would be to enter the following formula in, e.g., cell E1: =TRANSPOSE(A1:C4). This is a dynamic array formula in the current version (Microsoft 365). If you have an older version, you have to enter the formula as a legacy array formula by confirming the formula with Ctrl+Shift+Enter.
The result is as follows:
Edit: Dynamic setting of range
Theoretically, assuming that the data structure remains the same in terms of the number of columns and rows, the formulaic approach will update the transposed array accordingly as the original data is updated. In the specification above, the range A1:C4 is hard coded. While it is very likely that the number of columns will remain the same (could also be dynamically adjusted), in practice additional rows could be added/deleted from the data. In this case, you can specify the number of rows dynamically, which solves this problem:
=TRANSPOSE(INDEX(A:C,1,1):INDEX(A:C,COUNTA(A:A),3))

Excel VBA - Copy a Column from Table"TBL_IMPORT" and change value during coping

I use
Sheets"PrepCalc".ListObjects("TBL_PrepCalc").ListColumns.Add ...
to copy some whole columns from a "TBL_IMPORT" table on an "Import" Sheet. Works fine.
But there are Columns that need some adjustment and calculation,
like f.e.
Sheet "Import" - Table "TBL_IMPORT" Column "Area" contains the text string "125000000000mm3"
and needs to be placed as '12,5' (as a number) in the column "Area" at the end of the "TBL_PrepCalc" table.
Another column named "width" is also text with f.e "38mm" as value and has to read in the new column as a number value '38'.
The import table can contain 4500-10000+ rows and 18 columns. ± Half of the columns needs to be changed when copied. What is the best way to work as efficiently as possible?
At first I was thinking to walk through each row, create an universal function that take the "import_table_column" as argument1; the function "Do_the_Area_convert()" that do the math as a argument2; target column as Argument3.
Any suggestions? Being looking for this at this forum and googling around the world, but still haven' found what I'am looking for.
thank you

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.

How do I prevent Excel from automatically replicating formulas in tables?

I'm using Excel 2016. I have a table with headers and when I plug in a formula, Excel is automatically replicating the formula to all other cells in the column. While that would normally be fine, it's wrongly calculating the table headers. I thought I could just change the top row to exclude the header but Excel updates the rest of the column which I don't want.
I would like to either turn this automatic formula replication feature off or figure out a way to customize the formula in the top row so it doesn't calculate the header value.
Here's the formula I'm using and I didn't do anything special with the table outside of add a 'Totals' row:
=SUM(B2+C1-D2)
You can stop creating calculated columns. The option to automatically fill formulas to create calculated columns in an Excel table is on by default. If you don’t want Excel to create calculated columns when you enter formulas in table columns, you can turn the option to fill formulas off. If you don’t want to turn the option off, but don’t always want to create calculated columns as you work in a table, you can stop calculated columns from being created automatically.
Turn calculated columns on or off
1) On the File tab, click Options.
2) Click Proofing.
3) Under AutoCorrect options, click AutoCorrect Options.
4) Click the AutoFormat As You Type tab.
5) Under Automatically as you work, select or clear the Fill formulas in tables to create calculated columns check box to turn this option on or off.
Stop creating calculated columns automatically
After entering the first formula in a table column, click the AutoCorrect Options button that is displayed, and then click Stop Automatically Creating Calculated Columns.
In Excel 2016/365 you can also change a cell you want, let it auto-populate the rest of the column, then Ctrl+Z, this will undo auto-populate but keep the new formula/text you just changed in that one cell.
First, why do you wrap a simple formula into a SUM function? I always wonder why people do that when it's much shorter to write =B2+C1-D2 instead.
Second, if you used the true capabilities of SUM() then text, i.e. your column header, would be ignored instead of throwing an error. The + and - operators don't tolerate text, be it in a table or not. You could rewrite your formula to be
=Sum(B2,C1,D2*-1)
Third, be aware that cell referencing like that will behave erratically when you insert rows into the existing table (between existing rows). The row references will be off for anything below the inserted row and you will need to manually copy down the formula again to get correct results.
In order to get a formula that does not require adjusting, you may want to use structured referencing, where each row has exactly the same formula, instead of cell references, where row references are adjusted in each row. A possible formula for this would be (if your columns are labelled data1, data2 and data3 for columns B, C and D):
=SUM([#data1],OFFSET([#data2],-1,0),[#data3]*-1)
To get the data from the row above, Offset() is used on the cell in the current row (using the # sign), with a negative row offset. Keep in mind that Offset is volatile, which may slow down very large datasets.
Adjusting AutoCorrect options is not optimal. You can do it on a col by col basis by:
let the column autopopulate your formula
delete any number of values in the autocalculated column (just one will do the trick).
adjust formulas in the column as you wish
Only able to test in Excel 2013.
May be a little late to open this query - but a couple of points:
formula replication does not work if there are already different formula in the column - may need 3 different formula before Excel says it cannot guess which formula to replicate
a solution could be to add 2 dummy rows at the top which will then prevent the replication - the benefit of this is that you are not disabling replication for any other tables that may be in the workbook; and I'm not sure if the setting may also inadvertently be copied to other new workbooks you create whilst you have the first workbook open
finally, the old chestnut that =A1+B2 etc creates an error if A1 or B2 are not numeric; whereas =sum(A1,B2) works differently in that text will be ignored and effectively treated as zero
If your reference has letters in the middle these technics won't work. To add a zero before the reference in that case, create a new column next to it, add "0" to all the cells next to the references, add another column and use the function "concontenate" or whatever between the "0" cell and the one with the reference.
Not only that, the =len function will correctly tell you the # of digits on references with letters in the middle (because excel does not view them as numbers per se, but rather text/general format) while on other references with 0s added before them with formatting this doesn't happen.
Eg: 012345 =len gives 5 digits / 01A345 =len gives 6 digits

Resources