我想使用输入框按用户选择一列并将其应用于每个行索引,但一直没有成功。它说类型不匹配。有很多东西要学,而且缺乏编码经验。提前感谢任何意见。
Sub FormatRange()
Dim i As Long, r As Range
Set r = Application.InputBox("Pick Column", , , , , , , 8)
For i = 3 To 1500
If r.Value <> "" Then r.Interior.Color = vbYellow
Next i
End Sub
答案1
不知何故,经过反复试验,我设法利用了正确的列索引语法。([RowIndex], [ColumnIndex])
如果我们想要列索引,使用单元格似乎是编写代码的正确方法,而不是尝试Range(Cell1,[Cell2])
。我还有很多东西要学,这让我很感兴趣。
Sub FormatRange()
Dim i As Long, r As Range
On Error Resume Next 'Suppress error if user presses cancel
Set r = Application.InputBox("Pick Column", , , , , , , 8)
On Error GoTo 0 'Remove the On Error Resume Next condition
If rColumn Is Nothing Then Exit Sub 'Pressed cancel
For i = 3 To 1500
set r = cells(i,r.column) '>>Syntax to utilize Column Index Number
If r.Value <> "" Then r.Interior.Color = vbYellow
Next i
End Sub