Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA:指定给对象的宏,单击以执行操作时,它会进入代码和触发器-参数数量错误或属性分配无效_Excel_Vba_Excel 2016 - Fatal编程技术网

Excel VBA:指定给对象的宏,单击以执行操作时,它会进入代码和触发器-参数数量错误或属性分配无效

Excel VBA:指定给对象的宏,单击以执行操作时,它会进入代码和触发器-参数数量错误或属性分配无效,excel,vba,excel-2016,Excel,Vba,Excel 2016,我的VBA宏被指定给刚粘贴到工作表中的internet图像 我的宏将执行以下操作: 我有40列信息,我想在第1列和第2列、第2列和第3列之间添加一列。。等等 当我单击我的图像时,excel会进入代码,一旦进入该窗口,就会出现一个小窗口,显示: 参数数目错误或属性赋值无效 同时突出主体所在的第一行,我指的是这一部分: 列(“B:B”) 你能帮我解决这个问题吗? 对不起,我的业余问题 Sub columns() ' ' columns Macro ' ' columns("B:B").Se

我的VBA宏被指定给刚粘贴到工作表中的internet图像

我的宏将执行以下操作:

我有40列信息,我想在第1列和第2列、第2列和第3列之间添加一列。。等等

当我单击我的图像时,excel会进入代码,一旦进入该窗口,就会出现一个小窗口,显示:

参数数目错误或属性赋值无效

同时突出主体所在的第一行,我指的是这一部分:

列(“B:B”)

你能帮我解决这个问题吗? 对不起,我的业余问题

Sub columns()
'
' columns Macro
'

'
    columns("B:B").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("D:D").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("F:F").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("H:H").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("J:J").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("L:L").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("N:N").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("P:P").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("R:R").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("T:T").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("V:V").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("X:X").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("Z:Z").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("AB:AB").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("AD:AD").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("AF:AF").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("AH:AH").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("AJ:AJ").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("AL:AL").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("AN:AN").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("AP:AP").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("AR:AR").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("AT:AT").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("AV:AV").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("AX:AX").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("AZ:AZ").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    ActiveWindow.SmallScroll ToRight:=34
    columns("BB:BB").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("BD:BD").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("BF:BF").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("BH:BH").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("BJ:BJ").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("BL:BL").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("BN:BN").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("BP:BP").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("BR:BR").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("BT:BT").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("BV:BV").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("BX:BX").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    columns("BZ:BZ").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub

您可以使用您提供的全部代码,并将其精简为:

Sub insertColumns()

    Dim ws As Worksheet, i As Long
    Set ws = ThisWorkbook.Worksheets(1)

    For i = 2 To 78 Step 2
       ws.Columns(i).Insert
    Next

End Sub
易于阅读-(易于调试)且无需使用
。选择

工作表(1)
中的
1
替换为工作表索引号或工作表名称(用双引号括起来,
工作表(“工作表1”)


如果您愿意,可以将其设置为动态:

Sub insertColumns()

    Dim ws As Worksheet, i As Long
    Set ws = ThisWorkbook.Worksheets(1)

    For i = 2 To LastColumn(ws) * 2 Step 2
       ws.Columns(i).Insert
    Next

End Sub

Function LastColumn(ws As Worksheet, Optional rowNum As Long = 1)
    With ws
        LastColumn = .Cells(rowNum, .Columns.Count).End(xlToLeft).Column
    End With
End Function

这将确定您有多少个使用过的列,并在这些列之间循环-只需将
LastColumn
乘以2,因为您正在添加列。

您可以从开始。这不仅会大大降低代码的速度,而且还会造成调试难题,正如您所看到的。请使用
范围(“B:B”)。插入…
表格(“Sheet1”)。列(“B:B”)。插入…
,因为属性需要一个范围或表格。仅供参考,您的函数称为
columns
,并且您有(例如)
列(“B:B”)。选择
,这就是触发错误的原因