Denin Srmic
New Member
- Joined
- Apr 28, 2020
- Messages
- 19
- Office Version
- 365
- Platform
- Windows
I am new to Forum and new to VBA but have been following posts here and learning VBA along.
I have gone through this Forum and searched on the web in order to find the way how to compare Arrays and insert missing columns. I have adapted some of the codes generated here by VBA masters but cannot get it to work for the task I have in front of me which is adding missing columns.
I have two Column Header ranges one (Workbook "New") which consist of 216 columns and one of (Workbook "Old") 152. Both ranges, stored in two workbooks. Both have same Column Headers names (i.e, "ID number", "first name", "Surname", "Date of Birth", "Email", "GP Phone Number" etc), but Workbook "Old" has missing columns which I would like to check against Workbook "New" and populate missing Columns from Workbook "New" to Workbook "Old". Workbook Old should at the end have same column Headers as Workbook New, consisting of 216 columns.
After running my code, it adds unfortunately more columns than it should, it appears it adds duplicate columns. Could you please help here out. I would also like to reorder columns in Workbook Old based on patters from Column Header in Workbook New. Any improvements on my code or critics are very welcome. Many thanks.
I hop this make sense.
I have gone through this Forum and searched on the web in order to find the way how to compare Arrays and insert missing columns. I have adapted some of the codes generated here by VBA masters but cannot get it to work for the task I have in front of me which is adding missing columns.
I have two Column Header ranges one (Workbook "New") which consist of 216 columns and one of (Workbook "Old") 152. Both ranges, stored in two workbooks. Both have same Column Headers names (i.e, "ID number", "first name", "Surname", "Date of Birth", "Email", "GP Phone Number" etc), but Workbook "Old" has missing columns which I would like to check against Workbook "New" and populate missing Columns from Workbook "New" to Workbook "Old". Workbook Old should at the end have same column Headers as Workbook New, consisting of 216 columns.
After running my code, it adds unfortunately more columns than it should, it appears it adds duplicate columns. Could you please help here out. I would also like to reorder columns in Workbook Old based on patters from Column Header in Workbook New. Any improvements on my code or critics are very welcome. Many thanks.
I hop this make sense.
VBA Code:
Public Sub InsertMissingCol()
'with this code we aim to check column headers in Old Workbook and add missing one from New. _
After that we will reorder columns. _
Then we will feed all data except fist name and surname into master database
Dim oNhdrWb As Workbook
Dim oNhdrWs As Worksheet
Dim oWb As Workbook
Dim oWs As Worksheet
Dim r As Range 'range as a whole
Dim rHdr As Range 'old header Range
Dim rNhdr As Range 'New Header range
Dim rCopy As Range
Dim rDestination As Range
Dim aNewHdr As Variant 'Array for New Range
Dim aOldHdr As Variant 'Array for Old Range
'Dim aE As Variant
Dim i As Integer
Dim j As Integer
Dim lr As Integer 'last row in Old
Dim lc As Integer
Dim iCntrMissCol As Integer
Dim iNlr As Integer 'last row in New
Dim iNlc As Integer
Const f As Byte = 1
Dim sNhdrWbFilePath As String
Dim sOwB As String
Dim soWsNm As String
Dim bDup As Boolean
sNhdrWbFilePath = Environ("UserProfile") & "\OneDrive\Desktop\New.xlsx"
sOwB = Environ("UserPorfile") & "\OneDrive\Desktop\Old.xlsm"
On Error Resume Next
Set oNhdrWb = Workbooks.Open(sNhdrWbFilePath)
On Error GoTo 0
Set oNhdrWs = oNhdrWb.Worksheets(1)
Stop 'for testing only
iNlr = oNhdrWs.Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row
iNlc = oNhdrWs.Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column
Set rNhdr = oNhdrWs.Range(oNhdrWs.Cells(f, f), oNhdrWs.Cells(f, iNlc))
rNhdr.Select
aNewHdr = oNhdrWb.Worksheets("Testing").Range("A1:HH" & iNlc)
'aNewHdr = rNhdr.Value '<<<<<<<<<<< Question: How to use this line of code instead of line above.Would appreciate any suggestion
Stop
Set oWb = ThisWorkbook
Set oWs = oWb.Worksheets(1)
soWsNm = ExtrctStr(sOwB) function to extract original (genuine) name
oWs.Name = "Test " & soWsNm
oWs.Activate
lr = oWs.Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row
lc = oWs.Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column
Set rHdr = oWs.Range(oWs.Cells(f, f), oWs.Cells(f, lc))
rHdr.Select
aOldHdr = oWs.Range("A1:EV" & lc)
'aOldHdr = rHdr.Value '<<<<<<<<<<< Question: How to use this line of code instead of line above. Would appreciate any suggestion
oWb.Worksheets.Add.Name = "Test"
Set rDestination = oWb.Worksheets("Test").Range("A1")
Set r = oWs.Range(oWs.Cells(f, f), oWs.Cells(lr, lc))
Set rCopy = r
rCopy.Copy Destination:=rDestination
Application.CutCopyMode = False
Stop
'loop through Array of New column header and compare them to Sheet on ThisWorkbook
For i = LBound(aOldHdr, 1) To UBound(aOldHdr, 1)
bDup = False
For j = LBound(aNewHdr, 1) To UBound(aNewHdr, 1)
If aOldHdr(1, i) = aNewHdr(1, i) Then
bDup = True: Debug.Print bDup
End If
Next j
If Not bDup Then
oWb.Worksheets("Test").Cells(1, i).EntireColumn.Insert 'here i am attempting to understand where I have made mistake
oWb.Worksheets("Test").Cells(1, i).Value = aNewHdr(1, i)
'bX = True
'aOldHdr(1, i) = aNewHdr(1, i)
' oWs.Cells(1, i).EntireColumn.Insert
'oWs.Cells(1, i).Interior.ColorIndex = 45
' oWs.Cells(1, i).Value = aNewHdr(1, i)
iCntrMissCol = iCntrMissCol + 1
End If
Next i
'If bX = False Then
'Debug.Print "Items " & i & " identical"
'End If
'Debug.Print iCntrMissCol
Stop
oWb.Close SaveChanges:=True
oNhdrWb.Close SaveChanges:=True
End Sub
Public Function ExtrctStr(sExtr As String) As String
Dim sInput As String
Dim sOutput As String
Dim iIntFirst As Integer
Dim iIntSecond As Integer
Dim iIntThird As Integer
sInput = Environ("UserPorfile") & "\OneDrive\Desktop\Old.xlsm"
iIntFirst = InStr(1, sInput, "\", vbTextCompare)
iIntSecond = InStr(1, Mid(sInput, iIntFirst + 1), "\", vbTextCompare)
iIntThird = InStr(1, Mid(sInput, iIntSecond + 2), "\", vbTextCompare)
If iIntSecond > 0 Then
iIntSecond = iIntFirst + iIntSecond
End If
If iIntThird > 0 Then
iIntThird = iIntSecond + iIntThird
End If
'sOut = Mid(sIn, InStr(3, sIn, "\"), InStr(3, sIn, ".") - 1)
sOutput = Mid(sInput, (InStr(1, sInput, ".") - 1) - (iIntThird), (InStr(1, sInput, ".") - 1) - (iIntThird))
ExtrctStr = sOutput
End Function