尝试从工作表中选择单元格时出现错误:
Dim ABHdataPath As String
Dim ABHdataFile As Excel.Workbook
ABHdataFile.Sheets("Booked").Activate ' make sure program starts from correct sheet
ColumnName = ColumnLetter(ABHdataFile.Sheets("Booked").Cells(5, Columns.Count).End(xlToLeft).Column)
ABHbooked = ABHdataFile.Sheets("Booked").Range("B5:" & ColumnName & Cells(Rows.Count, "B").End(xlUp).row).Value
在行上方运行时,出现此错误:
它没有给我调试代码的任何选择,因此我很难找到错误。
编辑 我已经能够调试我的代码,这就是它的结尾:
ABHBooked = ABHdataFile.Sheets("Booked").Range("B5:" & ColumnName & Cells(Rows.Count, "B").End(xlUp).row).Value
调试:
ABHBooked =空
Rows.Count = 1048576
xlUp = -4162
ColumnName = CD
答案 0 :(得分:0)
每当ColumnNumber小于1时,该函数都会产生错误。因此,引入一个小检查,它应该可以工作:
Function ColumnLetter(ColumnNumber As Long) As String
Dim n As Long
Dim c As Byte
Dim s As String
If ColumnNumber < 1 Then
ColumnLetter = "NAN"
Exit Function
End If
n = ColumnNumber
Do
c = ((n - 1) Mod 26)
s = Chr(c + 65) & s
n = (n - c) \ 26
Loop While n > 0
ColumnLetter = s
End Function
答案 1 :(得分:0)
问题不是我的变量没有存储为long
,而是我选择的格式。
此问题已解决:
Selection.NumberFormat = "General”