首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Excel宏粘贴错误

Excel宏粘贴错误
EN

Stack Overflow用户
提问于 2012-01-02 14:39:47
回答 3查看 7.7K关注 0票数 1

我正在尝试创建一个excel宏,这个宏可能最终会变得相当大,使事情变得更容易一些--我每次都在处理它。到目前为止我..。

代码语言:javascript
复制
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'

  • Insert 'product_name'列左侧的空白列

  • 'product_name'列的内容粘贴到新创建的空白column

  • Change中,这个新列中的标题名称到'product_name_2'

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

代码语言:javascript
复制
'Run-time error '438'; - Object doesn't support this property or method'

有人能告诉我哪里出了问题吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-01-02 15:34:59

您的错误是:

代码语言:javascript
复制
Range(rngAddress, rngAddress.End(xlDown)).Select

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

如果这没有意义,请使用F8遍历宏,并查看每一步发生了什么。

当您了解当前宏不工作的原因时,请尝试如下:

代码语言:javascript
复制
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. I没有选择任何东西。
    1. ,我已经将代码保留在活动表上,但是最好使用With Sheets("XXX") .

对第二个问题的回答

宏记录器不能很好地显示如何系统地定位单个细胞。

代码语言:javascript
复制
With Sheets("xxxx")
  .Cells(RowNum,ColNum).Value = "product_name 1"
End With

上面使用的是我推荐的With。注意细胞前面的点。

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

代码语言:javascript
复制
Cells(RowNum,ColNum).Value = "product_name 1"

RowNum必须是一个数字。ColNum可以是数字(例如5),也可以是字母(例如"E")。

在您的例子中,RowNum是1,ColNum是ColToBeCopied和ColToBeCopied +1。

P.S.

我忘记提到要查找列的botton行,请使用:

代码语言:javascript
复制
RowLast = Range(Rows.Count, ColNum).End(xlUp).Row

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

P.S. 2

若要使用单元格指定范围,请执行以下操作:

代码语言:javascript
复制
.Range(.Cells(Top,Left),.Cells(Bottom,Right))

这些点必须匹配:所有三个或没有。

票数 4
EN

Stack Overflow用户

发布于 2012-01-02 15:11:39

我不知道你想要复制到哪里,但是当你想粘贴时,你需要做一个选择,然后

ActiveSheet.Paste

例如:

/your码/

代码语言:javascript
复制
Selection.Copy  

范围(“O:O”).Select

代码语言:javascript
复制
ActiveSheet.Paste
票数 0
EN

Stack Overflow用户

发布于 2012-01-03 17:44:24

如果您只想传递值,我将完全避免复制/粘贴。

例如,而不是:

代码语言:javascript
复制
Range("B1:B100").Copy Destination:=Range("A1")

我会用:

代码语言:javascript
复制
Range("A1:A100").Value = Range("B1:B100").Value

如果我们要将其替换到您的代码中,并包括Tony的一些评论:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8702086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档