我正在尝试创建一个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'
目前,它可以正常工作,直到粘贴到这个新创建的列,然后我得到一个
'Run-time error '438'; - Object doesn't support this property or method'有人能告诉我哪里出了问题吗?
发布于 2012-01-02 15:34:59
您的错误是:
Range(rngAddress, rngAddress.End(xlDown)).Select这将从列的顶部向下选择到第一个空白单元格的上方。insert将列的这一部分右移,其余部分留在原处。当您再次选择时,您可能会得到更大的范围,因为您已经混合了两列。复制失败,因为您随后试图在值的顶部复制值。
如果这没有意义,请使用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注意:
With Sheets("XXX") .对第二个问题的回答
宏记录器不能很好地显示如何系统地定位单个细胞。
With Sheets("xxxx")
.Cells(RowNum,ColNum).Value = "product_name 1"
End With上面使用的是我推荐的With。注意细胞前面的点。
下面的操作操作在活动工作表上。
Cells(RowNum,ColNum).Value = "product_name 1"RowNum必须是一个数字。ColNum可以是数字(例如5),也可以是字母(例如"E")。
在您的例子中,RowNum是1,ColNum是ColToBeCopied和ColToBeCopied +1。
P.S.
我忘记提到要查找列的botton行,请使用:
RowLast = Range(Rows.Count, ColNum).End(xlUp).Row这是从底部向上移动,而不是从顶部向下移动。
P.S. 2
若要使用单元格指定范围,请执行以下操作:
.Range(.Cells(Top,Left),.Cells(Bottom,Right))这些点必须匹配:所有三个或没有。
发布于 2012-01-02 15:11:39
我不知道你想要复制到哪里,但是当你想粘贴时,你需要做一个选择,然后
ActiveSheet.Paste
例如:
/your码/
Selection.Copy 范围(“O:O”).Select
ActiveSheet.Paste发布于 2012-01-03 17:44:24
如果您只想传递值,我将完全避免复制/粘贴。
例如,而不是:
Range("B1:B100").Copy Destination:=Range("A1")我会用:
Range("A1:A100").Value = Range("B1:B100").Value如果我们要将其替换到您的代码中,并包括Tony的一些评论:
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 Subhttps://stackoverflow.com/questions/8702086
复制相似问题