Copy a cell from one sheet to another sheet if that cell contains a particular text - excel

I have compiled wheel data but want a VBA macro that copies any cell from sheet 1 (named: SheetSJ) that matches partial text, and then copies that cell's data into sheet 2. This is to make the data much easier to work with.
Search each row for any cells in SheetJS that contain text "Product ID", if no matches then ignore
If any cell (text) matches, copy that cell, and paste the contents to sheet 2 column B (beginning with row 2)
Search each row for any cells in SheetJS that contain text "Bolt Pattern", if no matches then ignore
If any cell (text) matches, copy that cell, and paste the contents to sheet 2 column D (beginning with row 2)
Wheel Data
As evident in the picture, the data is all over the place in each column and thus the macro cannot use any particular cell for reference. It can only match text values (which are unique).
Sub Test()
For Each Cell In Sheets(1).Range("A1:ZZ200")
If Cell.Value = "Product ID" Then
matchRow = Cell.Row
Rows(matchRow & ":" & matchRow).Select
Selection.Copy
Sheets("Sheet2").Select
ActiveSheet.Rows(matchRow).Select
ActiveSheet.Paste
Sheets("Sheet1").Select
End If
Next
End Sub
I managed to find some examples online but they copy the entire row not the individual cell.

How about this code?
I cannot use English well, but if you want, I will help you with my best.
Sub test()
For Each cell In Sheets(1).Range("A1:ZZ200")
matchrow = cell.Row
If cell.Value Like "*Product ID*" Then 'You have to use "Like" Operator if you want to use wildcard something like *,?,#...
Sheets(2).Range("B" & matchrow).Value = cell.Value
'I recommend you to use ".value" Property when deal only text, not else(like cell color, border... etc), rather than "select-copy-paste". It could be slower while hoping both sheets
ElseIf cell.Value Like "*Bolt Pattern*" Then
Sheets(2).Range("D" & matchrow).Value = cell.Value
End If
Next
End Sub

I don't think you need a macro at all. In sheet2 column B, row 2, place the following formula:
=iferror(index(SheetJS!2:2,match("*Product ID*",SheetJS!2:2,0)),"")
The iferror part just keeps the cell empty if no match is found (as opposed to giving an ugly error message). Match tells how far into row 2 the product id occurs, and index goes that far in and gets the value.
Now grab the handle at the bottom right corner of the cell, and drag it down as many rows as you have rows in the first sheet. That should bring all product IDs from Sheet JS into column B.
Similarly start in row 2 column D with
=iferror(index(SheetJS!2:2,match("*Bolt Pattern*",SheetJS!2:2,0)),"")
and drag that on down.
I'm assuming no row has more than one product id or bolt pattern, which appears to be true.
This approach does have a mild drawback, that it will leave a blank space in the sheet 2 column if the SheetJS does not have that entry in that row.

Related

Match 2 colums in 2 excel sheets and paste a third

I have two excel sheets both with matching product numbers. What I need to do is match the product numbers and then copy a column from the first sheet to the second.
My example:
Column C in the first sheet contains product numbers
Column A in the second sheet contains product numbers
I want to match C & A and then copy column B from the first to the second sheet.
Sorry if this has been answered before, my knowledge is basic but I am trying to learn
Thanks for any help.
CD
Assuming your numbers to match start in C2 (headers in row 1) and numbers to copy in AC2 on Sheet1, this should do what you want. Paste it into AC2 on Sheet2 and then drag copy to the length of column A on Sheet2.
=IF(IFNA(MATCH(A2,Sheet1!C:C,0), FALSE), INDIRECT("Sheet1!AC"&MATCH(A2, Sheet1!C:C, 0)), "Not Found")
Note this will give "Not Found" for a value on Sheet2 which is not found on Sheet1, you can change that by just replacing the string "Not Found" in the formula with whatever you want (e.g. 0).
Formula:
You can use OFFSET with IF. In B1 of sheet 2 for example you could put, drag down for as many rows as required,
=IF(A1=Sheet1!C1,OFFSET(Sheet1!A1,0,COLUMNS(Sheet1!A:AC)-1,1,1))
If you set your data up as a table in sheet 2 (Ctrl + T with an populated cell selected) then add this formula to the appropriate column it will autofilter down the formula.
Or:
With code in a standard module:
Public Sub AddFormula()
Dim rng As Range
With Worksheets("Sheet2")
If Application.WorksheetFunction.Subtotal(103, .Columns(1).Cells) > 0 Then
For Each rng In Intersect(.Columns(1), .UsedRange)
If Not IsEmpty(rng) And Not IsError(rng) Then 'assume ignore empty cells
If rng = Worksheets("Sheet1").Cells(rng.Row, "C") Then Worksheets("Sheet1").Cells(rng.Row, "AC").Copy rng.Offset(, 1) 'determine where you want value to go
End If
Next rng
End If
End With
End Sub
Note:
This is based on your comment that you are comparing sheet2 col A with sheet1 col C and copying sheet1 col AC if match.
I assume that the product numbers in both sheets are unique. If that, I suggest use the offset and match formulas to achieve what you want.
sorry for my half-baked answer due to i just typed them with phone and it is not convenient to input complicated style, here is the additional information:
the match formula is used to query the location of the target product number in the original sheet( sheet 1 in your case above);
after we located the position of the target product number, extract the information you wanted through the offset formula.
here are the specific process:
Sheet 1 represent the original information and sheet 2 represent the target one. What we required to do is copy the info in col D of sheet 1 to col D of sheet through the product:
OFFSET($D$5,MATCH(I6,$D$5:$D$16,0)-1,1,1,1)
OFFSET($D$5,MATCH(I7,$D$5:$D$16,0)-1,1,1,1)
OFFSET($D$5,MATCH(I8,$D$5:$D$16,0)-1,1,1,1)
OFFSET($D$5,MATCH(I9,$D$5:$D$16,0)-1,1,1,1)
OFFSET($D$5,MATCH(I10,$D$5:$D$16,0)-1,1,1,1)
or you can refer to the output directly:
enter image description here
for more information about these two formulas, you can refer to the help of excel of google.

Copy rows from multiple sheets into one sheet based on a cell value

I want to copy rows from multiple sheets into one combined sheet based on a cell valve.
Details:
I have multiple project numbers that are tabs (2016-010-082, 2016-010-086, 2016-060-015... etc). In column E on each of these sheets I have a dropdown box where you can pick "yes" or "no" based on if this task is completed or not. I would like where all the tasks that are "no", the row will copy over into a single sheet so I can see all the tasks for all the projects that are not yet completed.
When the "no" is changed to a "yes" on the specific project tab, I would like the row to be removed from the compiled sheet as well.
I would also like cells D2 and D3 from each show up in rows A and B of the compiled sheet as that is standard information for each tab that I will need to see on the compiled sheet so I know which project number it is and the project name.
Sample image.
This is what I thought would work for one sheet.
Sub Test()
For Each Cell In Sheets(1).Range("E:E")
If Cell.Value = "NO" Then
matchRow = Cell.Row
Rows(matchRow & ":" & matchRow).Select
Selection.Copy
Sheets("Sheet2").Select
ActiveSheet.Rows(matchRow).Select
ActiveSheet.Paste
Sheets("Sheet1").Select
End If
Next
End Sub
For the first part, where you want a list of just those facilities with "No", you can use this array formula (enter with CTRL+SHIFT+ENTER):
=INDEX(B$2:B$4,SMALL(IF($D$2:$D$4="Yes",ROW(B$2:B$4)-ROW(B$2)+1),ROWS(B$2:B2)))
Assuming your data is laid out like this:
Then, wrap IfError([formula],"") around that to hide the #NUM errors. As you change "No" in "Completed" to "Yes", the list on the right will update.

How to find a cell with any value and then find the next value that is different in VBA

I have a list of data with various different columns.
What I am trying to achieve is for excel to look through one of the columns and find the first value that occurs, copy that cell and then paste it in another cell. The data starts in row three (Rows 1 and 2 are headers). Let's say I want to paste the first value in G3.
Then, I need excel to find the next value that is different from the first in the same column and perform the same action as before: copy the value, and paste it in the next row in column G.
I have tried coding this but I'm not getting anywhere with it, and I haven't found a way to find a cell and then the next cell if the value within the cell is not defined (as the .Find function requires a value to search for). I know how to code the copy/paste functions but I cannot figure out how to get it to find the cell in the first place.
Any help would be much appreciated. Many thanks in advance.
In this sample, we are looking for values in column A:
Sub FillG()
Dim i As Long
i = Rows.Count
Range("A3:A" & i).Copy Range("G3:G" & i)
ActiveSheet.Range("G3:G" & i).RemoveDuplicates Columns:=1, Header:=xlNo
If Range("G3").Value = "" Then Range("G3").Delete Shift:=xlUp
End Sub
The method is to copy all of column A to G, then remove duplicates, then remove an empty in G3 if it exists.:

Excel 2007 - Formula changes to #REF

So I've got this Workbook which contains a lot of data. And I've got this one sheet which basically copies the data based on certain conditions.
Each cell in each row looks like this (the last specified cell is the one where the formula is in):
=IF(Numbers1!E2<>0;Numbers1!A2;"")
=IF(Numbers1!E3<>0;Numbers1!A3;"")
=IF(Numbers1!E4<>0;Numbers1!A4;"")
=IF(Numbers1!E2<>0;Numbers1!B2;"")
=IF(Numbers1!E3<>0;Numbers1!B3;"")
=IF(Numbers1!E4<>0;Numbers1!B4;"")
So the formula in cell A2 is the first one, formula in A3 is the second line etc.
I want to copy the value from the same column and row from the sheet Numbers1, IF the value in the same row of column E is not 0. This seems to be working just fine.
But, when I update the data in Numbers1 sheet, the formulas are all of a sudden invalid and the formula now looks like this:
=IF(Numbers1!#REF!<>0;Numbers1!#REF!;"")
Each formula in each cells look identical to the formula above. And I can't have that, why can't Excel just keep the formula as it is without "helping" me?
Since you may be better off using a macro to rewrite your formulas, here are the basics:
Sub RewriteFormulas()
Dim row, col As Integer
row = 1 'row you want your target formulas to be on
For row = 1 To 60
For col = 1 To 13
ActiveSheet.Cells(row, col).Formula = "=IF(Numbers1!" & Cells(row,col).Address & "<>0,Numbers1!" & Cells(row+2,col).Adddress & ","""")"
Next row
Next col
End Sub
You can play around with using different sheets (or different workbooks) instead of just ActiveSheet so you can have 1 workbook that stores the macro and alters data in whatever workbooks provide your updated datasets.
Hope that helps...

i want to merge cells with vba

im new to this and i want to ask one question if anyone can help.
im working with excel 2003 and what i want is that i have one column which at cell A1 i have name and in cell A2 i have surname, in cell A3 i have name and in cell A4 i have surname and so on.
what i want to do is to merge cell A1 with A2 and then A3 with A4 and so on.
its about 3000 rows.
is there any vba code to do it automatically?
You don't really need a VBA program for this, just use Excel formulas as so:
In the column next to your names fill a sequential list of numbers starting with 1. So this should have 1, 2, 3, 4 and so forth. You can go to the bottom of your A column, but half-way will be fine. TO do this quickly, type a 1 in the first row (B1), then in the second (B2) add a formula: =B1+1. Then copy this formula all the way down B column to the extent of the A column. The trick here is to go to B2, select Edit/Copy, then move to A column and Ctrl-Down, move to the B column and then use Shift+Ctrl-Up, then paste.
In C column you will put a formula to merge two cells together from A column, as so:
=INDEX($A$1:$A$<ENDROW>,(B1-1)*2+1,1)&" "&INDEX($A$1:$A$<ENDROW>,(B1-1)*2+2,1)
Where <ENDROW> is the last row in A.
Copy this formula down at least half the rows of the A column. If you go beyond this, you will get #REF! error; just clear those cells.
Now you want to copy the formulas in C to another column, say D. Select them, use Edit/Copy, then move to D1 and right-click and choose "Paste special". In the dialog click on "Values" and then "OK". Now you have a column with the values of every two rows concatenated together.
to do this in vba:
Sub nameMerger()
Range("A1").Select
While ActiveCell.Value <> vbNullString
ActiveCell.Value = ActiveCell.Value & " " & ActiveCell.Offset(0, 1).Value
ActiveCell.Offset(0, 1).EntireColumn.Delete
ActiveCell.Offset(0, 1).Select
Wend
End Sub
Tested and works

Resources