Excel VBA: Concatenate lists with differing number of rows - vba

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

Related

Large Data set Macro query & alternative to Vlookup request

This query is two fold. Any assistance would be much appreciated and unfortunately there is a lot of detail behind this so apologies for the great deal of text.
I am working on updating a 12 month trending report that pulls data from one sheet using a series of complex vlookups based on several criteria (Project, unique project id, Order, yr-month etc). Yes it would likely be easier to use pivot tables, but unfortunately because of the way this document is used the people with access to it want it to remain as is, from a format and function perspective but want the document sped up (takes roughly 5 minutes to calculate when ever something is changed.)
The data set is 36 columns by 50000 rows with 7 columns containing identifiers used to pull in specific data on the trending sheet
As it current sits on a monthly basis new data (approximately 6000 rows) of additional data is added.
I've added two macros to the document in an attempt to reduce user error
I am currently using the following code to take the order number remove the duplicates and then apply an index/match to pull in the relevant Project and then use countif to apply a unique key to the end of the project (using concatenate)
Sub ProjTrending1()
Dim s1 As Worksheet, s2 As Worksheet
'Defines S1 as a Worksheet
Set s1 = Sheets("All Data")
'Defines S2 as WorkSheet
Set s2 = Sheets("Workings")
'Defines LastR1
Dim LastR1 As Long, DataRange1 As Range
'Finds last row cell working sheet
LastR1 = s2.Range("A1").CurrentRegion.Rows.Count
'Takes Data from Order Column of defined data Sheet and copy & pastes it to Working Sheet Column B
s1.Range("J1:J" & LastR2).Copy s2.Range("A1")
'Removes Duplicates from Column B Working sheet
s2.Range("A2:A" & LastR1).RemoveDuplicates Columns:=2, Header:=xlNo
'Copies the formula from B2 and applies it to all cells in column B where column A has values
s2.Range("B2").Copy s2.Range("B2:B" & LastR1)
'Copies the formula from C2 and applies it to all cells in column C where column A has values
s2.Range("C2").Copy s2.Range("C2:C" & LastR1)
End Sub
The formula is column B is as follows
=INDEX('All Data'!E:E,MATCH(Workings!A2,'All Data'!J:J,0))
The formula in column C is as follows
=B2&COUNTIFS($B$2:B2,B2)
The data set in worksheet All Data is approx 50k rows long and will continue to grow. The issue I'm not running into is that this macro is starting to take a great deal of time to run because the information in column C is used as a unique identifier on the trending report.
Is this because I have a vast number of Vlookups on the trending tab (approx 20k) pulling data based on this or is there an issue with the macro I have created to create a unique identifier?
I've been looking at alternative ways in which I could draw the data through to the trending tab (dictionaries etc) with no luck so far. It would be much appreciated if someone could offer an alternative to 20k vlookups.

Copying values across a dynamic column and row range

I have the following code that:
copies the column headings (in row 1) from column C across to the second last column
pastes these column headings in row 1 in the column 2 across from the last column with data
pastes the column headings alongside each row of data through to the bottom row
Sub GLDR()
'use End(xlUp) to determine Last Row with Data, in column A of the GLDRYYPP tab
Dim lastRowDR As Long
lastRowDR = Sheets("GLDRYYPP").Range("A" & Rows.Count).End(xlUp).Row
'copy the cost type categories and paste alongside the cost centres
CTNameCol = "S2:AF" & lastRowDR
Sheets("GLDRYYPP").Range("C1", Range("C1").End(xlToRight).Offset(0, -1)).Copy
Sheets("GLDRYYPP").Paste Destination:=Sheets("GLDRYYPP").Range("C1").End(xlToRight).Offset(0, 2)
Sheets("GLDRYYPP").Range(Range("C1").End(xlToRight).Offset(0, 2), Range("C1").End(xlToRight).Offset(0, 2).End(xlToRight)).Copy
Sheets("GLDRYYPP").Paste Destination:=Sheets("GLDRYYPP").Range(CTNameCol)
End Sub
The first two steps have been set to be dynamic for any additional columns added but I am having trouble writing some code that will paste the data through to the bottom row. As you can see the range "S2:AF(last row)" has been written to make use of the result from the lastRowDR dimension.
Is there a way to write the code which will make the copy dynamic across the columns and rows?
Yes there is a way to write the code which will make the copy dynamic across the columns and rows. What you need to do is to identify a distinct quality about the cost centres so that you locate it using something like ActiveSheet.Cells.Find(Txt).
Then you'll be able to determine what CTNameCol should be. There is some helpful info here: Range.Find Method.
If you add add a picture of your spreadsheet I will update this answer with more precise information.

Find Last Row of Name with constantly changing names

I'm really stuck on this one. I have a spreadsheet with thousands of rows. I use this code to filter them based off of product in the E column.
Sub IsolateCCENCE()
Dim Operations As Workbook
Dim Operations_Sheet As Worksheet
Set Operations = Workbooks("Operations for Macros")
Set Operations_Sheet = Operations.Worksheets("Operations")
Operations_Sheet.Range("$A$6:$AH$13108").AutoFilter Field:=5, Criteria1:="=CCE" _
, Operator:=xlOr, Criteria2:="=NCE"
End Sub
Which works and leaves me with just under 1700 rows. Within these rows, in the A column, there are company names. Each company takes up approximately 20 rows. Each row represents a payment and has a corresponding date, in the D column. I need a macro (I'm assuming with a loop) that will then do the following:
Go through the rows, find the last row for each company
In that row, find the corresponding date
If that date is within 30 days from today, generate an email
Part 3 is easy. But Part 1 and 2 I can't seem to get. The data is always going to be changing.
Maybe it would be easier to have all of the data copy and pasted into another spreadsheet and then filter through every single company, find the last row (and thus the corresponding date)? But I don't know I would have a macro defined to filter through each company when the company names will be changing constantly.
I appreciate any help. Thanks in advance!
If a specific company name in say F1 then:
=MIN(IF(A:A=F1,D:D))
entered with Ctrl+Shift+Enter should give you the earliest date for the company named in F1, that if more recent than today()-30 (or less far into the future than today()+30 ?) you might use for your e-mail trigger (subject to other filtering etc).

Removing duplicates and summing their values into a single row - Excel Reporting

So I would like to get this table to only display 1 row for each individual with a sum of all of their tickets. The way it is right now, Marci is coming up multiple times because each row represents the amount of tickets for a different department (Ex. # of paramedic tickets, # of FireFighter tickets, etc..) I tried the "Unique Records Only" under the advanced tab, and obviously that didn't work because the rows are not unique.
Can someone please assist me?
You need VBA. Make a loop on these 2 concepts:
1: sort cells alphabetically
1: check neighbour cells for equality:
2: Delete and number occurences if true
3: proceed to read next line
Sub RemoveAndNumberDuplicates
Dim r As range
Set r = Selection
for N=1 to 100
IF r.offset(N, 0).value= R.offset(N - 1 - NumOfDeletedLines, 0).value THEN
SumOfDupl=SumOfDupl+1 'number of current record instances
rang.offset(N, 0).Delete (xlShiftUp)
rang.offset(N, 1).Value=SumOfDupl ' show duplicates in side column
NumOfDeletedLines=NumOfDeletedLines+1 'needed to correct offset for deleted lines
Else
SumOfDupl=1'restart numbering if no duplicate found
End If
Next N
End Sub
This is a general idea for Excel VBA, not a complete code.

Getting corresponding data from another sheet

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