I am beginner. I want to write VBA to base on the column "A" value to open the workbooks is same name. and copy column B to E value to this workbook.
Such as Open workbook "PRO" (A2) and paste Column "B2:E2"value to workbook"PRO".sheet"BB" column (A: D)
Sub statem()
Dim i As Integer
Dim wbm As Workbook
Dim wbL As Workbook
Dim masterNextRow As Long
For i = 2 To 40
Set wbL = ThisWorkbook
Set wbm = Workbooks.Open(ThisWorkbook.Path & "\" & Worksheets("LIST").Cells(i, 1) & ".xlsm")
masterNextRow = wbm.Worksheets("BB").Range("A" & wbm.Worksheets("BB").Rows.Count).End(xlUp).Offset(1)
wbL.Worksheets("LIST").Range(i, 2).Copy
wbm.Worksheets("BB").Cells(masterNextRow, 1).PasteSpecial Paste:=xlPaasteValues
wbL.Worksheets("LIST").Range(i, 3).Copy
wbm.Worksheets("BB").Cells(masterNextRow, 2).PasteSpecial Paste:=xlPaasteValues
wbL.Worksheets("LIST").Range(i, 4).Copy
wbm.Worksheets("BB").Cells(masterNextRow, 3).PasteSpecial Paste:=xlPaasteValues
Next i
wbm.Close True
End Sub
Such as Open workbook "PRO" (A2) and paste Column "B2:E2"value to workbook"PRO".sheet"BB" column (A: D)
File name | Date | invoice no | Model no | qty |
PRO | 2024/4/3 | AA1234 | CHKEEU | 1 |
NEW | 2024/4/4 | AA1235 | NOVEM | 2 |
OLD | 2024/4/6 | AA1236 | KKBOX | 3 |
Sub statem()
Dim i As Integer
Dim wbm As Workbook
Dim wbL As Workbook
Dim masterNextRow As Long
For i = 2 To 40
Set wbL = ThisWorkbook
Set wbm = Workbooks.Open(ThisWorkbook.Path & "\" & Worksheets("LIST").Cells(i, 1) & ".xlsm")
masterNextRow = wbm.Worksheets("BB").Range("A" & wbm.Worksheets("BB").Rows.Count).End(xlUp).Offset(1)
wbL.Worksheets("LIST").Range(i, 2).Copy
wbm.Worksheets("BB").Cells(masterNextRow, 1).PasteSpecial Paste:=xlPaasteValues
wbL.Worksheets("LIST").Range(i, 3).Copy
wbm.Worksheets("BB").Cells(masterNextRow, 2).PasteSpecial Paste:=xlPaasteValues
wbL.Worksheets("LIST").Range(i, 4).Copy
wbm.Worksheets("BB").Cells(masterNextRow, 3).PasteSpecial Paste:=xlPaasteValues
Next i
wbm.Close True
End Sub