2012-01-02 118 views
1

我正在尝试创建一个可能最终变得相当大的excel宏,以使事情变得更容易我正在一次处理它。到目前为止,我有....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' 

任何人都可以提出我要去哪里错了吗?

+0

您是否尝试记录过程?记录宏函数通常会提示您出错的地方。尝试复制某些东西并粘贴到其他地方,同时记录并研究该代码。 – Default 2012-01-02 15:15:06

回答

4

你的错误是:

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 

也就是说从底部从顶部不上下移动。

P.S. 2

要使用的细胞指定一个范围:

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

的点必须匹配:所有三个或无。

+0

谢谢,这工作完美。感谢您解释我哪里错了!我现在正忙于重命名,因为我有两列名为'product_name',我怎么能让其中一个名称改为product_name_1,另一个是product_name_2? – fightstarr20 2012-01-02 16:42:59

+0

我已为您的额外问题添加了答案。希望这是明确的。如果你接受我的回答,我将不胜感激。我必须承认我是这样做的,而不是爱我的同胞或女人。 – 2012-01-02 17:22:13

+0

完美:)谢谢! – fightstarr20 2012-01-02 17:27:33

0

我不知道你在哪里试图复制到, 但是当你要粘贴你需要做一个选择,然后
ActiveSheet.Paste
例如:

/您代码/
Selection.Copy

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

0

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

,而不是例如:

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

我会用:

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

如果我们替代品进入你的代码,包括一些由托尼提出的意见:

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