Cortex1000
New Member
- Joined
- Jul 30, 2022
- Messages
- 16
- Office Version
- 2013
- Platform
- Windows
Hi
I am working on a couple of macros and I got stuck on one of the tasks. I got everything working so far but there is one thing I cannot figure out how to put in place. The Idea is that I have 2 workbooks Main wb and Second wb. The VBA is helping to copy the data from second wb to main wb.
I looking to copy the data from column E (test5) from second workbook to column E(test5) in the main workbook if this is missing.
Sorry I cannot use XL2BB
Main WB
Second WB
The code you see helped me to get all the info from second wb and fill in the gaps into the main wb. But now I am looking to create a separate macro under the same idea but only to fill me column E values if these are missing in main wb.
I hope someone can have a look and get me an idea of how should I put this in place because what I have done so far didn't work.
I am working on a couple of macros and I got stuck on one of the tasks. I got everything working so far but there is one thing I cannot figure out how to put in place. The Idea is that I have 2 workbooks Main wb and Second wb. The VBA is helping to copy the data from second wb to main wb.
I looking to copy the data from column E (test5) from second workbook to column E(test5) in the main workbook if this is missing.
Sorry I cannot use XL2BB
Main WB
test1 | test2 | test3 | test4 | test5 | test6 | test7 | test8 | test9 | test10 | test11 | test12 |
info0 | R2000R22102312345 | info10 | info20 | info30 | info40 | info50 | |||||
info1 | R2000R22102312342 | info11 | info21 | info31 | info41 | info51 | |||||
info2 | R2000R22102312350 | info12 | info22 | info32 | info42 | info52 | |||||
info3 | R2000R22102312332 | info13 | info23 | info33 | info43 | info53 | |||||
info4 | R2000R22102312312 | info14 | info24 | info34 | info44 | info54 | |||||
info5 | R2000R22102312324 | info15 | info25 | info35 | info45 | info55 | |||||
info6 | R2000R22102312330 | info16 | info26 | info36 | info46 | info56 | |||||
L3000L07112316852 |
Second WB
test1 | test2 | test3 | test4 | test5 | test6 | test7 | test8 | test9 | test10 | test11 | test12 |
info0 | R2000R22102312345 | info10 | info20 | info30 | info40 | info50 | |||||
info1 | R2000R22102312342 | info11 | info21 | info31 | info41 | info51 | |||||
info2 | R2000R22102312350 | info12 | info22 | info32 | info42 | info52 | |||||
info3 | R2000R22102312332 | info13 | info23 | info33 | info43 | info53 | |||||
info4 | R2000R22102312312 | info14 | info24 | info34 | info44 | info54 | |||||
info5 | R2000R22102312324 | info15 | info25 | info35 | info45 | info55 | |||||
info6 | R2000R22102312330 | info16 | info26 | info36 | info46 | info56 | |||||
L3000L07112316852 | |||||||||||
L3000L07112316853 | |||||||||||
L3000L07112316854 | |||||||||||
L3000L07112316855 |
VBA Code:
Option Explicit
Sub TEST()
Dim objDic As Object
Dim i As Long, j As Integer, sKey As String
Dim arrData, rngData As Range
Dim arrRec, rngRec As Range
Dim wb2 As Workbook, Sh_Data As Worksheet
Dim lastRow As Long
Set objDic = CreateObject("scripting.dictionary")
' Open second workbook
Set Wb2 = Workbooks.Open("C:\Users\" & Environ$("USERNAME") & "\Desktop\" & "Second.xls")
Set Sh_Data = wb2.Worksheets("data2")
' Read data from sheet
With Sh_Data
lastRow = .Cells(.Rows.Count, "E").End(xlUp).Row
Set rngData = .Range("A1", .Cells(lastRow, 12))
End With
arrData = rngData.Value
wb2.Close False
' Load Dict with data
For i = LBound(arrData) + 1 To UBound(arrData)
objDic(arrData(i, 5)) = i
Next i
Dim Sh_Record As Worksheet, Main_wk As Workbook
Set Main_wk = ActiveWorkbook
Set Sh_Record = Main_wk.Sheets("Data1")
' Read data from sheet
With Sh_Record
lastRow = .Cells(.Rows.Count, "E").End(xlUp).Row
Set rngRec = .Range("A1", .Cells(lastRow, 12))
End With
arrRec = rngRec.Value
' Comparing Col E
For i = LBound(arrRec) + 1 To UBound(arrRec)
sKey = arrRec(i, 5)
If objDic.exists(sKey) Then
' Populate Col D to J if matching
For j = 4 To 10
If Len(arrRec(i, j))=0 Then arrRec(i, j) = arrData(objDic(sKey), j)
Next
End If
Next i
' Update main workbook
rngRec.Value = arrRec
Set objDic = Nothing
End Sub
The code you see helped me to get all the info from second wb and fill in the gaps into the main wb. But now I am looking to create a separate macro under the same idea but only to fill me column E values if these are missing in main wb.
I hope someone can have a look and get me an idea of how should I put this in place because what I have done so far didn't work.