I want to create a macro which Compares data between the sheets.
I have data in the Sheet1 under Column A (Size of row / length is not constant) which needs to be compared with the data available in the Sheet2 under Column A. If that particular cell is matching then I would require the data next to the column A in Sheet1 (i.e. Column B in Sheet1) to be pasted in the column C (Sheet1). I'm trying to write a macro using the If and Loop but I fail and I forgot to save the macro I wrote.
When I use INDEX MATCH Excel becomes slow. Also I got up a new criterion now regards to my data. If column A in first sheet is matched with column A in second sheet then I want the column B to be validated with that off in second sheet and if both the conditions are satisfied then I want a value in column C.
I believe this complications can be sorted with macros loop or For each Next. I'm not sure about it.
Please sort it for me.
You might need to modify this slightly to work with your data but it should do the trick(based on my understanding of your problem).
Sub compare()
Dim i As Integer
i = 1
For Each cell1 In Sheet1.Range("A1:A100")
For Each cell2 In Sheet2.Range("A1:A100")
If cell1.Value = cell2.Value Then
Sheet3.Cells(i, 1).Value = cell1.Value
i = i + 1
End If
Next
Next
End Sub
Related
I am just starting out with Excel VBA and to be honest I am not that skilled in using normal Excel either.
I've got one sheet that has unique codes in column C, I also have another sheet that has unique codes in column A (except first rows as they've got column labels).
In case this unique code from sheet 1 is also found in the column in sheet 2, I want column J in the sheet1 to have that code value, otherwise, if it cannot be found, I want it to have #N/A.
Normally in Excel I would do this by selecting J2 and entering following function:
=VLOOKUP(C2,Sheet2!A:A,1,False)
Then I'd just double click the corner of the cell to populate all rows.
How do I do it in VBA? I wrote this code hoping it would do something:
Worksheets("Sheet1").Activate
ActiveSheet.Range("J:J").Value = Application.VLookup(C2,Worksheets("Sheet2").Range("A:A"),1,False)
However, this does not work. I just get #N/A for all cells in the J column. Any suggestions what I can do?
The following code will give you what you want. Note that you would only want to put the formula into rows where values actually exist in column C on sheet1.
Option Explicit
Sub InsertVlookup()
Dim LastRow As Long
LastRow = Sheet1.Cells(Rows.Count, 3).End(xlUp).Row
With Sheet1.Range("J2:J" & LastRow)
.FormulaR1C1 = "=VLOOKUP(RC[-7],Sheet2!C[-9],1,FALSE)"
.Value = .Value
End With
End Sub
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.
I have an Excel file with many sheets.
Each sheet contains some raw data in two columns. The first column is the item (name in text) and the second is the value (number).
I like to draw a graph which shows how one item evolves. So I have created a new sheet and started making references to all raw data sheets to the item in interrest.
Doing this manually takes a long time for two reasons:
a) There is many raw-data sheets
b) The item is not on the same row in all raw-data sheets
So I'm wondering if it is possible to make a macro that would fetch all the values. If I gave the macro the item, e.g. apples, the macro would search all raw data sheets, find the row containing item apples and copy the value to a new row in my graph-sheet.
Is it doable? And how would I start writing such a macro?
Note: I'm not asking for a complete solution. I'm asking for names of functions that would be useful. Example: Which function can I use to iterate over all raw data sheets, which function can I use to find the item in each raw data sheet and so on.
Updates after comments
Yes, the item names are always in the same column, i.e. A. The value is always in column B (in the same row). But the row number may change from sheet to sheet.
It is only the value I want to copy (i.e. from column B in the row where column A contains the item name I'm interrested in.
Consider:
Sub dural()
Dim Master As Worksheet, sh As Worksheet
Dim GotIt As Range, r As Range, K As Long
Set Master = Sheets("Master")
arr = Array("raw1", "raw2", "raw3")
K = 1
For Each a In arr
Set sh = Sheets(a)
Set r = sh.Range("A:A").Cells
Set GotIt = r.Find(What:="apples", after:=r(1))
If GotIt Is Nothing Then
Else
GotIt.Resize(1, 2).Copy Master.Cells(K, 1)
K = K + 1
End If
Next a
End Sub
Where arr is the array of sheet names of the raw data sheets and Master is the name of the summary worksheet.
I have some data in a worksheet in a single row (row 44) where the required data is in columns C,F,I,L and so on (i.e. data required every 3rd column starting from C).
This ends at column 'ET'
I need to extract this and paste it into another worksheet row where there are no column spaces.
I've looked around for solutions but its usually columns but this is data i need in one row.
Assuming this needs to be done even when the data in row 44 changes, you could do a macro. In a procedure, the following code could work as a guideline:
Public Sub copyover()
Dim c As Long
For c = 1 To 50
Worksheets("Sheet2").Cells(1, c).Value = _
Worksheets("Sheet1").Cells(44, c * 3).Value
Next
End Sub
Does this have to be a macro? Put this in the first destination cell on the other worksheet:
=INDEX(Sheet1!$C$44:$ET$44,1,3*(COLUMN(A1)-1)+1)
Then copy right
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...