I need to duplicate cells from one worksheet to another if the associated record [row] contains a given word.
Say row 2 has "foo" in cell A, I would like to duplicate all the cells in that row to another workbook. If A3 has "floop" in it no cells should be duplicated—it would be skipped essentially (without a blank row).
The data is as so:
A | B | C
2 foo | blah | blah
3 floop | blah | blah
4 foo | blah | blah
The resulting table would be as so:
A | B | C
2 foo | blah | blah
3 foo | blah | blah
—
This is as far as I've gotten:
I've created a helper table beginning at A52 to sort the data for which "foo" is present in column A utilizing the formula =IF(ISNUMBER(SEARCH("foo",a2)),a2,"")
[Which I have a feeling is redundant, but is the only bit that seems to be working.]
In the final table I'm using =IF(ISNUMBER(ISBLANK(A52)),A2,[!!!!!])
[Wherein [!!!!!] is the display code that I don't know.]
It resolves as "FALSE"
I know this is likely an elementary question, but I haven't had any luck finding an answer elsewhere.
Thanks in advance!
– p
PS As I'm writing this I realize that I'm going to need to process the entire row as opposed to the individual cells, so I'm likely going to need another approach. Oh well! I've gotten this far!
Best Answer
Using an IF formula to achieve this is going to be problematic for a number of reasons. But assuming you got it to work, you're always going to have the issue that a formula has to be placed within a cell and therefore your workbook is not scalable (i.e. what happens over time if/when you add/delete rows)? Sure, you can work around that too (e.g. by formatting your data as a proper table/list), but from my experience you'd be better to use a macro to do this.
Step 1: Add the Developer tab to Excel
First of all you need to ensure that you have the Developer tab visible in Excel's ribbon. If it's already there (typically it's the last one after the View tab, then skip to Step 2: Creating your macro. Otherwise, follow the steps below:
Step 2: Creating your macro
Now we need to create your macro. Follow these steps:
The top left of the window should look something like:
Now right-click on ThisWorkbook and select Insert > Module (see below)
Now copy the code below and paste it into the white space:
Now you should see something like the following:
Now you will need to customise this code slightly as it's currently written specifically for the example in your question. You need to customise the following aspects:
Sheet1
with the name of your first worksheet (i.e. the one you're copying from)Sheet2
with the name of your second worksheet (i.e. the one you're copying to)A1:A
indicates the column being searched for the value you're trying to match (i.e. the "foo" value in your example. If it's not Column A in your worksheet, then change the A to whatever column it needs to be. For example, if your worksheet contains the "foo" value in Column D, then this needs to beD1:D
"foo"
in the code. Replace the foo with whatever value you're trying to match.Finally, you will need to save your workbook as a macro-enabled one. To do this, click on the Excel icon at top left to return to Excel and then choose the Save As option from the File menu. For the file format you must select the Excel Macro-Enabled Workbook (.xlsm) option from the drop-down list.
Step 3: Using the macro
Now you're set to go. To use the macro, follow these steps:
Note: The above code was adapted from code originally sourced from https://www.extendoffice.com/. I have no affiliation with that website whatsoever.