Home > Blockchain >  How can I copy data from one Excel workbook to another Excel workbook using variables for Workbook a
How can I copy data from one Excel workbook to another Excel workbook using variables for Workbook a

Time:12-28

I tried this code, but I can't figure out how to use the variable for Worksheet. Can someone help? Thanks a lot!

Sub Test()

    Dim wbSource As Workbook, wbDest As Workbook
    Set wbSource = ThisWorkbook
    Set wbDest = Workbooks("DestinationWB.xlsx")
    
    Dim wsSource As Worksheet, wsDest As Worksheet
    Set wsSource = ThisWorkbook.Worksheets("Sheet1")
    Set wsDest = Workbooks("DestinationWB.xlsx").Worksheets("Sheet1")
    
    wbDest.Worksheets("Sheet1").Range("A1").Value = _
    wbSource.wsDest.Range("A1").Value
    
End Sub

CodePudding user response:

Sub Test()

    Dim wbSource As Workbook, wbDest As Workbook
    Set wbSource = ThisWorkbook
    Set wbDest = Workbooks("DestinationWB.xlsx")
    
    Dim wsSource As Worksheet, wsDest As Worksheet
    Set wsSource = wbSource.Worksheets("Sheet1")
    Set wsDest = wbDest.Worksheets("Sheet1")
    
    wsDest.Range("A1").Value = wsSource.Range("A1").Value
    
End Sub
  • Related