如果是从第一列到i-1列进行遍历,则代码为:
Sub s1()
Dim i%, k%
i = InputBox("input i:")
For k = 1 To i - 1
Columns(k).Select 'select可以更改为任意操作代码
Next
End Sub
如果是对从第一列到i-1列的所有列进行操作,则代码为:
Sub s1()
Dim i%
i = InputBox("input i:")
Columns(1).Resize(, i - 1).Select 'select可以更改为任意操作代码
End Sub
sub aa()
dim i,j
i=100
for j = 1 to i-1
columns(j).select
next
end sub
你这是要做啥?
上面代码是从第一列到99列,依次选中一下。
'下面是一个任意选取两个列数之间的l列的过程。直接带参数调用即可。参数可用数字或数字变量,也可用值为数字的表达式。即使参数不是数字,或数字不是整数,或数字超出列数范围,过程能智能判断,并给出提示信息。
'比如选取6到10(均含)之间的列的调用语句为:Call 选取两数字标识列之间的列(6,10),代码如以下三行(去掉前面的注释符):
'Sub 选取第6到10列之间的列()
' Call 选取两数字标识列之间的列(6, 10)
'End Sub
Sub 选取两数字标识列之间的列(Ing1 As Variant, Ing2 As Variant)
Dim IngMin As Integer
Dim IngMax As Integer
Dim I As Integer
Dim J As Integer
Dim strColL As String
Dim strColR As String
If IsNumeric(Ing1) And IsNumeric(Ing2) Then
IngMin = Application.WorksheetFunction.Min(Ing1, Ing2)
IngMax = Application.WorksheetFunction.Max(Ing1, Ing2)
If IngMin < 0 Or IngMax > 256 Then
MsgBox "列数参数溢出", , ""
Exit Sub
End If
I = (Len(Columns(IngMin).Address) - 1) / 2
J = (Len(Columns(IngMax).Address) - 1) / 2
strColL = Mid(Columns(IngMin).Address, 1, I)
strColR = Mid(Columns(IngMax).Address, 1, J)
Columns(strColL & ":" & strColR).Select
Else
MsgBox "列数参数错误", , ""
End If
End Sub
Sub test()
i = 100
Range("$A:" & Left(Columns(i - 1).Address, (Len(Columns(i - 1).Address) + 1) / 2 - 1)).Select
End Sub
你题意表达太不清晰了,不知道你真正的命题是什么。试试上面的语句吧。
你需要一次选到多少列, “i” 就随便你设置。