【问题标题】:Excel VBA Copy Destination syntax with variables for Workbook/Worksheet带有工作簿/工作表变量的 Excel VBA 复制目标语法
【发布时间】:2018-02-21 12:31:20
【问题描述】:

我将数据从一个工作簿复制到另一个工作簿,都声明为变量,但我的复制目标方法向我们抛出运行时错误 13“类型不匹配”或运行时错误 438“对象不支持此属性或方法”。

你能帮我解释一下语法吗?有没有可能有类似的东西

Range("").Copy Destination:=wb1.ws.Range("a" & Rows.Count).End(xlUp).Offset(1)

Range("").Copy Destination:=wb1.ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)

我的代码

Sub Sundry_AllFiles(wb1, wbsource, rl, wsctrl, Mths6, Mths12)
(==Declared elsewhere - Dim wb1,wbsource as Workbook
Set wb1 = ThisWorkbook
Set wbsource = Workbooks.Open(FPath & Finame, ReadOnly:=True, Local:=True)
===)
Dim ws As Worksheet
Dim r, rw As Long
Dim fnd As String
Dim fnm As String

fnd = "TOTAL"
fnm = wbsource.Name
Set ws = wb1.Worksheets("Sundry")

'=====Macro runs from wb1 and analyses data in the wbsource

If IsEmpty(Range("A1")) = True Then
wbsource.Close SaveChanges:=False
Exit Sub

Else
            Application.CutCopyMode = False 'POSSIBLE SOLUTION DELETE IF DIDNT WORK
            Columns("A:A").Select
            Selection.Insert shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
            With wbsource.Worksheets(1)
                    For rw = 2 To .Cells(Rows.Count, 6).End(xlUp).Row
                    .Cells(rw, 1) = Application.VLookup(fnd, wbsource.Worksheets(1). _
                    Range("B:E"),4, False)
                    Next rw

                For rw = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
                .Cells(rw, 15) = "=IF(RC[-13]<=TODAY()-90,""Yes"",""No"")"
                Next rw
                .AutoFilterMode = False
            End With

            'Autofilter by tranDate>90 days, copy and append to the ws sheet
            With Range("N" & Rows.Count).End(xlUp)
            .AutoFilter 15, "Yes"
            With wbsource.Worksheets(1)
            Dim LR As Long
            On Error Resume Next
            LR = Range("N" & Rows.Count).End(xlUp).Row
            Range("A2:N" & LR).SpecialCells(xlCellTypeVisible).Copy _
Destination:=wb1.ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)

'#Alternative to Copy Destination that works
            'wb1.Activate
            'ws.Range("a" & Rows.Count).End(xlUp).Offset(1).PasteSpecial

            End With
            End With

            wbsource.Close SaveChanges:=False
End If
End Sub

【问题讨论】:

  • 目的地应该是Destination:=ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0),因为您已经在wb 中将ws 定义为工作表。
  • 现在明白了,谢谢,它成功了!如何将评论标记为答案?

标签: excel vba


【解决方案1】:

而不是

Destination:=wb.ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)

目的地应该是

Destination:=ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)

因为您已经将 ws 定义为 wb 中的工作表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    相关资源
    最近更新 更多