将单元格值传输到另一个工作表而无需复制和粘贴?

时间:2013-10-22 19:37:47

标签: excel vba excel-vba

我的目标是将单元格值从workbook2传输到workbook1。这是情况设置。我想将单元格值从单元格O4传输到工作簿2中的列末尾(其中包含至少138行)到工作簿1中的单元格D147到Dxxx。

这就是我的VBA代码。我的问题是VBA不明白我要告诉程序要做什么直到第6行。

Private Sub CommandButton2_Click()
Dim wb1 As Workbook
Dim wb2 As Workbook
Set wb1 = ActiveWorkbook
Set wb2 = Workbooks.Open("C:\Users\zyu\Documents\Processed Data\Allagash\16 cells\Copy of TXM10421_24M_capacity_102113.xls")

wb1.Sheets("sheet1").Range("D147", Range("D147").End(xlToLeft)).value = wb2.Sheet("16").Range("O4").value

End Sub

1 个答案:

答案 0 :(得分:0)

  

@siddharth溃败我希望D147中的O4值,D148中的O5值,D149中的O6值等等

这是你正在尝试的(UNTESTED)吗?

Option Explicit

Sub copyrow2()
    Dim wb1 As Workbook, wb2 As Workbook
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim lastRow  As Long

    Set wb1 = ThisWorkbook
    Set ws1 = wb1.Sheets("sheet1")

    Set wb2 = Workbooks.Open("C:\Users\zyu\Documents\Processed Data\16 cells\Copy of TXM10421_24M_capacity_102113.xls")
    Set ws2 = wb2.Sheets("16")

    With ws1
        lastRow = ws2.Range("O" & ws2.Rows.Count).End(xlUp).Row

        .Range("D147").Resize(lastRow, 1).Value = ws2.Range("O4:O" & lastRow).Value
    End With
End Sub