|
各位大大
在excel的VBA里做了一个点击单元格自动获取范围数据,生成一个序列的数据验证单元格。
但是每次保存后,再去打开Excel的时候就会报错
代码如下
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim d As Object, rng, rng1, rng2, i%
Set d = CreateObject("scripting.dictionary")
s = Target.Value
If Target.Row = 8 And Target.Column = 8 Then
Range("H8").Select
Set rng = Rows(10).Find("小计")
MsgBox (rng.Column - 1)
rng1 = Range(Cells(10, 9), Cells(10, rng.Column - 1))
rng2 = Application.Transpose(rng1)
For i = 1 To UBound(rng2)
d(rng2(i, 1)) = ""
Next
a = d.keys
Range("q1") = Range("q1") + 1
If Not rng Is Nothing Then
Dim str As String
str = "=$I$10:$" & ColumnName & "$10"
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=Join(a, ",") '自动有效性
End With
End If
End If
End Sub
验证报错时,发现 rng1 = Range(Cells(10, 9), Cells(10, rng.Column - 1))这句话,将Cells(10, rng.Column - 1))范围写成34以下就不会报错,写35以上就会报错了。实在无法理解。
求大大指导
|
|