vba based on cell value to open another workbook and paste data

LAMB_LAMB

New Member
Joined
Apr 6, 2024
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
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)

File nameDateinvoice noModel noqty
PRO2024/4/3AA1234CHKEEU1
NEW2024/4/4AA1235NOVEM2
OLD2024/4/6AA1236KKBOX3






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
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Please try the following on a copy of your workbook. Assumes all your destination files are type macro-enabled.

VBA Code:
Option Explicit
Sub statem()
    Dim wbm As Workbook, wsSrc As Worksheet
    Set wsSrc = Worksheets("LIST")
    Dim i As Long
    For i = 2 To 40
        If wsSrc.Cells(i, 1) <> "" Then
            On Error Resume Next
            Set wbm = Workbooks.Open(ThisWorkbook.Path & "\" & wsSrc.Cells(i, 1) & ".xlsm")
            On Error GoTo 0
            wsSrc.Range(wsSrc.Cells(i, 2), wsSrc.Cells(i, 4)).Copy
            wbm.Worksheets("BB").Cells(Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial xlPasteValues
            Application.CutCopyMode = False
            wbm.Close True
        End If
    Next i
End Sub
 
Upvote 0
Please try the following on a copy of your workbook. Assumes all your destination files are type macro-enabled.

VBA Code:
Option Explicit
Sub statem()
    Dim wbm As Workbook, wsSrc As Worksheet
    Set wsSrc = Worksheets("LIST")
    Dim i As Long
    For i = 2 To 40
        If wsSrc.Cells(i, 1) <> "" Then
            On Error Resume Next
            Set wbm = Workbooks.Open(ThisWorkbook.Path & "\" & wsSrc.Cells(i, 1) & ".xlsm")
            On Error GoTo 0
            wsSrc.Range(wsSrc.Cells(i, 2), wsSrc.Cells(i, 4)).Copy
            wbm.Worksheets("BB").Cells(Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial xlPasteValues
            Application.CutCopyMode = False
            wbm.Close True
        End If
    Next i
End Sub

Thank you for your help. Success.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,189
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top