我试图为Excel实现logger,WONT logg用户更改为单元格值(谁关心...)。
但我的应用程序的内部动态。
所以:
到目前为止,这是我的代码:
Public Sub Log(level As LoggerSeverityLevel, functionName As String, message As String, Optional Arguments As Variant)
Dim sh As Object
Set sh = ActiveSheet
LoggerDB.Activate
''' Find blank row for next entry
Dim firstEmptyRow As LongLong
firstEmptyRow = Range("A" & rows.count).End(xlUp).row + 1
''' Parse level into human redable format
Dim lvlMessage As String
lvlMessage = "Unknown"
If level = lslInfo Then lvlMessage = "Info"
If level = lslWarning Then lvlMessage = "Warning"
If level = lslDebug Then lvlMessage = "Debug"
If level = lslCritical Then lvlMessage = "Critical"
''' Insert data
LoggerDB.Cells(firstEmptyRow, 1) = Now()
LoggerDB.Cells(firstEmptyRow, 2) = lvlMessage
LoggerDB.Cells(firstEmptyRow, 3) = functionName
LoggerDB.Cells(firstEmptyRow, 4) = message
''' And optional arguments, one cell per argument
Dim i As Long
Dim arg As Variant
i = 4
For Each arg In Arguments
LoggerDB.Cells(firstEmptyRow, i) = CStr(arg)
i = i + 1
Next arg
sh.Activate
End Sub
答案 0 :(得分:0)
Public Sub Log(level As LoggerSeverityLevel, functionName As String, message As String, Optional Arguments As Variant)
Dim sh As Object
Set sh = ActiveSheet
LoggerDB.Activate
''' Find blank row for next entry
Dim firstEmptyRow As LongLong
firstEmptyRow = Range("A" & rows.count).End(xlUp).row + 1
''' Parse level into human redable format
Dim lvlMessage As String
lvlMessage = "Unknown"
If level = lslInfo Then lvlMessage = "Info"
If level = lslWarning Then lvlMessage = "Warning"
If level = lslDebug Then lvlMessage = "Debug"
If level = lslCritical Then lvlMessage = "Critical"
''' Insert data
LoggerDB.Cells(firstEmptyRow, 1) = Now()
LoggerDB.Cells(firstEmptyRow, 2) = lvlMessage
LoggerDB.Cells(firstEmptyRow, 3) = functionName
LoggerDB.Cells(firstEmptyRow, 4) = message
''' And optional arguments, one cell per argument
Dim i As Long
Dim arg As Variant
i = 5
Dim tmp As Variant
Dim coll As Collection
此部分将标量(数字,字符串,bool,无论单个值)打包到集合中。 因此,我总是可以假设,如果传递任何其他参数,For Each将起作用。
它在TypeName()上传递,返回字符串为“()”的字符串。
如果Not IsMissing(Arguments)那么 '如果不能将每个(单个变量)包迭代到集合中。 如果不是(InStr(TypeName(Arguments),“()”)<> 0或TypeName(Arguments)=“Collection”或TypeName(Arguments)= “字典”)然后 设置coll = New Collection coll.add参数 设置tmp = coll 其他 tmp =参数 结束如果
For Each arg In tmp
LoggerDB.Cells(firstEmptyRow, i) = CStr(arg)
i = i + 1
Next arg
End If
sh.Activate
End Sub