Comparing and updating a sheet in Excel using VBA - vba

So I am a student currently doing a vacation job at a company and have been tasked with maintaining and updating a database. The database is created on an excel spreadsheet. Now the issue that I am having is that I am unable to run a comparison.
In one workbook I have 2 sheets. Sheet 1 has 6 columns and sheet 2 has only 4 columns. I want to compare column A in sheet 1 and sheet 2, and if they are the same sheet, let sheet 2 values become those in sheet 1. That I can do, however, if column A from sheet 1 does not equal column A from sheet 2 the entire row from sheet 2 must be copied into sheet 1.
If A1 = A2 Then
B1 = B2
C1 = C2
D1 = D2
Else
'add into first empty row in sheet 1.
Your help would greatly be appreciated. Thank you

Maybe this will help.
This code will compare the data form column a in sheet 2 to the data from column a in sheet 1 and if they are the same, the first 4 column values in that row will be copied.
sub test
application.screenupdating = false
For i = 1 to x 'number of rows you want to search in
For j = 2 to 4
If sheets("sheet1").range("A"&i).value = sheets("sheet2").range("A"&i).value
then sheets("sheet2").Cells(i,j).value = sheets("sheet1").Cells(i,j).value
next j
next i
end sub
Tested and worked

Related

How to fill cell with data from a separate worksheet in excel?

I am attempting to match 2 columns in two separate worksheets and then fill data from worksheet 2 in worksheet 1.
I need to match Column A(Worksheet 2) to Column D(Worksheet 1). Once Matched I need to fill Column F(Worksheet 1) with the data from Column B(Worksheet 2). Once data is populated I would also like to change the color of Column F(Worksheet 1) based on the data that is present. Worksheet 1
Worksheet 2
Put this in F2 On Sheet 1, update the sheet name Sheet2 to whatever your second sheet is named, then copy down:
=VLOOKUP($D2,Sheet2!$A:$B,2,FALSE)
Then you can apply conditional formatting to Column F on Sheet 1.
If you might have values in Sheet 1 that aren't in Sheet 2, this will handle the error:
=IFERROR(VLOOKUP($D2,Sheet2!$A:$B,2,FALSE),"Not Found!")
[Updated for additional question about spanning accross workbooks]
For another open workbook, use the following and replace [Book2] with the path of the second workbook or the name of an open workbook:
=IFERROR(VLOOKUP($D2,[Book2]Sheet2!$A:$B,2,FALSE),"Not Found!")
Also note, Excel will automatically build all of the references if you select them manually while building the formula in the formula bar: Excel Formulas Overview on MSDN
More information is needed to provide you with an exact code but here is a good start
'Assuming there are 10 rows in each worksheet
dim i as integer
dim j as integer
for i = 1 to 10
for j = 1 to 10
if sheet1.cells(i,4).value = sheet2.cells(j,1).value then
sheet1.cells(i,6).value = sheet1.cells(j,2).value
sheet1.cells(i,6).interior.color = vbyellow
end if
next j
next i
the color can also be controlled with the rgb function, simply replace the vbyellow in the above code:
For example rgb(255,204,255) will be a light pink

vba copy some specific cells when a cell matches value but cells sometime is same column

I am facing a problem that is when i use if else statement i can copy the value from table and paste to another sheet,
If the value matches that with same column c in table 1 and 2 (same as in the attached example link) it will just copy the table one match value cell to another sheet.
How can i copy the other matches value to another sheet?
Please view the example here. and Expect output and This is Problem output
actually now my only problem is when i use if else statement to do this it just copy the first condition that that match value like the column c first yellow colored value,and it will not copy the second cells that is colored match value in column c.thanks.
enter code here //Sub tab()
Dim a
Dim b
a = 2
For b = 3 To 25
If Sheet1.Cells(XX,XX)'max' = 5 And Sheet1.Cells(XX,XX)'min' = 2 Then
If Sheet1.Cells(XX,XX)'total' > Sheet1.Cells(XX,XX)'max' Or Sheet1.Cells(XX, XX)'TOTAL' < Sheet1.Cells(XX, XX)'min' Then
Sheet2.Cells(XX,XX) = Sheet1.Cells(XX,XX).Value
""
a= a + 1
End If
ElseIf Sheet1.Cells(xx, xx)'max' = 10 And Sheet1.Cells(xx,xx)'min' = 7 Then
If Sheet1.Cells(XX,XX)'total' > Sheet1.Cells(XX,XX)'max' Or Sheet1.Cells(XX, XX)'TOTAL' < Sheet1.Cells(XX, XX)'min' Then
Sheet2.Cells(XX,XX) = Sheet1.Cells(XX,XX).Value
""
a= a+ 1
End If
Next b
End Sub

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

Copy data in 1 row in Sheet 1 to changed data in 2 rows of Sheet 2

I am relatively new to VBA.
I have a 10 digit number (1234567890) in A1 of Sheet 1 and want to copy this data in A1 in sheet 2 with a concatenated value (-qwerty) and again in A2 in sheet 2 with another concatenated value (-abcdef). So the final values in A1 of Sheet 2 will be 1234567890-qwerty and in A2 of Sheet 2 will be 1234567890-abcdef
Similarly, I want to fill n rows of A column in sheet 1 to 2n rows of A column in Sheet 2.
Looking out for VBA help in excel for the same.
Thank you in advance.
I think no need a VBA
just write in excel's Sheet2 column A1, a formula
= Sheet1.A1 & "-qwerty"
and just write in excel's Sheet2 column A2, a formula
= Sheet1.A1 & "-abcdef"
or something like that
Direct value transfer with a string concatenation would seem to be the easiest.
sheet2.cells(1,1) = sheet1.cells(1, 1).value2 & "-qwerty"
sheet2.cells(2,1) = sheet1.cells(1, 1).value2 & "-abcdef"
Bulk transfer can be accomplished with a simple formula and more string concatenation.
with sheet2.cells(1,1).resize(99,1)
.formula = "='Sheet1'!A1&"-qwerty"
.value = .value
end with
After the formula has been filled, the value in the cell is reverted to the value from the formula (thereby removing the formula).

Compare two sheets and change value

I need to compare a sheet (which has 4k date) with another sheet and say 'yes' or 'no'.tried to use
ifSheets(j).Range("A2").Text = Sheets(1).Range("A2").Text Then
'Write 4 in sheet1 cell C6 when the two values are coinciding.
Sheet1.Range("C6") = 4
from site,
Comparing two text cells in different sheets
but couldn't succeed. tried for 4 days without success so finally thought to ask help
what i try to do is compare sheet 1 range A with a specific value/text in "B2" on sheet 2, sheet 1 range A has about 4k data. if it finds a match I need "B3" to show "Y", if it doesn't find a match "N".
idea is simple but I really need help please...
sub main
dim i as integer
for i = 1 to 4000
if sheet1.cells(i, 1) = sheet2.cells(2, 2) then
sheet2.cells(3,2 ) = "Y"
exit sub
end if
next i
sheet2.cells(3, 2) = "N"
end sub