select案例中的应用程序定义或对象定义错误

嗨,大家好,我试图修改这个用于放置服务器types和主机名的vbamacros代码块,但是当我运行它时,它显示我运行时错误'1004':应用程序定义或对象定义的错误。 我不是那么熟悉VBA,当我试图解决这个问题时,我碰到了一个问题,希望我能从你们那里得到任何build议。

更新:我发现通过debugging该问题依赖于hostName = Left(systemName, WorksheetFunction.Find(":KUX", systemName) - 1) ,因为我有另外两个主机名涉及KUL,ithink这就是为什么ststem混淆,它停在KUL。 所以现在我不得不增加另一个案例来findKUL的主机。

 Private Sub CommandButton1_Click() Dim total_row As Integer Dim systemName As String 'Column C Dim newString As String Dim tabName As String Dim hostName As String 'Dim serverType As String 'Hostname and Server Type column total_row = WorksheetFunction.CountA(Range(Range("C2"), Range("C2").End(xlDown))) For i = 2 To total_row + 1 systemName = Cells(i, 3).Value 'Copy Size to Size_MB Column Cells(i, 6).Value = Cells(i, 5).Value 'Take Size_MB/1024 to get Size_GB Cells(i, 7).Value = Cells(i, 6).Value / 1024 'Take Size_GB/1024 to get Size_TB Cells(i, 8).Value = Cells(i, 7).Value / 1024 'Tab_Name column S(19) tabName = Cells(i, 19).Value Select Case tabName Case "LINUX" hostName = Left(systemName, WorksheetFunction.Find(":LZ", systemName) - 1) Cells(i, 2).Value = hostName Cells(i, 1).Value = GetServerType(hostName) Case "WINDOWS" hostName = Mid(systemName, WorksheetFunction.Find("Primary:", systemName) + 8, _ WorksheetFunction.Find(":NT", systemName) - WorksheetFunction.Find("Primary:", systemName) - 8) Cells(i, 2).Value = hostName Cells(i, 1).Value = GetServerType(hostName) Case "UNIX" hostName = Left(systemName, WorksheetFunction.Find(":KUX", systemName) - 1) Cells(i, 2).Value = hostName Cells(i, 1).Value = GetServerType(hostName) Case Else MsgBox "Tab Name don't match" Stop End Select 'Column M(13) = Space Used Percent, 'Column O(15) = Greater then 85% If (Cells(i, 13).Value > 85) Then Cells(i, 15).Value = "Yes" Else Cells(i, 15).Value = "No" End If 'Column Q(17) = Timestamp, Column R(18) = Date Cells(i, 18).Value = Mid(Cells(i, 17), 5, 4) 'To Generate Month Column p (16) Cells(i, 16).Value = GetMonth(Left(Cells(i, 18), 2)) 'Application Name - CEP (Column T)(20) (Note maybe need check if cannot find the application then display #NA) 'Note VLOOPUP must minus 3 cause loopup start from column D Cells(i, 20).Value = Application.VLookup(hostName, Sheet3.Range("D:V"), 19, False) 'LOB (BU) - CEP (Column U)(21) Cells(i, 21).Value = Application.VLookup(hostName, Sheet3.Range("D:X"), 21, False) 'System purpose (Column V)(22) Cells(i, 22).Value = Application.VLookup(hostName, Sheet3.Range("D:V"), 16, False) 'Tower (Column W)(23) Cells(i, 23).Value = Application.VLookup(hostName, Sheet3.Range("D:Y"), 22, False) 'Harddisk (Column X)(24) (from column M in MASTERCEP) Cells(i, 24).Value = Application.VLookup(hostName, Sheet3.Range("D:V"), 10, False) 'HW Model (Column Y)(25) Cells(i, 25).Value = Application.VLookup(hostName, Sheet3.Range("D:V"), 5, False) 'MasterBU (Column Z)(26) Cells(i, 26).Value = Application.VLookup(Cells(i, 21).Value, Worksheets("MASTERBU").Range("A:B"), 2, False) 'Ts/Others (Column AC)(29) Cells(i, 29).Value = Application.VLookup(hostName, Sheet2.Range("A:B"), 2, False) 'SL (Column AD)(30) Cells(i, 30).Value = Application.VLookup(hostName, Sheet3.Range("D:W"), 20, False) 'Mount_Check (Column AE)(31) 'Note: Cells(i,4) is the Mount_Point Column 'Index Match If (Application.WorksheetFunction.IsNA(Application.VLookup(Cells(i, 2), Worksheets("IndexMatch").Range("B:B"), 1, False))) Then Cells(i, 31).Value = "#N/A" Else Cells(i, 31).Value = Application.Index(Worksheets("IndexMatch").Range("G:G"), Application.Match(Cells(i, 4), Worksheets("IndexMatch").Range("D:D"), 0)) End If Next i End Sub Function GetServerType(host_name As String) As String Select Case Left(host_name, 1) Case "A", "a", "p" GetServerType = "AIX" Case "S", "s" GetServerType = "SUN" Case "X", "x", "W", "w", "P" GetServerType = "WINTEL" Case Else GetServerType = "" End Select End Function Function GetMonth(twoDigitMonth As String) As String Select Case twoDigitMonth Case "01" GetMonth = "Jan" Case "02" GetMonth = "Feb" Case "03" GetMonth = "Mar" Case "04" GetMonth = "Apr" Case "05" GetMonth = "May" Case "06" GetMonth = "Jun" Case "07" GetMonth = "Jul" Case "08" GetMonth = "Aug" Case "9" GetMonth = "Sep" Case "10" GetMonth = "Oct" Case "11" GetMonth = "Nov" Case "12" GetMonth = "Dec" End Select End Function 

您将很难从失败的工作表函数中获取debugging信息,因此请尝试替代路由。

用下面的语句replace“UNIX”语句。

  Case "UNIX" If CBool(InStr(1, systemName, ":KUX", vbTextCompare)) Then hostName = Left(systemName, InStr(1, systemName, ":KUX", vbTextCompare) - 1) ElseIf CBool(InStr(1, systemName, ":KUL", vbTextCompare)) Then hostName = Left(systemName, InStr(1, systemName, ":KUL", vbTextCompare) - 1) Else hostName = vbNullString Debug.Print systemName End If Cells(i, 2).Value = hostName Cells(i, 1).Value = GetServerType(hostName) 

如果通过跳过一个条目或中途崩溃运行,您将能够检查VBE的立即窗口( Ctrl + G )在列C中的违规值。