Merge two excel files using a common column - vba

I have two excel sheets. I have to merge the two such that the values in one match with the other. For eg.
The first excel, the 2nd excel
1 t 1 tes1
2 5 3 tes3
3 t 4 tes4
4 g
Notice that in the first column of the 2nd excel, 2 is missing, so I want the first excel to look like this,
1 tes1 t
2 5
3 tes3 t
4 tes4 g
I am new to excel. Any help on this will be highly appreciated.

Sub left_join()
Dim res As Variant
Dim i As Long, lastUsedRowSh1 As Long, lastUsedRowSh2 As Long
Dim cell As Range
Sheets(3).Cells.ClearContents
Sheets(1).Range("a:b").Copy Destination:=Sheets(3).Range("a1")
Sheets(3).Columns(2).Insert Shift:=xlToRight
lastUsedRowSh1 = Sheets(1).Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
lastUsedRowSh2 = Sheets(2).Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
i = 1
For Each cell In Sheets(1).Range("a1:a" & lastUsedRowSh1)
On Error Resume Next
res = Application.WorksheetFunction.VLookup(cell.Value, Sheets(2).Range("a1:b" & lastUsedRowSh2), 2, 0)
If Err.Number = 0 Then
Sheets(3).Range("b" & i).Value = res
i = i + 1
Else
i = i + 1
End If
Next cell
End Sub
You can even solve with a simple formula.
Foglio1
A B
1 t
2 5
3 t
4 g
Foglio2
A B
1 tes1
3 tes3
4 tes4
Foglio3
Copy the content of Foglio1 in Foglio3, then run this formula
=IF(ISERROR(VLOOKUP(Foglio1!A1,Foglio2!$A$1:$B$3,2,0))=TRUE,"",VLOOKUP(Foglio1!A1,Foglio2!$A$1:$B$3,2,0))
and drag it down. Regards.

Related

Find the cell with more common subcells

I haven't seen any similar question to this one. Thank you in advance for your help!
I have these two columns:
Final Product - Subcomponent
A - 1
B - 1
C - 1
D - 1
A - 2
C - 2
B - 3
C - 3
A - 4
C - 4
D - 4
A - 5
B - 5
Final product A is made with the subcomponents 1, 2,4 and 5.
B is made with the subcomponents 1,3 and 5.
C is made with the subcomponents 1,2 and 4.
D is made with the subcomponents 1 and 4.
What I am looking for is an algorithm in vba or pivot tables that optimizes the final production in this way:
1 repeats 4 times.
2 repeats 2 times.
3 repeats 2 times.
4 repeats 3 times.
5 repeats 2 times.
First A should be made because it has more common components. Then B should be made because there is just 1 component missing compared with A. Then C because there is just one component to be replaced and last D because there is has the same two components as C.
I know this is not easy at all... Thank you!
Try this code
Sub Test()
Dim d As Object
Dim v As Variant
Dim m As Long
Dim r As Long
Dim i As Long
m = Range("A" & Rows.Count).End(xlUp).Row
v = Range("A1:B" & m).Value
Set d = CreateObject("Scripting.Dictionary")
For r = 1 To m
If d.Exists(v(r, 1)) Then
d(v(r, 1)) = d(v(r, 1)) & ", " & v(r, 2)
Else
d(v(r, 1)) = v(r, 2)
End If
Next r
Range("E1").Resize(d.Count).Value = Application.Transpose(d.Keys)
Range("F1").Resize(d.Count).Value = Application.Transpose(d.Items)
End Sub

How concatenate columns in excel repeatedly

I have 256 columns in a excel sheet. I need to bring them to 128 columns by merging pairs of columns. How can I repeat the process of concatenating two cells throughout the sheet
Eg:
Col | Col2 | Col3 | Col4
1: | 45 | 2: | 556
1: | 34 | 2: | 567
Now my expected result is
Col1 | Col2
1:45 | 2:556
1:34 | 2:567
How can I perform this in Excel?
Can I use VBA?
Add two columns with the simple formula =A2&B2. Copy down. then copy resulting formula columns and paste special values to get rid of formula.
Assuming that range is "A1:IV1000" (change to what you need). In the end the original range is cleared and resulting array is dumped.
Sub ConcatColumns()
Dim rng As Range
Dim x As Long, c As Integer, z As Integer
Dim arr, arrResult
Const COLS As Integer = 256
Set rng = Range("A1:IV100") '//256 columns
arr = rng.Value
ReDim arrResult(1 To UBound(arr, 1), 1 To COLS / 2)
For x = 1 To UBound(arr, 1)
z = 1
For c = 1 To COLS - 1 Step 2
arrResult(x, z) = arr(x, c) & arr(x, c + 1)
z = z + 1
Next
Next
rng.ClearContents '//Clear all previous data
Range("A1").Resize(UBound(arrResult, 1), UBound(arrResult, 2)).Value = arrResult '//Dump result
End Sub
Using the formula (in cell A4 in image below)
=OFFSET(A1, , COLUMN()-1)&OFFSET(B1,,COLUMN()-1)

VBA running sum

Let's say I have 2 columns (the following table includes the result)
Product ID Price Average
1 4 5
1 4 5
1 7 5
2 3 3
2 3 3
3 9 9
I want to be able to write a VBA code to loop through the rows of Product IDs and create the 3rd column which has average out the Prices.
I guess a For statement would work, but how do I define temp variables to store each ID?
Thanks!
As Vasily and L.Dutch told, AVERAGEIF() function is all you need. If you want to loop it through all cells, you can use Do While loop like this:
Sub avg()
Dim i As Integer
i = 2
Do While Range("A" & i).Value <> ""
Range("C" & i).FormulaR1C1 = "=AVERAGEIF(C1,RC1,C2)"
i = i + 1
Loop
End Sub

How to define a range using R for loop to create graphs

I have a dataset in excel that looks like this:
MA M1 M2 T1 T2 W1 W2 Th1 Th2 F1 F2
100 1 2 2 1 2 0 0 2 2 1
100 2 0 2 1 2 2 1 2 2 0
101 1 3 0 1 1 0 1 0 1 1
101 0 2 1 1 0 1 1 1 1 1
102 1 1 1 2 0 1 0 0 2 2
102 1 2 0 1 1 0 1 1 0 3
I am trying to create a column chart for each code (100,101,102) where each code will have 2 data sets and the horizontal values will be m1, m2, t1, etc.
So in the end I want 3 column graphs. I am trying to use a for loop to create these graphs in VBA, and here is what I have been trying:
Sub MA()
Dim i As Integer
Dim row1 As Integer, row2 As Integer
For i = 1 To 6 Step 2
Dim MAChart As Chart
Set MAChart = ActiveSheet.Shapes.AddChart.Chart
With MAChart
row1 = i + 1
row2 = i + 2
.ChartType = xlColumnClustered
.SetSourceData Source:=ActiveSheet.Range("Q& row1 & : & Z & row2")
End With
Next i
End Sub
I keep getting an "Application defined or object defined" error. I am having trouble defining the range of each chart since it changes based on i. I would love to find a clean way to make a series of charts using a for loop without redefining the range/dataset each time for each different chart. Does anyone know a good way to do this??
Below is treating the whole "range" as a string which would not equate to a range
Range("Q& row1 & : & Z & row2")
Try using below, you have no need for row1 and row2. Take note of how I am building up the string that makes a valid range
Range("Q" & 1+i & ":Z" & 2+i)
Used with your code be something like, notice I have also moved your "Dim" out of the loop this does not need to created each loop but needs to be "Set" every loop
Sub MA()
Dim i As Integer
Dim MAChart As Chart
Dim row1 As Integer, row2 As Integer
For i = 1 To 6 Step 2
Set MAChart = ActiveSheet.Shapes.AddChart.Chart
With MAChart
.ChartType = xlColumnClustered
.SetSourceData Source:=ActiveSheet.Range("Q" & i & ":Z" & 1+i)
End With
Next i
End Sub
You may also want to consider the position of the charts above will create them all on top of each other.

Delete rows in which column A contains values in column A of Sheet 2

I'm traiting an 40M Excel file, I'm thinking of doing some deletes.
For example, I have the data in Sheet 1, and the primary key which I want to delete in sheet2.
Sheet 1
Column A | Column B | ...
0047 | a | ...
0048 | b | ...
0051 | c | ...
Sheet 2
Column A
0047
0051
Would you please tell me how do write a VBA script which delete line 0047 and 0051 in Sheet 1?
I'm new to VBA scripting.
try the code below. Loops through the rows until you find the values you are after using the line below you can delete rows:
Rows(i - j).Delete
Complete code:
Sub main()
Dim i As Integer
Dim j As Integer
j = 0
For i = 2 To 1000
If (Cells(i - j, 1) = "0047") Or (Cells(i - j, 1) = "0051") Then
Rows(i - j).Delete
j = j + 1
End If
Next i
End Sub