Use V-Lookup to the cell number - vba

So I have been given the task of comparing to worksheets in excel and if there is a match replacing the data from one cell with another, for example, I have 2 columns in 2 excel sheets, ID and name, I want to compare the IDs in sheet 1 with the IDs in sheet two and if it finds a match update the name linked with that ID.
Sheet 1
ID Name
1 Thomas
2 Jerry
Sheet 2
ID Name
3 Spike
1 Tom
So in the example above, I need the code to see that ID 1 is in both sheets and change the name in sheet 1 to match sheet 2, so that it looks like this:
ID Name
1 Tom
2 Jerry
I'm trying to use the Vlookup, which has allowed me to find out if it is existing or not, but then I don't know how to change the cell to match the existing one, here is my code so far:
Range("C2").Select
ActiveCell.Formula = _
"=IF(ISNA(VLOOKUP([#[ID]], 'Sheet2'!A:B,1,FALSE)), ""New"", ""Existing"")"
Do While Len(Range("A" & r).Formula) > 0
If (Range("C" & r).Value = "Existing") Then
Sheets("Sheet2").Range("A" & r & ":B" & r).Copy _
Destination:=Sheets("sheet1").Range("A" & r & ":B" & r)
Else
End If
I need to be able to get the cell number from the Vlookup so that I can use it in the if statement to pull in the correct data. There may be a simpler way to do this, if so I am open to changing everything.
Any help would be much appreciated.

I don't think there is a need of VBA for this task. As the names in "Sheet1" has to be updated by looking into the data in "Sheet2". Following formula can be used to find the updated names (by looking into data from the "Sheet2").
=IFERROR(VLOOKUP(A2,Sheet2!A2:B3,2,FALSE),B2)
IFERROR is used so as to retain the names which were not found in "Sheet2" data. Have a look at following screenshot for the example.

Easiest way is on a third sheet create 3 headings in row A: ID, Name and Source. Copy your data from sheet 1 into the first two columns and fill the third column with the text sheet 1. Do the same for sheet 2 but paste it below your sheet 1 data. You should end up with something like this:
ID Name Source
1 Thomas Sheet 1
2 Jerry Sheet 1
3 Spike Sheet 2
1 Tom Sheet 2
Then just create a pivot table from this data with source as the column, name as the row and a count of name for the data. It should look something like this:
Name Sheet 1 Sheet 2 Total
Thomas 1 0 1
Jerry 1 0 1
Spike 0 1 1
Tom 0 1 1
You can then sort on the Total column to find the duplicates which will have a total of two.
One other advantage of using this method is that it is quite easy to expand it to compare three or more lists.

Related

Looking for vlookup or VBA any formula

I have 2 different worksheet as below
1st worksheet as --
Column A
Column B
Name 1
Done
Name 2
Pending
Name 3
Working
Name 4
Pending
Name 1
working
Name 5
Name 1
Name 6
Done
2nd sheet - output
Column A
Column B
Name 1
Name 1
Name 2
Name 3
Name 5
Name 5
Name 6
on 2nd sheet i am looking for output on column B from 1st worksheet.
if next to Column A if user has updated comment then ignore
if any user has left any one comment blank than in 2nd sheet same user name to display
Thanks
tried by joining column A&B in worksheet 3 and then using Vlookup on 2nd sheet from worksheet 3.
this one solved the problem but i have like 20 worksheet like worksheet 1 which make excel very slow.
how can i do it directly from worksheet 1 to worksheet 2
Any formula or VBA will do so that excel dont get slow

Set the value from another sheet. With a test of condition. Excel

I have a table with two sheets. On the first (1) the name is written, on the second (2) Imin and numerical meaning are written, the same name can be written several times. There are also names that are not present on the sheet (1).
Sheet 1.
A
Column A
Column B
Vova--
Ben
Sheet 2.
Column A
Column B
Ben +
2
Timmy
1
Ben T
4
Vova
6
How to put in the first sheet the sum of the values from the sheet (2)
I tried:
=IF(ColA2 = $Sheet2 ColA2; $Sheet2 ColB2)
=VLOOKUP(A1;$Sheet2.B2:B4;2)
=SUMIF($'Sheet2'A1:A4;A1;$'Sheet2'ColB1:ColB4)
Not work
Does not summarize the value
Looking for accurate coincidences
Names can end in different ways ...
Ben = Ben T
Required:
Column A
Column B
Vova--
6
Ben
3
Need help please.
I found an answer. I need to use LEFTB
=SUMIF($'Sheet2'A1:A4;LEFTB(A1;4)&"*";$'Sheet2'ColB1:ColB4)
or
=SUMPRODUCT((LEFTB($Sheet2!$A$1:$A$4;3)=A2)*$Sheet2!B2:B5)

Compare two sheets using ID column

I am looking to do a comparison of 2 sheets in a workbook in Excel 2013. Due to the number of records VLOOKUP and other formulas have been slow so I thought I would try VB to see if this was a quicker solution.
What I would like to do is compare each record by ID and highlight and mismatches in red. Due to the column names and position being different, I would also like to do the cell comparison on each record by specifying the column names to compare against. Finally, I would like to total the mismatches for each column into a 3rd sheet.
For example:
Sheet 1:
ID Col1 Col2 Col3 Col4
1 1 1 1 1
2 2 2 2 1
3 3 3 3 3
4 4 4 4 4
Sheet 2:
DBID Col1 Col2 Field Col3
1 1 1 1 1
2 2 2 2 2
4 4 4 4 4
3 3 3 3 3
So in the above example I would only like to Col4 compared with Field column and only see the Field column for ID 2 highlighted as an error with ID records 3 and 4 ignored because they match and are just in different positions in the file.
I would normally sort on ID instead of picking out a particular ID, but conscious that there could be records missing which means the data would be misaligned.
At the moment I have found this code which will highlight the mismatches in red, but matches cell by cell without taking into consideration that the columns and records might not be in the same order.
Sub RunCompare() 'Call the compareSheets routine Call compareSheets("Sheet1", "Sheet2") End Sub
Sub compareSheets(shtBefore As String, shtAfter As String) Dim mycell As Range Dim mydiffs As Integer 'If current cell is not a date then proceed (else skip and go to next), then 'if not same as corresponding cell in sheet After, 'mark as yellow and repeat until entire range is used For Each mycell In ActiveWorkbook.Worksheets(shtAfter).UsedRange If Not IsDate(mycell) Then
If Not mycell.Value = ActiveWorkbook.Worksheets(shtBefore).Cells(mycell.Row, mycell.Column).Value Then
mycell.Interior.Color = vbRed
mydiffs = mydiffs + 1
End If End If Next 'Display a message box stating the number of differences found MsgBox mydiffs & " differences found", vbInformation ActiveWorkbook.Sheets(shtAfter).Select End Sub
I am assuming that the ID is unique.
You have basically two solutions, with and without macro.
With Macro Logic can be as follows :
Get the first (Unique) column of first sheet
Loop through the first (Unique) column and find the matching row in second sheet
Compare between cells in that row with the first row of first sheet
Repeat the same steps for all rows
Also do a check to see if both sheets have same number of rows and columns; and no rows are duplicated.
Non Macro Solution :
Use VLookup Function to lookup for the row matching the value and do an equal comparison formula in a new sheet as
=IF(Sheet1!B1=VLOOKUP(Sheet1!A1,Sheet2!A:Z,2,FALSE),"Same","Different")
Note that you will need to increment the row number and column name I have highlighted in first column of the third (Answer) sheet.
Once you have values, you can use conditional formatting to highlight Different to Red

Using VBA to create a grid from 3 spreadsheets with the row, column, and values

I have three excel spreadsheets. The first has the values that are to be assigned to a new excel spreadsheet. The second has the column that the data belongs in. The third has the row that the data belongs in.
It looks something like this:
Data Value:
1 5 7 9
2 2 6 8
Column Number:
1 2 3 1
2 3 1 2
Row Number:
1 2 3 2
4 4 3 1
How can I combine all of this information to create a single spreadsheet that contains the values in a format like this:
Column
1 2 3
Row
1 1 8 0
2 9 5 0
3 6 0 7
4 0 2 2
I have tried to do it by using loops in vba, but I am a beginner and I am having some difficulty.
I know that I need to use a loop that checks the row and column that the data is supposed to be in against the row and column for each iteration. I am just not sure how to go about doing that.
Assuming these are different sheets in the same workbook (if not - create a new workbook and copy the sheets over). I assume that on each sheet, the same range of cells is used (for example A1:D2 in all 3 cases with the values on the first sheet, the column numbers in the second and the row numbers in the third). You can dispense with VBA entirely at the expense of using some complicate formulas (inspired by this excellent article: http://exceluser.com/blog/1043/how-to-create-two-dimensional-lookups-in-excel-formulas.html )
Step 1. Add a fourth sheet and in A1 add the formula
=CONCATENATE(Sheet3!A1,"_",Sheet2!A1)
and copy it over the appropriate range (e.g. A1:D2). This will give you things like 2_1 which tell you that the corresponding entry in sheet 1 belongs in row 2 column 1. Name this range "location" (formula tab - define names option)
Step 2 - Decide where you want to hold the data (I'm assuming it is in sheet 4 for simplicity) And add the row numbers (1-4) and the column numbers (1-3) as labels. In my case the row labels are in A5:A8 and the column labels in B4:C4 (see the screenshot below). Then in the upper left corner of the values to be filled in (B5 in my case) enter the following formula (suitably adjusted to match your ranges):
=IFERROR(INDIRECT(ADDRESS(SUMPRODUCT(ROW(location)*(location = CONCATENATE($A5, "_",B$4))),SUMPRODUCT(COLUMN(location)*(location = CONCATENATE($A5, "_",B$4))),,,"Sheet1")),0)
and copy it over the intended range. Be careful with the dollar signs - this formula mixes row absolute and column absolute references in an essential way. Somewhat oddly, it actually works:

VBA for checking rows and returning cells that match criteria to another sheet

I’m struggling trying to develop an automated solution for the following challenge (ie apart from a button press, no user intervention):
I have a master ‘sheet that contains Accrual figures in monthly columns and an associated Receipt Number for this in a column immediately to the right. The Receipt columns have the month in the form: ‘Feb’, ‘Jun’, ‘Dec’ at the top.
I need to return a variety of cell data to another summary ‘sheet in the workbook from each row that matches the following criteria:
Identify the Receipt column based on a match of the month with an entered month in cell ‘x’ in the summary ‘sheet, eg if I type ‘Mar’, find the column headed ‘Mar’ in the master ’sheet.
Ignoring blank rows (ie there are data in Column A), if the cell in the first row of the Receipt column is blank, identify the row, select 6 different cells and return the contents to 6 specific cells in the first blank row (from a given row number) in the summary ‘sheet – then move onto the next row in the column and continue this process until the end (or a row limit). NB: the 'blank' cell will have a fill colour (conditionally formatted).
If the cell contains data, move to the next row down and continue the process.
A VBA routine I can attach to an onscreen button to update the results each time would do the job nicely. Although I've picked up bits of really useful code here, eg for checking each row for given conditions, I can’t get my head around a working solution to include returning the relevant cells to the summary 'sheet...
Any and all feedback greatly appreciated.
(Sorry - can't post images / screenshots yet...)
Update
Thanks for the quick response, and apols I couldn't append/paste-in a sample of the spreadsheet - apparently I need 10 points... :)
Slightly Different summary sheet (Sheet1). The columns are: Month (Col A), then 4 cells (TBD) to be returned to cols B-E, then Receipt No (Col F). The data come from each identified row in the master sheet (Sheet4) and are placed in the next available blank row in Sheet1. I can mod any examples given to match the actual positions.
I have the criteria for selecting the row (this is from a loop I've got working to identify the row, but using a fixed column reference [26 - Col Z] for a specific month):
If Not Cells(TheRow, 3).Value = "" And Cells(TheRow, 26).Value = "" And Cells(TheRow, 6).Value < Cells(TheRow, 25).Value Then... (do the other bit I'm stuck on)
Hope this is a bit clearer. Appreciate your help.
Can't add a comment, or chat (!) so further update:
Thanks Tony
The month column in the summary is to confirm the month reported on, as this will change when the user overwrites the source cell for the month they wish to parse the master sheet with. The person this data goes to will get these 7 columns and nothing else.
The mechanism has to find the Receipt col (month), then identify that row entry is blank AND col 3 on that row is not blank AND that the Accrual value [Rng.Column - 1] is greater than the PO value (Col 4) on that row. Once these criteria are established, the Receipt, Accrual, PO and 3 other data are selected, copied and pasted into the summary sheet on the next available blank row.
The master sheet has Accrual and Receipt for each month, so there is only one column to identify.
Hope this helps...
I am struggling to visualise you worksheets. I see the master sheet as something like:
... |Accrual| Jan|Accrual| Feb|Accrual| Mar|Accrual| Apr| ...
... | 1.23|A12 | | | 23.67|A14 | | |
... | | | 56.78|C34 | 178.12|C56 | | |
... | 2.34|B678 | | | | | 123.82|B821 |
... | | | | | 96.52|D56 | | |
Somewhere within the Summary sheet there is a list of months of interest.
I need a variable:
Dim MonOfInt As String
and a loop within which a month of interest will be loaded from the worksheet into this variable:
With Sheets("Summary")
MonOfInt = .Cells(R, C).Value
End With
The following is a possible skeleton for the code to move values for the month of interest:
Dim ColCrnt As Long
Dim Rng As Range
Dim RowCrnt As Long
Dim RowLast As Long
:
With Sheets("Master")
' Look for month of interest in row 1
Set Rng = .Rows(1).Find(MonOfInt)
End With
If Rng Is Nothing Then
' No columns for this month in Master
Else
' Accrual and Receipt columns for this month found
ColCrnt = Rng.Column - 1 ' Accrual column for month
' Find last used row in Accrual column
RowLast = .Cells(Rows.Count, ColCrnt).End(xlUp).Row
For RowCrnt = 2 To RowLast
' Code to extract values from Master and move to Summary here
Next
End If
End With
If you can add information to your question to confirm my visualisation and to give more information about (1) how to detect is a row is blank (other than testing the whole row which is an option) and (2) the source and destination of the six cells then I will try to pad out this answer.