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

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

Related

Loop through column values from one sheet and paste COUNTIF value from another column into another sheet

I have two sheets in an Excel file and need to perform a COUNTIF formula from one sheet and paste the respective information in another sheet. The original sheet just has the type in 1st column with an empty 2nd column. I am trying to loop through the Type from Sheet 1, in each increment loop through the Type from Sheet 2, and past the Count of column 2 from Sheet 2 into Column 2 of sheet 1.
My current VBA code is as follows:
Sub TestOE()
'For loop to go until end of filled cells in 1st column of each sheet
a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
'Loop
For i = 2 To a
For j = 2 To b
If Worksheets("Sheet1").Cells(i, 1).Value = Worksheets("Sheet2").Cells(j, 1).Value Then
Worksheets("Sheet1").Cells(i, 2).Value = Application.WorksheetFunction.CountIf(Range("B:B"), 1)
End If
Next j
Next i
End Sub
This code is only pasting 0's in the desired outcome on Sheet 1.
Sheet to extract information from
Sheet to paste information in
Desired Outcome in destination sheet
You can simply use sumif function to sum the values based on criteria.
here is the formula
=COUNTIF(Sheet1!$A$2:$A$20,Sheet2!A2)
if you want to sum the col B then
=SUMIF(Sheet1!$A$2:$A$20,Sheet2!A2,Sheet1!$B$2:$B$20)
In a few steps, you can accomplish what you want without VBA, and just use a pivot table. Just do as follows.
Select your data set, including the header.
Click on insert tab, then PivotTable. See example for Office 365
Since you want a different worksheet, set PivotTable to be "New Worksheet" See example.
You'll need to drag the TYPE field into the rows, and binary into the values. CountIF is the same as summing binary, so you can leave as sum. See Example
And you'll have an output nearly identical to what you're looking for:

How to compare two columns from different sheets and put a value in another sheet? vba

I have 3 worksheets: WUR, Homologation and read.
I want to read column 1 from "WUR", when i find first value i want to compare with first column from "Homologation", if the values is the same then put this values in firm column in worksheet 3, "Read". This code is from Button found in Worksheet 3.
I try this:
Sub Read_Click()
Dim x As Integer
Application.ScreenUpdating = False
NumRows = ActiveWorkbook.Worksheets("WUR").Range("A1", ActiveWorkbook.Worksheets("Homologation").Range("A1").End(xlDown)).Rows.Count
Range("A1").Select
For x = 1 To NumRows
Next
End Sub
With my code i try to find first values from the first column in "WUR" but my code work in Sheet 3, not in first WorkSheet where i want.
It might be because in
Range("A1").Select
you don't specify the worksheet where A1 has to be selected. If you therefore have currently another worksheet open, the selection will be made there. Try defining it like you did the line above
ActiveWorkbook.Worksheets("WUR").Range("A1").Select

Copy rows based on cell value and paste on a new sheet

Check This
I need a help. I want to copy whole cell from A sheet name "Components" only if value in Column C is > 0 to a new Sheet name "Load list"
Can someone please give me the macro code for this?
on your new sheet you can add this condition the cell or range of cells:
=IF(Components!C5>0,Components!A5)
where C5 has thevalue to compare, and A5 has the value copy if the condition happens.
Right in my swing!
The formula given by #sweetkaos will work fine, in case you want to replicate the data as it is with blanks where data is not found.
I will imagine a slightly more complicated situation. I am assuming you want just one line in the next format as is shown in your image.
Also conveniently assuming the following:
a. both sheets have fixed start points for the lists
b. 2 column lists - to be copied and pasted, with second column having value
c. Continuous, without break source list
d. basic knowledge of vba, so you can restructure the code
Here is the code. Do try to understand it line by line. Happy Excelling!
Sub populateLoadList()
'declaring range type variables
Dim rngStartFirstList As Range, rngStartLoadList As Range
'setting values to the range variables
'you must change the names of the sheets and A1 to the correct starts of your two lists
Set rngStartFirstList = Worksheets("Name_of_your_fist_sheet").Range("A1")
Set rngStartLoadList = Worksheets("Name_of_your_second_sheet").Range("A1")
Do While rngStartFirstList.Value <> ""
If rngStartFirstList.Offset(1, 0).Value < 0 Then
Range(rngStartFirstList, rngStartFirstList.Offset(0, 1)).Copy
rngStartLoadList.PasteSpecial xlPasteValues
Application.CutCopyMode = False
Set rngStartLoadList = rngStartLoadList.Offset(1, 0)
End If
Set rngStartFirstList = rngStartFirstList.Offset(1, 0)
Loop
End Sub
Basically what i want is ... if Value on C is >0 i want whole column 10 copied to that new sheet .... not only that cell

Excel, conditional formatting column base on comparing two columns

I have excel worksheet, with two sheets. First one (Worksheet 1) is big table (about 2000 rows), and in second (Worksheet2) I have only about 20 rows.
I need to check, if string in first column in each row in worksheet 1 is already in Worksheet 2, if so, make it green.
Thanks for solutions, both EXCEL or VBA will be appreciated :)
In worksheet 1 you need to create a new column (say column AA) which uses the formula VLOOKUP to find the same data (if it exists) in the other sheet. If is is not found VLOOKUP will return and error #N/A. So you formula will look soemthing like:
IF(ISERROR(VLOOKUP(A2, 'Sheet2'!$SA$1:$X$9999, 3, false)),"NOT FOUND","FOUND")
You would then add conditional formatting to sheet1 column A that references the value in your new column (AA) and sets the colour accordingly.
If you don't know how to use VLOOKUP or get the conditional formatting to work, there are plenty of resource on the internet to help you learn these. eg youtube channel ExcelisFun, WiseOwl.co.uk.
I hope this helps.
PS You do not need to use VBA to do this! In fact using a foumula can be preferable as if a single value changes it can cause the colours to be changed.
Harvey
If you want to use VBA, try as follow:
Public Sub findDuplicate()
Dim sh1row, sh2Row As Long
For sh1row = 1 To 20 'loop all row from Sheet2 (if more than 20, modify it)
For sh2Row = 1 To 2000 'loop all row from Sheet1 (if more than 2000, modify it)
'If A cell are equal
If Sheets("Sheet1").Range("A" & sh2Row) = Sheets("Sheet2").Range("A" & sh1row) Then
'Change background color to green.
Sheets("Sheet1").Range("A" & sh2Row).Interior.Color = RGB(0, 255, 0)
End If
Next sh2Row
Next sh1row
End Sub

Excel: Use values in a sheet as index to list in a different sheet and replace values in the first sheet

I have an XL file with some data to be manipulated. I think I will need to use a VB script to do this - but perhaps there is a simpler way with a formula. Just the same, could someone point out BOTH ways of achieving the following?
I have a column of numeric values (ID) in Sheet 1.
I want to use each ID as an index to lookup a list in Sheet 2.
Sheet 2 has two columns
First column is the index and Second column is the Text String
e.g.
1 Apple
2 Orange
3 Pear
What I want is to replace the column of IDs in sheet 1 with the looked up text string from Sheet 2!
Thats all...
Please help!
Not a tough situation there. Here are some solutions...
With VBA:
I know you said you're a little new with VB so I tried to explain each line as I went along. Also, the code is free-handed so forgive me if I left an error in there somewhere.
Sub replaceData()
dim i as integer, j as integer 'These are just some variables we'll use later.
dim sheetOne as worksheet, sheetTwo as worksheet, myWb as workbook
dim myData as string, myId as string
set myWB = excel.activeworkbook 'These three lines set your workbook/sheet variables.
set sheetOne = myWB.worksheets("Old Data")
set sheetTwo = myWB.worksheets("New Data")
for i = 1 to sheetTwo.usedrange.rows.count 'This loops through the rows on your second sheet.
myId = sheetTwo.cells(i,1).value 'This assigns the value for your id and the data on your second sheet.
myData = sheetTwo.cells(i,2).value
for j = 1 to sheetOne.usedrange.rows.count 'This loops through the rows on your first sheet.
if sheetOne.cells(j,1).value = myId then 'This checks each row for a matching id value.
sheetOne.cells(j,1).value = myData 'This replaces that id with the data we got from the second sheet.
end if
next j
next i
end sub
With an Excel formula:
Place the following formula in cell C1 of the first worksheet (the
sheet with the IDs you will be replacing). **Note that you will
have to replace the "InsertSheetTwoNameHere" portion with the name
of your second sheet (don't remove those single quotes though). Also
note you will need to replace the "1000" with the number of the last
used row in sheet two.
=vlookup(A1,’InsertSheetTwoNameHere’!$A$1:$B$1000,2,FALSE)
Next simply drag the handle on the cell that makes it copy itself
(whatever the heck it's called) all the way down to the end of your
range.
Next, copy those cells and then paste them over the IDs using the
Values Only setting.
Hope this helps and good luck.