0

我的目标是将单元格值从工作簿 2 转移到工作簿 1。这是情境设置。我想将单元格值从单元格 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
4

1 回答 1

0

@siddharth rout 我想要 D147 中 O4 的值,D148 中的 O5,D149 中的 O6 等等

这是您正在尝试的(未测试)吗?

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
于 2013-10-22T19:50:41.187 回答