0

我有一个名为“调查”的工作表。我正在尝试将复选框附加到 A 列中答案旁边的所有单元格,并且由于某种原因,我收到“需要对象”错误。开头附近的 4 行以“Set rng =”开头,被突出显示。

我对 VBA 很陌生,所以我不确定这是否只是一个我没有看到的简单语法问题。我已经尝试搜索正确的格式,但无济于事。任何人都可以帮忙吗?这是我得到的代码:

Sub AddCheckBox()

Dim rng As Range
Dim rcell As Range

Set rng = Survey.Range("A7:A10,A13:A17,A21:A25,A28:A33" _
& "A36:A43, A48,A51:A56,A60:A66,A69:A73,A76:A80" _
& "A83:A87, A90:A94, A97:A102, A105:A113, A116:A122, A125:A131" _
& "A134:A141,A145:A149, A152:A158, A161:A165")



DelCheckBox 

For Each rcell In rng
  With ActiveSheet.CheckBoxes.Add(rcell.Left, _
     rcell.Top, rcell.Width, rcell.Height)
     .LinkedCell = rcell.Offset(, 0).Address(External:=True)
     .Interior.ColorIndex = 14   'or  xlNone or xlAutomatic
     .Caption = ""
     .Border.Weight = xlThin
  End With
Next

With Range("A7:A10,A13:A17,A21:A25,A28:A33" _
& "A36:A43, A48,A51:A56,A60:A66,A69:A73,A76:A80" _
& "A83:A87, A90:A94, A97:A102, A105:A113, A116:A122, A125:A131" _
& "A134:A141,A145:A149, A152:A158, A161:A165")
.Rows.RowHeight = 15
End With
End Sub


Sub DelCheckBox()
For Each cell In Range("A1:A166")
    Worksheets("Survey").CheckBoxes.Delete
Next
End Sub
4

1 回答 1

1

您在行尾缺少逗号。尝试这个:

Set rng = Survey.Range("A7:A10,A13:A17,A21:A25,A28:A33," _
& "A36:A43, A48,A51:A56,A60:A66,A69:A73,A76:A80," _
& "A83:A87, A90:A94, A97:A102, A105:A113, A116:A122, A125:A131," _
& "A134:A141,A145:A149, A152:A158, A161:A165")

请注意,您还必须在具有 With Range("....") 块的地方进行相同的更改。此外,上面的代码并不能反映您正在尝试做的其他事情的有效性......只是一个错误。

编辑以解决问题...

尝试所有这些代码,看看它是否符合您的要求:

 Sub test()

Dim rng As Range
Dim rcell As Range

Set rng = Sheets("Survey").Range("A7:A10,A13:A17,A21:A25,A28:A33," _
& "A36:A43, A48,A51:A56,A60:A66,A69:A73,A76:A80," _
& "A83:A87, A90:A94, A97:A102, A105:A113, A116:A122, A125:A131," _
& "A134:A141,A145:A149, A152:A158, A161:A165")

DelCheckBox

For Each rcell In rng
  With Sheets("Survey").CheckBoxes.Add(rcell.Left, _
     rcell.Top, rcell.Width, rcell.Height)
     .LinkedCell = rcell.Offset(, 0).Address(External:=True)
     .Interior.ColorIndex = 14   'or  xlNone or xlAutomatic
     .Caption = ""
     .Border.Weight = xlThin
  End With
Next

rng.Rows.RowHeight = 15
End Sub
Sub DelCheckBox()

Sheets("Survey").DrawingObjects.Delete

End Sub
于 2013-06-19T16:44:50.793 回答