Created
November 8, 2013 11:46
-
-
Save hussainm/7369900 to your computer and use it in GitHub Desktop.
VBA script to combine two excel sheets based on a column. Quick and very dirty script to join two sheets in a one to one mapping based on a common column.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Const sheet1 As String = "magento1" 'primary sheet | |
Public Const sheet2 As String = "ga" 'secondary sheet | |
Public Const keyCol As String = "A" 'column to use as key | |
Public Const sheet1ColEnd As String = "H" 'column to start appending data | |
Sub MergeSheet() | |
Dim id1, id2 As String | |
Dim sheet1Loop, sheet2Loop As Integer | |
sheet1Loop = 2 | |
Do While Range(sheet1 & "!" & keyCol & sheet1Loop).Value <> "" | |
id1 = Range(sheet1 & "!" & keyCol & sheet1Loop).Value | |
sheet2Loop = 2 | |
Do While Range(sheet2 & "!" & keyCol & sheet2Loop).Value <> "" | |
id2 = Range(sheet2 & "!" & keyCol & sheet2Loop).Value | |
If id1 = id2 Then | |
' append row from sheet2 to sheet 1 | |
Dim hLoop As Integer | |
hLoop = 0 | |
Do While Range(sheet2 & "!A" & sheet2Loop).Offset(0, hLoop).Value <> "" | |
Range(sheet1 & "!" & sheet1ColEnd & sheet1Loop).Offset(0, hLoop + 1).Value = Range(sheet2 & "!A" & sheet2Loop).Offset(0, hLoop).Value | |
hLoop = hLoop + 1 | |
Loop | |
End If | |
sheet2Loop = sheet2Loop + 1 | |
Loop | |
sheet1Loop = sheet1Loop + 1 | |
Loop | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment