Column A is inventory that I should have. Column B is inventory I received.
I am missing a lot of inventory and have had to manually do this. What I want is the numbers in each column to match up & the numbers that are not present in B that are present in A to add a space in column B to show thats it's missing:
What I have:
What I want:
This is a pretty lightweight VBA routine that will do what you need:
Sub matchCells()
Dim colARange As Range
Dim colACell As Range
'Change this to your Column A range size
Set colARange = Sheet1.Range("A1:A5000")
'Iterate through each cell in your Column A Range
For Each colACell In colARange.Cells
'Check if the cell in Column A doesn't match the cell in Column B
If Not (colACell.Value = colACell.Offset(0, 1).Value) Then
'It doesn't match so shift Column B's cell down one
colACell.Offset(0, 1).Insert Shift:=xlShiftDown
End If
'Now we loop again and we will continue shifting Column B down
' a cell until it matches.
Next
End Sub
You can try below formula on Column C... Should give you the result you need in Column C.
=IF(ISNA(VLOOKUP(A1,$B$1:$B$10,1,FALSE)),"",VLOOKUP(A1,$B$1:$B$10,1,FALSE))
$B$1:$B$10 is the Range of data (no of rows in column B)
Related
Consider the following table:
I have a series of blank cells with missing data. From this missing data I only have the year in the next column. I need to fill any blank cells with a standard day/month of 30/06. The year of each cell however needs to be the year in the next column. The attached file shows how my data is arranged. So at cell B 2091, the date shall be 30/06/2011 while for cell B 2098 the date shall be 30/06/2018 and at cell B 2100 the date shall be 30/06/2008.
Filter on the blank cells in column B. Then, in the topmost cell (which I'll assume to be B1 but will likely be different), enter a formula similar to the following and fill down
=DATE(C1,6,30)
where the row number in C1 is the same as your first row of data.
You can achieve this with a helper column (any blank column in the same worksheet where you need the dates). In that column enter this formula in the first cell (here in row 2) and copy down.
=IF(ISBLANK(B2),DATE(C2,6,30),B2)
Then copy the Values from the helper column to the date column and delete the helper.
Below is a small macro that is doing the same job. It needs no helper column and over-writes your existing blanks. Before you run it make sure to check the values of the 2 constants at the top and the name of the worksheet (especially the latter!) against your requirements.
Sub WriteStandardDate()
'293
Const FirstDataRow As Long = 2 'change to suit
Const DateClm As Long = 2 'change to suit
' year column must be adjacent to DateClm
Dim R As Long
Dim Arr As Variant
Dim Rng As Range
With Worksheets("Sheet1") ' change name as required
Set Rng = .Range(.Cells(FirstDataRow, DateClm), _
.Cells(.Rows.Count, DateClm).End(xlUp)) _
.Resize(, 2)
Arr = Rng.Value
For R = 1 To UBound(Arr)
If IsEmpty(Arr(R, 1)) Then
Arr(R, 1) = DateSerial(Arr(R, 2), 6, 30)
End If
Next R
Rng.Value = Arr
End With
End Sub
Update: I used the formula suggested by Variatus: =IF(ISBLANK(B2),DATE(C2,6,30),B2) and worked fine through a helper column. There was no need to copy / paste the new dates into the Dates column. I just used the helper column as the new Dates column since full dates from the original column were not changed and got inserted in the helper column thanks to the IFBLANK portion of the formula. Thanks.
I am new to VBA Excel and wondering if you can help.
I have tried typing something similar to this =IF(C10,(ROW(A10)-ROW(A$9)),"") and it works. However, the downside to this is you have to enter this formula into every cell that you want to auto populate. I am trying to find a macro code in Excel so that cells will auto number 1,2,3,etc. whenever the adjacent column contains data?
For example, when a user enters data into B1, A1 will automatically be populated to 1. Then when users enters data into B2, A2 will automatically be populated to 2 and so on. Then when users delete data from B column, adjacent column in A will not contain a number.
Enter this into A1 based on what you said:
=if(isblank(B1),"",row())
If you want this in VBA you can use the following, but this assumes your data starts at Row 1. If it you want the numbering at Row 2 to start as 1, just add "-1" after the RngA part.
Sub Serial()
Dim RngA As Long, LastRow as Long 'Declares the variables used
LastRow = ActiveSheet.Cells(Cells.Rows.Count, "B").End(xlUp).Row 'This finds the last row in Column B where the loop will end.
For RngA = 1 To LastRow ' This loops from the first row to the last cell filled in column B
If Cells(RngA, 2) <> "" Then ' If Column B is blank, skip this row
Cells(RngA, 1).Value = RngA 'Column B was not blank, so put the row number in column A
End If
Next
End Sub
I've looked high and low for this and can't find a solution. Plenty with formulas, but it must be in VBA.
I have a range of text in column A (A2:A100) and dates in column B (B2:B100), with sequential calendar dates in row 1 (C1:Z1). For every cell in the matrix/table, if the date in column B is the same as the date in row 1, the text in corresponding column A must be displayed. I can't use a VLOOKUP or MATCH formula, as the columns are narrow and don't display the full text unwrapped because of the formula in the adjacent cell. Thanks for any guidance. In the below, N2 must be "Electrician chasing", since N1 is the same as B2.
I think you want this (and you got your columns A and B the wrong way round)
Sub LoopAndDisplay
Dim r as range
For each r in range("C2:Z200")
If cells(1,r.column) = cells(r.row,2) then 'if date above matches column B then
r = cells(r.row,1) 'display contents of column A
End If
Next R
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 three columns, A, B and C:
Column A contains names, NAME1, NAME2, etc.
Column B contains only the values "YES" or "NO".
Column C is suppose to contain the names from column A that have value "YES" in column B.
I can say that as long as the value is "YES" in column B, copy the value from column A to column C. Very simple with:
C1=IF(B1="YES",A1,"")
But this will include blank cells, which I don't want to. So I guess I am looking for a way to copy all the names from column A with value "YES" in column B and paste them into column C skipping the blanks.
I did find a VBA project that colors all the cells within a column with a certain value. I am not sure how to edit this into what I need. Here is the code I came up with so far.
ISSUES
1) Runtime Error '1004' Application-defined or Object-defined error
2) Copying from Column A
3) Check and Remove Duplicates from NewRange
EDIT 1: Added comment rows into the code
EDIT 2: Change NewRange to be made from column A with Offset (untested due to runtime error)
EDIT 3: Code for copying form one sheet separated from code for pasting into another sheet
EDIT 4: Added correction from user #abahgat
EDIT 5: Remove duplicates
Sub RangeCopyPaste()
Dim cell As Range
Dim NewRange As Range
Dim MyCount As Long
MyCount = 1
'--> Loop through each cell in column B
'--> Add each cell in column A with value "YES" in column B to NewRange
For Each cell In Worksheets("Sheet1").Range("B1:B30")
If cell.Value = "YES" Then
If MyCount = 1 Then Set NewRange = cell.Offset(0,-1)
Set NewRange = Application.Union(NewRange, cell.Offset(0,-1))
MyCount = MyCount + 1
End If
Next cell
'--> Copy NewRange from inactive sheet into active sheet
NewRange.Copy Destination:=activesheet.Range("C1")
'--> Remove Duplicates
activesheet.Range("C1:C30").RemoveDuplicates
End Sub
Solution without VBA:
column C contains formulas like:
=COUNTIF(B$1:B1;"yes")
increase number in column C if this row has "yes" value in column B.
This value will by used in next step.
column D contains formulas like:
=INDEX(A:A;MATCH(ROW();C:C;0))
take value from:
table: an entire A row
row number: calculated by match function: find first occurance of row number (row number where we will place the value) in entire C column. 0 meens that we looking for exactly this number not an clossest.
to skip errors:
=IF(ISERROR(MATCH(ROW();C:C;0));"";INDEX(A:A;MATCH(ROW();C:C;0)))
easier can be writen:
=IFERROR(INDEX(A:A;MATCH(ROW();C:C;0));"")
and this means:
write the value from rule if this value is not an error or write empty string if the rule is an error
Just used a Andcondition on your If to avoid the empty cells
In C1, put then copy down =IF(AND(LEN(A1>0),B1="YES"),A1,NA()))
Select column C
Press F5
Special ... check Formulas and then tick Errors (see pic)
Delete the selected cells, to leave you with a shorter list of desired names in column C
This will do the trick:
Sub RangeCopyPaste()
Dim cell As Range
Dim NewRange As Range
Dim MyCount As Long
MyCount = 1
For Each cell In Worksheets("Sheet1").Range("B1:B30")
If cell.Value = "YES" Then
If MyCount = 1 Then Set NewRange = cell.Offset(0,-1)
Set NewRange = Application.Union(NewRange, cell.Offset(0,-1))
MyCount = MyCount + 1
End If
Next cell
NewRange.Copy Destination:=activesheet.Range("D1")
End Sub