Getting corresponding data from another sheet - vba

I'm rather new with excel and no good at VB, and couldn't find a way to solve the following:
I have one sheet which contains data about articles and another sheet which contains only the title of the article and the amount of times it is cited.
For example the first sheet contains data in these columns:
First Author Second Author Other Authors Publication Year Title Published In More Info
and the second in these:
Title Count
I want to copy all the data from the first sheet to the corresponding row on the second sheet (based on the article title). Some titles may appear twice on the first sheet, but should only be copied once to the second. Also, it would be good if all lines that were copied from the first sheet to the second would be highlighted so that I could see if there were any mistakes.
A VBA solution is also welcome.

No need for VBA. This is a simple INDEX-MATCH combination.
Set-up:
Sheet1:
Sheet2:
Enter the following formula to Sheet2!C2, drag down and right:
=INDEX(Sheet1!$A:$G,MATCH(Sheet2!$A2,Sheet1!$E:$E,0),MATCH(Sheet2!C$1,Sheet1!$A$1:$G$1,0))
Result:
Let us know if this helps.

The code below will give you an Idea how to match the cells. Assuming you have the title in column 10 in sheet1 and column 1 in sheet2
dim i as integer
dim j as integer
for i = 1 to 'number of rows in sheet1
for j = 1 to 'number of rows in sheet2
if sheet1.cells(i, 1) = sheet2.cells(j, 1) then
'do what ever you want with the matching records
end if
next j
next i

Related

Excel VBA: Concatenate lists with differing number of rows

So, this should be an interesting one...
I have 2 separate reports that provide customer satisfaction information on tickets (if I was able to get the data in 1 list, then this issue would not exist...). Each list provides information including, most importantly, the ticket number. One list provides a customer comment, while the other list provides a customer satisfaction score on a 1-5 scale.
A simple concatenate matching ticket numbers would be fairly easy, however, the database designer failed to make the customer comment field mandatory, so approximately 50% of the tickets have no comment.
I have both sheets with the past 60 days of information, however the one with scores is nearly double the size in row number. What I need to do is combine the lists using first the score sheet, since it will include every ticket, then adding in the comment sheet, matching the comments with their respective tickets.
I am not even sure how to start this code, and I am not looking for finished code, only suggestions on direction.
Pseudo code would be something like:
Clear useless columns in each sheet
Sort each sheet by ticket creation date (or ticket number)
Copy survey comment sheet into survey score sheet, leaving a blank column or two in the middle.
Assuming that column A and column G include the ticket numbers, sort so that the values match up, and the rows with no survey comment value are blank for column G
Any direction is much appreciated!
Something along the following will do what you want:
Sub foo()
LastRow = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row 'find how many rows on Sheet1
LastRow2 = Sheet2.Cells(Rows.Count, "A").End(xlUp).Row 'Find how many rows on Sheet2
For i = 2 To LastRow 'loop from row 2 to last row
Sheet1.Cells(i, 3).FormulaR1C1 = "=VLOOKUP(RC[-1],Sheet2!R1:R1048576,2,FALSE)"
'enter the Vlookup formula on column number 3, change this number to suit your needs, also change the 2 before FALSE to the column number you wish to bring into Sheet1
Next i
End Sub

VBA macro or functon to copy cell values based on criteria to a master sheet

Let’s say I have a rental car company and I have 12 sheets with 10 columns each and unknown amount of rows. Each sheet is holding information about cars rented. Below are the column headings for each spreadsheet
- A. Date rented
- B. Customer Name
- C. Customer Address
- D. Customer Phone
- E. Customer email
- F. Car Year
- G. Car Make
- H. Car Model
- I. Car Plate number
- J. Car Vin
I have a master sheet that I want to get specific information from all sheets and copy the cellValues of those sheets into the master sheet. I’m not familiar with VBA so Here is the sudocode of the loop I want to do:
For each sheet
For each row
Copy customer name, customer phone, car plate number into next available row on master sheet
In the master sheet, the columns would be respectively how I put them in the sudocode
- A. Customer Name
- B. Customer Phone
- C. Car Plate number
Can someone show me what the VBA macro code would be for this?
Disclaimer: this is not my actual information in my spreadsheet as what I am working on is confidential so I can’t provide screenshots. This is just example information that simulates what I want to do.
I've tried =HLOOKUP(B1,'Sheet1 (51)'!1:1048576,2:2,FALSE) but getting a value error or an NA error, depending on what range or values I try in the parameters. The way I was understanding the HLookup function is this:
lookup value is the column heading I'm looking for within the source sheet: for customer name I would but B2 for the lookup value
The table array would be the whole source sheet
The row array would be the row I'm getting the cell value from in the source sheet
range lookup is either T or F or nothing as it is optional. If i use True or False, I get the NA error if I use nothing I get the value error.
The idea is that once I get this formula working for one cell then expand it to one row then expand it to the loop i have in my sudocode for all rows within the source sheet, then expand it to look or go to the next source sheet.
You can accomplish this fairly easily by looping through all the available sheets, excluding the master sheet and setting the relevant range for each sheet. Then using the find function to get the last row of data in the master sheet to be able to append the next rows.
This should produce the desired result:
Sub MasterGrab()
Dim master As Worksheet
Dim subSheet As Range
Dim i As Integer
Dim lastRow As Long
Set master = Worksheets("MasterSheet")
x = Sheets.Count
lastRow = master.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 'find last row on master sheet
lastRow = lastRow + 1
For i = 1 To x
If Not Sheets(i).Name = "MasterSheet" Then 'capture all sheets except MasterSheet
Set subSheet = Sheets(i).Range("A1:J" & ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row) 'set each sheet range to cover required data
For Each r In subSheet.Rows
master.Cells(lastRow, 1) = r.Cells(2) 'Customer name
master.Cells(lastRow, 2) = r.Cells(4) 'Customer phone
master.Cells(lastRow, 3) = r.Cells(9) 'Car Plate number
lastRow = lastRow + 1
Next r
End If
Next i
End Sub

Filter and copy certain rows from multiple excel sheets to another

I am using a workbook that has various sheets. I want to copy all the rows from the last 5 sheets that have the value "Pending" in their column "J". I want to create a new tab named "Pending week" and paste all these rows there. Any help would be really appreciated.
Thanks
You can create this yourself very easily if you just break it down:
Add a new Sheet
Name the sheet to Pending Week
Find the five latest sheets.
Create some kind of loop that copy paste row if cells in column J contains the value "Pending"
You have not provided any code, so I'll give you a base to work from:
You add a new sheet & name it using:
Worksheets.Add
ActiveSheet.Name = "Pending week"
Find the five latest sheets
To my knowledge, you can't find the latest sheets. Sheets doesn't contain the date and time of when they were created. But if we ignore that and expect the five latest sheets to be placed in the workbook to the far most right (Default position for newly created sheets). Then you need to figure out how many sheets you have and count backwards.
You can use: Worksheets.Count to count all the sheets. Use this number and count it backwards. My first thought would be to use a For Loop
Dim X As Integer
For X = (Worksheets.Count - 4) To Worksheets.Count
Next
X would be the identifier to find our latest sheets. So you should incorporate that into our loop below. You want to place the loop within this For Block.
Loop
There are many ways to find a value in a sheet, but you need to figure out what the last row of your sheets are. Without it we don't know when the code should stop.
You can use a Do Until Loop if there is a value in all J cells. Then you can simply insert the entire row into Pending week
It would look something like:
Dim XLrow As Integer
XLrow = 1
Do Until Worksheets(1).Cells(XLrow, "J") = ""
If Worksheets(1).Cells(XLrow, "J") = "Pending" Then
Worksheets(1).Range(XLrow & ":" & XLrow) = Worksheets("Pending week").Cells(XLrow, "J").Value
End If
XLrow = XLrow + 1
Loop
You will need to change the Range to the length of the range you want to copy. Note: the value Pending is case sensitive, so keep that in mind.
Alright, this is what you need to create your code. Of course you need to change values to fit your own workbook, but this is the base.

Find number of rows in a column of a particular sheet in macros

I am struggling to write a function in VBA which takes sheet name and column as parameters and returns last used row number in that column of that sheet.
I used the following code:
Worksheet("sheetname").Range("e2").End(xlDown).Row
But it returns wrong values... like 104567
Please provide me the code to find row count in a particular column of particular sheet.
The 1 in the code is the column you would like to check. You can change ActiveSheet to any other valid worksheet object.
lLastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
An explanation: The number is actually 1,048,576 - the bottom of the worksheet. What it means is that there are no non-empty cells in the column "E:E" below row 2. The solution provided by braX is a standard and a very good way of handling this. In case you want to spec the column by name, you can do this:
Worksheet("sheetname").Range("e" & Worksheet("sheetname").Rows.Count).End(xlUp).Row

vlookup if name is matched display employee id

Ive never used vlookups i have a spreedsheet not sure if this is the right function. I have two sheets
Sheet 1
first name last name username
Sheet 2
first name last name employee id business unit
I need in column D on sheet 1 to have employee id ive below. Pay no attention to column letters and sheets because i moved to another sheet to try getting this right.
=MATCH(B11,Sheet1!C:C,0)
Any help is much appreciated.
So you realize you have to match both first and last names? There are several ways to accomplish this depending on how many employees you have in sheet 2: a) small list could a two-column search using array formula; b) large list just create another column in both sheets joining last & first names and do a MATCH or VLOOKUP on them.
Since your needs are simple and to illustrate option (b):
Insert a column in both Sheet1 and Sheet2 after the "last name"; you should now have an empty column C in both sheets.
Assuming you have column headers in row one, and thus data starts in row two, set cell C2 in both sheets with function =B2&","&A2, then fill-down that formula on both sheets in all rows.
Set Sheet1 cell E2 to formula =VLOOKUP(Sheet1!C2, Sheet2!$C:$D, 2, False), and fill-down that formula in all rows.
Voila, employee IDs on Sheet1. I do have to say this is so Excel 101. There are all sorts of examples and tutorials on this easily found using even the most trivial Google searches.