Hello all
I am a first-time poster to this forum, but have read through a lot of the posts and have gained so much knowledge to help in my own journey as a relative novice in learning and applying VBA in my job as an analyst.
I have a question as to how to paste certain non-contiguous columns from a worksheet in one workbook to a Table in a worksheet in another workbook and I would really appreciate any help or guidance you could give me. I have posted an example of the code I have tried so far below. I get a data type mismatch error (13) on the line:
Set vCOLs = Array("A", "C", "D", "H", "I", "M", "P", "T", "U", "V")
What I am trying to do is as follows:
1) From my destination worbook pen the SKUListing Workbook (my source workbook) and copy the data from row 2 in columns A,C,D,H,I,M,P,T,U,V, down to the last populated row, from the SKUListing sheet
2) Paste this data into columns 1 through to 10 (the column headings of the destination workbook match those of the source) into column A of Table1 on Sheet1 of the destination workbook. Because there will already be data in the destination workbook I just need to have this new data populated into the next available row. Basically each week I will be appending new data as the source workbook itself gets refreshed with new data. The code I have tried thus far which fails (due to extent of my knowledge) is as follows:
Sub InsertLockStockData()
Dim WB1 As Workbook
Dim WB2 As Workbook
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set WB1 = ThisWorkbook
Set WB2 = Workbooks.Open("C:\Users\mrpavlos\Desktop\Latest UTL Report Templates\Stock Reports\SKU Listing\SKUListing.xlsb", Local:=True)
Dim a As Long, b As Long, c As Long, lr As Long
Dim vCOLs As Variant, vVALs As Variant, vSRCs As Variant
With WB2.Worksheets("SKUListing")
Set vCOLs = Array("A", "C", "D", "H", "I", "M", "P", "T", "U", "V")
lr = Application.Max(.Cells(.Rows.Count, "A").End(xlUp).Row, _
.Cells(.Rows.Count, "C").End(xlUp).Row, _
.Cells(.Rows.Count, "D").End(xlUp).Row, _
.Cells(.Rows.Count, "H").End(xlUp).Row, _
.Cells(.Rows.Count, "I").End(xlUp).Row, _
.Cells(.Rows.Count, "M").End(xlUp).Row, _
.Cells(.Rows.Count, "P").End(xlUp).Row, _
.Cells(.Rows.Count, "T").End(xlUp).Row, _
.Cells(.Rows.Count, "U").End(xlUp).Row, _
.Cells(.Rows.Count, "V").End(xlUp).Row)
vSRCs = .Range(.Cells(2, "A"), .Cells(lr, "V")).Value2
ReDim vVALs(1 To lr - 1, 1 To UBound(vCOLs) + 1)
For a = 1 To lr - 1
For b = LBound(vCOLs) To UBound(vCOLs)
vVALs(a, b + 1) = vSRCs(a, vCOLs(b))
Next b
Next a
WB1.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Resize(UBound(vVALs, 1), UBound(vVALs, 2)) = vVALs
End With
Application.CutCopyMode = False
WB2.Close False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
I am a first-time poster to this forum, but have read through a lot of the posts and have gained so much knowledge to help in my own journey as a relative novice in learning and applying VBA in my job as an analyst.
I have a question as to how to paste certain non-contiguous columns from a worksheet in one workbook to a Table in a worksheet in another workbook and I would really appreciate any help or guidance you could give me. I have posted an example of the code I have tried so far below. I get a data type mismatch error (13) on the line:
Set vCOLs = Array("A", "C", "D", "H", "I", "M", "P", "T", "U", "V")
What I am trying to do is as follows:
1) From my destination worbook pen the SKUListing Workbook (my source workbook) and copy the data from row 2 in columns A,C,D,H,I,M,P,T,U,V, down to the last populated row, from the SKUListing sheet
2) Paste this data into columns 1 through to 10 (the column headings of the destination workbook match those of the source) into column A of Table1 on Sheet1 of the destination workbook. Because there will already be data in the destination workbook I just need to have this new data populated into the next available row. Basically each week I will be appending new data as the source workbook itself gets refreshed with new data. The code I have tried thus far which fails (due to extent of my knowledge) is as follows:
Sub InsertLockStockData()
Dim WB1 As Workbook
Dim WB2 As Workbook
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set WB1 = ThisWorkbook
Set WB2 = Workbooks.Open("C:\Users\mrpavlos\Desktop\Latest UTL Report Templates\Stock Reports\SKU Listing\SKUListing.xlsb", Local:=True)
Dim a As Long, b As Long, c As Long, lr As Long
Dim vCOLs As Variant, vVALs As Variant, vSRCs As Variant
With WB2.Worksheets("SKUListing")
Set vCOLs = Array("A", "C", "D", "H", "I", "M", "P", "T", "U", "V")
lr = Application.Max(.Cells(.Rows.Count, "A").End(xlUp).Row, _
.Cells(.Rows.Count, "C").End(xlUp).Row, _
.Cells(.Rows.Count, "D").End(xlUp).Row, _
.Cells(.Rows.Count, "H").End(xlUp).Row, _
.Cells(.Rows.Count, "I").End(xlUp).Row, _
.Cells(.Rows.Count, "M").End(xlUp).Row, _
.Cells(.Rows.Count, "P").End(xlUp).Row, _
.Cells(.Rows.Count, "T").End(xlUp).Row, _
.Cells(.Rows.Count, "U").End(xlUp).Row, _
.Cells(.Rows.Count, "V").End(xlUp).Row)
vSRCs = .Range(.Cells(2, "A"), .Cells(lr, "V")).Value2
ReDim vVALs(1 To lr - 1, 1 To UBound(vCOLs) + 1)
For a = 1 To lr - 1
For b = LBound(vCOLs) To UBound(vCOLs)
vVALs(a, b + 1) = vSRCs(a, vCOLs(b))
Next b
Next a
WB1.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Resize(UBound(vVALs, 1), UBound(vVALs, 2)) = vVALs
End With
Application.CutCopyMode = False
WB2.Close False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub