将粘贴范围(使用设置值)复制到不同的工作簿

时间:2017-05-17 06:02:01

标签: excel vba excel-vba

我有两个工作簿,wbwb2。我想做什么,

  1. Nov中的数据复制到Nov Temp //中的工作表wb //此部分代码正常运行。

  2. wb中的特定范围复制到wb2。 //我尝试使用Rng来设置值,但由于我处理范围的方式,它可能无法正常工作。 <{1}}中的Rng突出显示为

      

    编译错误;找不到方法或数据成员。

  3. 请帮忙......

    (myRange = sht.Rng.Value)

1 个答案:

答案 0 :(得分:0)

现在可以使用,删除myRange并单独使用Rng。

Option Explicit

Sub cont()
    Application.Volatile

    Dim sht As Worksheet
    'Dim myRange As Variant
    Dim Rng As Range
    Dim Lastrow, ecol, eRow As Long
    Dim station As String
    Dim wb As Workbook, wb2 As Workbook

    Set wb = ActiveWorkbook
    wb.Sheets("Nov").Activate

    eRow = Cells(Rows.Count, 2).End(xlUp).Row
    station = Range("B2").Value
    Range(Cells(2, 2), Cells(eRow, 2)).Copy
    MsgBox "Transfer data for station: " & station

    wb.Sheets("Nov Template").Activate
    Set sht = ActiveWorkbook.Sheets("Nov Template")
    ecol = sht.UsedRange.Columns.Count

    sht.Range(Cells(1, 2), Cells(eRow, 2)).PasteSpecial xlPasteValues
    With ActiveSheet.UsedRange
        Lastrow = .Rows(.Rows.Count).Row
        Set Rng = Range(Cells(1, "C"), Cells(Lastrow, ecol))
        'myRange = sht.Rng.Value
    End With

    Workbooks.Open "G:\Mean2std\Merge NDj (2).xlsm"
    Set wb2 = ActiveWorkbook
    'wb2.Worksheets("GM").Range("B3:AO32").Value = myRange
    wb2.Worksheets("GM").Range("B3:AO32").Value = Rng.Value
    wb2.Close SaveChanges:=True
End Sub