【问题标题】:Excel Macro giving error when pasting粘贴时Excel宏出错
【发布时间】:2015-02-10 04:36:12
【问题描述】:

我正在尝试创建一个可能最终会变得非常大的 excel 宏,以使事情变得更容易,我一次处理一点。到目前为止,我有....

Sub Macro4()
'
' Test Macro
'
  'Selects the product_name column by header name
  Dim rngAddress As Range
  Set rngAddress = Range("A1:Z1").Find("product_name")
  If rngAddress Is Nothing Then
  MsgBox "The product_name column was not found."
  Exit Sub
  End If
  Range(rngAddress, rngAddress.End(xlDown)).Select

  'Inserts new column to the left of the product_name column
   Selection.Insert Shift:=xlToRight

  'Re-selects the product_name column
   Range(rngAddress, rngAddress.End(xlDown)).Select

  'Copys the contents of the product_name column
   Selection.Copy
   Selection.Paste

End Sub

我希望它执行以下操作....

  • 在电子表格中搜索标题名称 'product_name'
  • 'product_name'列左侧插入一个空白列
  • 复制'product_name'列的内容
  • 将它们粘贴到新创建的空白列中
  • 将此新列中的标题名称更改为 'product_name_2'

目前它工作正常,直到粘贴到这个新创建的列中,然后我得到一个

'Run-time error '438'; - Object doesn't support this property or method'

谁能建议我哪里出错了?

【问题讨论】:

  • 您尝试记录过程吗? Record 宏功能通常会提示您出错的地方。尝试复制一些内容并将其粘贴到其他地方,同时记录和研究该代码。

标签: vba excel


【解决方案1】:

你的错误是:

Range(rngAddress, rngAddress.End(xlDown)).Select

这会从列的顶部向下选择到第一个空白单元格的正上方。插入将列的这一部分向右移动,将其余部分留在原处。当您再次选择时,您可能会获得更大的范围,因为您混合了两列。复制失败,因为您随后尝试将值复制到值的顶部。

如果这没有意义,请按 F8 单步执行您的宏,看看每一步发生了什么。

当您了解当前宏为何不起作用时,请尝试以下操作:

Sub Macro5()

  Dim rngAddress As Range
  Dim ColToBeCopied As Integer

  Set rngAddress = Range("A1:Z1").Find("'product_name")
  If rngAddress Is Nothing Then
    MsgBox "The product_name column was not found."
    Exit Sub
  End If

  ColToBeCopied = rngAddress.Column
  Columns(ColToBeCopied).EntireColumn.Insert
  Columns(ColToBeCopied + 1).Copy Destination:=Columns(ColToBeCopied)

End Sub

注意:

  1. 我没有选择任何东西。
  2. 我已将代码保留在活动工作表上,但最好使用With Sheets("XXX") ... End With

回答第二个问题

宏记录器不擅长展示如何系统地处理单个单元格。

With Sheets("xxxx")
  .Cells(RowNum,ColNum).Value = "product_name 1"
End With

以上使用我推荐的With。注意 Cells 前面的点。

下面的操作在活动工作表上。

Cells(RowNum,ColNum).Value = "product_name 1"

RowNum 必须是一个数字。 ColNum 可以是数字(比如 5)或字母(比如“E”)。

在您的情况下,RowNum 为 1,ColNum 为 ColToBeCopied 和 ColToBeCopied + 1。

附言

我忘了提到要查找列的底部行:

RowLast = Range(Rows.Count, ColNum).End(xlUp).Row

这是从底部向上移动,而不是从顶部向下移动。

附注2

使用单元格指定范围:

.Range(.Cells(Top,Left),.Cells(Bottom,Right))

点必须匹配:全部三个或一个都不匹配。

【讨论】:

  • 谢谢,效果很好。感谢您解释我哪里出错了!不过,我现在正在为重命名而苦苦挣扎,因为我有两列名为“product_name”,我怎样才能让其中一个重命名为 product_name_1,另一个重命名为 product_name_2?
  • 我已经为您的额外问题添加了答案。希望这很清楚。如果您接受我的回答,我将不胜感激。我必须承认我这样做是为了分数,而不是爱我的同胞。
【解决方案2】:

我不确定您要复制到哪里, 但是当您要粘贴时,您需要进行选择,然后
ActiveSheet.Paste
例如:

/你的代码/
选择.复制

范围(“O:O”)。选择
ActiveSheet.Paste

【讨论】:

    【解决方案3】:

    如果您只想转移值,我会完全避免复制/粘贴。

    例如,而不是:

    Range("B1:B100").Copy Destination:=Range("A1")
    

    我会使用:

    Range("A1:A100").Value = Range("B1:B100").Value
    

    如果我们将其替换到您的代码中,并包含一些由 Tony 制作的 cmets:

    Sub Macro4()
    
        Dim colFound  As Integer
        Dim rowLast As Long
    
        Const rowSearch As Integer = 1
    
        'Find the product_name column
        colFound = Rows(rowSearch).Find("product_name").Column
    
        If colFound = 0 Then
            MsgBox "The product_name column was not found."
            Exit Sub
        End If
    
        'Find the last non-empty row
        rowLast = Cells(Rows.Count, colFound).End(xlUp).Row
    
        'Inserts new column to the left of the product_name column
        Columns(colFound).EntireColumn.Insert
    
        'Transfer the contents of the product_name column to the newly inserted one
        Range(Cells(rowSearch, colFound), Cells(rowLast, colFound)).Value = _
         Range(Cells(rowSearch, colFound + 1), Cells(rowLast, colFound + 1)).Value
    
        'Rename the new column
        Cells(rowSearch, colFound).Value = Cells(rowSearch, colFound).Value & "_2"
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-05
      • 2022-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-27
      • 1970-01-01
      相关资源
      最近更新 更多