【问题标题】:copy and pasting area not the same size?复制粘贴区域大小不一样?
【发布时间】:2020-05-19 21:22:51
【问题描述】:
Dim lastrow&, lastCol&, myarray As Range
lastrow = Range("A1").End(xlDown).Row
lastCol = Range("XX1").End(xlToLeft).Column
Set myarray = Range("A1").Resize(lastrow, lastCol)
Range("A1", myarray).Select

所以我添加了上面的代码来识别最后一列和最后一行并复制数组

 Selection.Copy
    Application.CutCopyMode = False
    Selection.Copy
    Application.WindowState = xlNormal
    Windows("Ex-Pakistan Calculator Final.xlsm").Activate
    Sheets("MRG").Select
    'has to find the last row by itself
    Range("A" & Rows.Count).End(xlUp).Offset(2, 0).Select
    ActiveSheet.Paste

在最后一行“activesheet.paste”出现错误,提示复制和粘贴区域大小不同,请尝试选择一个单元格。 enter image description here

问题是,"Range("A" & Rows.Count).End(xlUp).Offset(2, 0).Select" 只选择一个单元格,所以我看不到问题。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    以下是使用范围选择进行复制和粘贴的理想方式。您可以根据需要更改此代码。

    Sub CopyPaste()
        Dim selectRange As range
        Dim lastrow As Integer
        Application.CutCopyMode = False
        Sheets("Sheet1").Activate
        lastrow = range("A1").End(xlDown).Row
        Set selectRange = range("A1:A" & lastrow)
        selectRange.Copy
        Sheets("Sheet2").range("B1:B" & lastrow).PasteSpecial xlPasteAll
    End Sub
    

    【讨论】:

      【解决方案2】:

      恭喜您开始使用 VBA。您的代码中有几处可以改进。您希望避免使用 select(常见的初学者任务)。您甚至不需要在工作表中移动,甚至不需要使用复制/粘贴。

      但是,请参阅下面我用一些语句分解您的代码以停止并检查您所在的位置。我认为这将实现您想要的,但也可以帮助您更好地掌握您正在做的事情(这总是一场战斗的开始!)

      继续战斗。

      Sub adfa()
      Const turnOnStops As Boolean = True 'change to true or false to review code
      
          Dim WS_Pull As Worksheet:
              Set WS_Pull = ActiveSheet 'better to define this with actual sheet name
      
          Dim lastrow As Long:
              lastrow = WS_Pull.Cells(Rows.Count, 1).End(xlUp).Row 'this assumes column a has the bottom row and no rows hidden
          If turnOnStops Then
              Debug.Print "Lastrow is " & lastrow
              Stop
          End If
      
      
      
          Dim lastcol As Long:
              lastcol = WS_Pull.Cells(1, Columns.Count).End(xlToLeft).Column 'same assumptions but with columns on row 1 instead of columna a
          If turnOnStops Then
              Debug.Print "lastcol is " & lastcol
              Stop
          End If
      
      
      
      
          Dim myarray As Range:
              Set myarray = WS_Pull.Range("A1").Resize(lastrow, lastcol) ' I'm not sure what you're trying to do here.
      
      
          If turnOnStops Then
              Dim theAnswer As Long
                  theAnswer = MsgBox("The address of myArray is " & myarray.Address & ". Stop code?", vbYesNo)
                  If theAnswer = vbYes Then Stop
          End If
      
      
          Dim WS_paste As Worksheet: Set WS_paste = Sheets("MRG")  'it would be better to use the SHEET (shown in the VBA project)
      
      
          WS_Pull.Range("A1", myarray).Copy '<--- what are trying to copy.
      
          If turnOnStops Then
                  theAnswer = MsgBox("The area copied was " & WS_Pull.Range("A1", myarray).Address & ". Stop code?", vbYesNo)
                  If theAnswer = vbYes Then Stop
          End If
      
      
          If turnOnStops Then
              theAnswer = MsgBox("The area you are going to paste to is " & _
              WS_paste.Cells(1, Rows.Count).End(xlUp).Offset(2, 0).Address & " stop code?", vbYesNo)
              If theAnswer = vbYes Then Stop
          End If
      
      
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多