如何在excel vba中粘贴整个列而不选择工作表
我正试图将第一列复制到最后一列。这个代码怎么了?获取以下错误:如何在excel vba中粘贴整个列而不选择工作表,excel,vba,macos,Excel,Vba,Macos,我正试图将第一列复制到最后一列。这个代码怎么了?获取以下错误: Run-time error '438 object doesn't support this property or method vba是否允许对列进行复制,但不允许粘贴 Sub copy_ids_user_output(sheet_name As String) ' find last column Dim last_col As Integer last_col = Worksheets(sheet_
Run-time error '438
object doesn't support this property or method
vba是否允许对列进行复制
,但不允许粘贴
Sub copy_ids_user_output(sheet_name As String)
' find last column
Dim last_col As Integer
last_col = Worksheets(sheet_name).Cells(1, Columns.Count).End(xlToLeft).column
Debug.Print last_col
Columns(1).Copy
Columns(last_col + 1).Paste
End Sub
您应该选择目标,然后使用粘贴: 而不是:
Columns(last_col + 1).Paste
使用
如果可能,应避免在宏中敲击用户的剪贴板。直接复制该列即可
Columns(1).Copy Columns(last_col + 1)
如果可能,我希望避免选择工作表。代码选择列,而不是工作表。除非是活动工作表,否则无法在工作表上选择范围。源或目标中是否有合并的单元格?每次你尝试复制/粘贴时,Excel都会对此感到恶心。完美。像lightning.dwstein一样,我还打算建议列(最后一列+1)。粘贴特殊的xlPasteAll,因为您不必参考选择工作表。这个方法添加到剪贴板上,并且是两行代码,所以我认为Tmdean的建议更精简。Tmdean-那很酷,我不知道这种方法。是的,我发现一种情况是,你必须弄乱剪贴板,那就是你只需要粘贴值。您可以将Range.Value分配给变量数组,以便一次分配多个值,但如果使用数千个单元格执行此操作,则会出现性能问题。
Columns(1).Copy Columns(last_col + 1)