我正在设置一个VBA宏,它会自动运行列中的数据列表并选出缺失的值。我现在获得的代码(见下文)有效,但我相信它开始在A2单元格中查找。我希望从B1开始。
我怎么能做这个改变?
道歉,我是VBA的初学者!
Sub Check_Sequential()
Dim LR As Long, i As Long
LR = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
ALR = ActiveSheet.Range("C" & Rows.Count).End(xlUp).Row + 1
x = 2
Cells(1, 3) = "Missing Numbers"
For i = 2 To LR
0
If Cells(i, 1) <> x Then
Cells(ALR, 3) = x
ALR = ActiveSheet.Range("C" & Rows.Count).End(xlUp).Row + 1
x = x + 1
Else
x = x + 1
End If
If Cells(i, 1) > x Then GoTo 0
If Cells(i, 1) = x Then
x = x + 1
End If
Next i
MsgBox "Done"
End Sub
答案 0 :(得分:0)
我亲爱的朋友史蒂文罗曼告诉我们,细胞不是官员&#34;集合或对象......但它确实很有用。 Cells等同于Application.Cells和Activesheet.Cells。
单元格(1,2)相当于Range(&#34; B1&#34;)。我喜欢Cells()比Range()更好,因为它可以像你的i和x那样获取行和列的整数变量,而Range并不喜欢它。
答案 1 :(得分:0)
要从搜索开始范围的单元格A2移动到B1,您需要对代码进行以下更改。这只是解决了您定义初始搜索的区域:
file = open('file_name')
d = {}
for line in file.readlines():
try:
key, value = line.replace('\n','').split('\t') ##if your sep == '\t'
except ValueError:
pass ### if line is empty
else: ### if all is ok
d[key] = value
print(d)
这应该允许你的代码工作,虽然正如其他一些人指出的那样,你可以考虑一些事情来进一步发展你的VBA技能:
Sub Check_Sequential()
Dim LR As Long, i As Long
LR = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row '// Changed from "A" to "B"
ALR = ActiveSheet.Range("C" & Rows.Count).End(xlUp).Row + 1
x = 2
Cells(1, 3) = "Missing Numbers"
For i = 1 To LR '// Chaged i to one to start at row 1 instead of 2
0
If Cells(i, 2) <> x Then '// Changed from i,1 to i,2
Cells(ALR, 3) = x
ALR = ActiveSheet.Range("C" & Rows.Count).End(xlUp).Row + 1
x = x + 1
Else
x = x + 1
End If
If Cells(i, 2) > x Then GoTo 0 '// Changed from i,1 to i,2
If Cells(i, 2) = x Then '// Changed from i,1 to i,2
x = x + 1
End If
Next i
MsgBox "Done"
End Sub
)ALR
&#34;是你如何恢复默认错误处理,这可能会误导On Error GoTo 0
&#34;编辑单元格值更清晰。属性,在这种情况下它将完全相同虽然这不是一个糟糕的代码,但请考虑将自己归类为初学者。