Solved

Run-time error '5' Excel VBA Script

Posted on 2014-04-17
3
499 Views
Last Modified: 2014-05-03
I have written a script to go through vulns and determine the application the vuln applies to. But i get a run-time errors and not sure why. Here is the code.
Sub Format()
    
    screenUpdateState = Application.ScreenUpdating
    statusBarState = Application.DisplayStatusBar
    calcState = Application.Calculation
    eventsState = Application.EnableEvents
    displayPageBreakState = ActiveSheet.DisplayPageBreaks
    
    Application.ScreenUpdating = False
    Application.DisplayStatusBar = False
    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False
    displayPageBreakState = ActiveSheet.DisplayPageBreaks
    
    Dim re As New RegExp
    
    re.Pattern = "MS[0-9][0-9]-[0-9][0-9][0-9]"
    re.IgnoreCase = True
    re.Global = True
    
    Sheetcount = ThisWorkbook.Worksheets.Count
    For N = 1 To Sheetcount
    'For N = 1 To 1
        Sheets(N).Select
        
        ActiveSheet.DisplayPageBreaks = False
        
        Worksheets(N).Range("H1").Value = "Vulnerability Type"
        Columns("H:H").EntireColumn.AutoFit
        Worksheets(N).Range("I1").Value = "Microsoft Severity"
        Columns("I:I").EntireColumn.AutoFit
        'Worksheets(N).Range("J1").Value = "CVE Severity"
        'Columns("J:J").EntireColumn.AutoFit
        numrows = Worksheets(N).UsedRange.Rows.Count
        'For C = 2 To numrows
        For C = 1590 To numrows
            If InStr(Range("D" & C).Value, "jre-") Then
                Range("H" & C).Value = "Java"
            ElseIf InStr(Range("D" & C).Value, "adobe-") Then
                Range("H" & C).Value = "Adobe"
            ElseIf InStr(Range("D" & C).Value, "oracle-") Then
                Range("H" & C).Value = "Oracle"
            ElseIf InStr(Range("D" & C).Value, "mfsa") Then
                Range("H" & C).Value = "Mozilla"
            ElseIf InStr(Range("D" & C).Value, "windows-") Then
                Range("H" & C).Value = "Windows"
            ElseIf Range("D" & C).Value = "" Then
                Range("H" & C).Value = ""
            Else
                Range("H" & C).Value = "Other"
            End If
            Set allMatches = re.Execute(Range("D" & C).Value)
            If allMatches.Count > 0 Then
                tempString = getMsSeverity(Left((Range("G" & C).Value), 8))
                If InStr(tempString, "Critical") Or InStr(tempString, "Important") Or InStr(tempString, "Moderate") Or InStr(tempString, "Low") Then
                    Range("I" & C).Value = Mid(tempString, 40, InStr(40, tempString, ":") - 41)
                Else
                    Range("I" & C).Value = "No Severity"
                End If
            End If
            'If Range("E" & C).Value <> "" Then
            '    Range("J" & C).Value = getCVESeverity(Left((Range("E" & C).Value), 13))
            'End If
        Next C
        
        ActiveSheet.DisplayPageBreaks = displayPageBreaksState
        
    Next N
    
    Application.ScreenUpdating = screenUpdateState
    Application.DisplayStatusBar = statusBarState
    Application.Calculation = calcState
    Application.EnableEvents = eventsState
    
End Sub

Function getMsSeverity(MS) As String
    Dim title As String
    Dim objHttp As Object
    
    Set objHttp = CreateObject("MSXML2.ServerXMLHTTP")
    objHttp.Open "GET", "http://technet.microsoft.com/en-us/security/bulletin/" & MS, False
    objHttp.Send ""

    title = objHttp.ResponseText

    If InStr(1, UCase(title), "<TITLE>") Then
        title = Mid(title, InStr(1, UCase(title), "<TITLE>") + Len("<TITLE>"))
        title = Mid(title, 1, InStr(1, UCase(title), "</TITLE>") - 1)
    Else
        title = ""
    End If

    getMsSeverity = title
End Function

Function getCVESeverity(CVE) As String
    Dim title As String
    Dim objHttp As Object
    
    Set objHttp = CreateObject("MSXML2.ServerXMLHTTP")
    objHttp.Open "GET", "http://web.nvd.nist.gov/view/vuln/detail?vulnId=" & CVE, False
    objHttp.Send ""

    title = objHttp.ResponseText

    If InStr(1, title, "<span class=""label"">CVSS v2 Base Score:</span>") Then
        title = Mid(title, InStr(1, title, "<span class=""label"">CVSS v2 Base Score:</span>"))
        title = Mid(title, 1, InStr(1, title, "</a>") - 1)
        title = Right(title, Len(title) - InStrRev(title, ">"))
    Else
        title = ""
    End If
    
    getCVESeverity = title
End Function

Open in new window

0
Comment
Question by:evilmonkey2148
  • 2
3 Comments
 
LVL 17

Expert Comment

by:bigeven2002
ID: 40007324
Hello,
We have seen the same error for one of our applications.  Not sure if it is relevant, but try changing your default printer to another (preferably local non-network) and then change it back to see if that works.
0
 

Accepted Solution

by:
evilmonkey2148 earned 0 total points
ID: 40027568
This was due to poor programming. Made the program more dynamic but pulling the needed information instead of static variables.
0
 

Author Closing Comment

by:evilmonkey2148
ID: 40039030
didn't do enough code review before submitting the question.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Alphabetical Order for Letters 2 21
Excel VLOOKUP. How to return "Not Found" if Item or Qty is missing in Lookup table? 11 38
And OR formula 5 22
Excel Formula 16 45
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question