代码之家  ›  专栏  ›  技术社区  ›  Mike D

Excel(2007)VBA复制粘贴

  •  1
  • Mike D  · 技术社区  · 14 年前

    主服务器最终将为每个供应商提供一个选项卡。

    这是我现在的密码。。

    我一直收到错误1004粘贴方法失败

    'Now copy from the Update Master to the Cust Master...
    mWrk = "A1:Z" & Trim(Str(TotRows))   <---TotRows is the total # of rows used
    
    
    
    Application.CutCopyMode = XLCopy
    Worksheets(WhichFile).Range(mWrk).Copy  <-- WhichFile has the value of the sheet name..
    
    
    Dim mXLCopy As Workbook
    Set mXLCopy = Workbooks.Open(ThisWorkbook.Path & "\Customer Master Price.xlsx")
    
    ' See if the sheet is already there. if so delete it.
    Dim IsThere As Boolean
    IsThere = False
    For x = 1 To mXLCopy.Sheets.Count
        If UCase(mXLCopy.Sheets(x).Name) = UCase(WhichFile) Then
            IsThere = True
        End If
    Next x
    Application.DisplayAlerts = False
    If IsThere Then
        mXLCopy.Sheets(WhichFile).Delete
    End If
    
    '
    'Now add it & activate it..
    mXLCopy.Sheets.Add().Name = WhichFile
    mXLCopy.Activate
    
    With mXLCopy.Sheets(WhichFile)
        Range(mWrk).PasteSpecial xlPasteAll, xlPasteSpecialOperationNone  <- Fails here
    End With
    
    Application.DisplayAlerts = True
    
    mXLCopy.Save
    mXLCopy.Close
    Set mRange = Nothing
    Set mXLCopy = Nothing
    

    有什么想法吗?你一定要拿我开玩笑,但我需要一个答案,我的答案都不起作用。。。

    1 回复  |  直到 14 年前
        1
  •  1
  •   user425445 user425445    14 年前

    发生这种事的原因是因为 mXLCopy.Sheets(WhichFile).Delete

    必须重新排列代码,以便先删除并重新创建工作表,然后复制要粘贴的范围。