+ Reply to Thread
Results 1 to 3 of 3

Exception Handling through Macros

Hybrid View

  1. #1
    Registered User
    Join Date
    10-04-2011
    Location
    zurich
    MS-Off Ver
    Excel 2003
    Posts
    34

    Exception Handling through Macros

    HI,

    I am calling the below macros for generating the data-load scripts, all the macros are executing fine, but I am not able to do the exception handling.


    
    This is my master macro which will generate a script based on the columns in A,B,C. The macro should not generate any script if
    any one of the cell value is empty. The macro is working fine as expected.
    
    Sub Create_Vscripts()
        Dim cell As Range
        Dim RNG As Range
        Vclear
        RR = Cells.Find("*", Cells(Rows.Count, Columns.Count), SearchOrder:=xlByRows, _
                SearchDirection:=xlPrevious).Row
        Set RNG = Range("E5:E154")
        For Each cell In RNG
            If Not Range("A" & cell.Row) = "" Then
                If Not Range("B" & cell.Row) = "" And _
                        Not Range("C" & cell.Row) = "" Then
                    cell.Value = "cdrtpy -n " & Replace(Range("C" & cell.Row), ".xml", "") & " -o " _
                            & Range("A" & cell.Row) & " -f " & Range("B" & cell.Row) & " -s" & " -b"
                End If
               End If
        Next cell
            Call Vclear_validation
    End Sub
    
    Second macro for clearing the previously generated data.Working as expected.
    
    Sub Vclear()
    Range("E5:E154").clear
    End Sub
    
    Third macro,is not working as I planned, the macro will generated a .bat file based on the values in the Range("E5:E154").
    if the first macro is not generating even a single line, the VScripts.bat should not be generated, but here in this case it is
    generating a empty VScripts.bat file. I want to generate this file, only if there is some value present in the range("E5:E154"). 
    
    Public Sub Vclear_validation()
        Const FILENAME = "C:\temp\VScripts.bat"
        Dim FileNumber As Long
        Dim retVal As Variant
        Dim cell As Range
        FileNumber = FreeFile
        Open FILENAME For Output As #FileNumber
            For Each cell In Range("E5:E154")
            Print #FileNumber, cell.Text
        Next cell
        Close #FileNumber
        
       Call Vclear_validation_Log
        
    End Sub
    
    Fourth Macro is also not working properly, the macro should generate Execute_VScripts.bat file, if and only if
     the VScripts.bat is present in the C:\temp location, if the file is not present Execute_VScripts.bat should not be generated.
    
    Public Sub Vclear_validation_Log()
             
        Const FILENAME = "C:\temp\Execute_VScripts.bat"
         
        Dim FileNumber As Integer
        Dim retVal As Variant
             
        FileNumber = FreeFile
            
        Open FILENAME For Output As #FileNumber
        
        Print #FileNumber, "VScripts.bat > VScripts & .Log"
    
        Close #FileNumber
         
            
    End Sub
    Please give me some hints to handle these exceptions.

    Regards,
    Swathi
    Attached Files Attached Files

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,789

    Re: Exception Handling through Macros

    The problem in your third Sub is not really a problem in your third Sub. In the exception case you shouldn't even be calling it. See update to master macro. This will also solve your second problem. If the third Sub is not called to begin with, then it won't call the fourth Sub.


    
    This is my master macro which will generate a script based on the columns in A,B,C. The macro should not generate any script if
    any one of the cell value is empty. The macro is working fine as expected.
    
    Sub Create_Vscripts()
        Dim cell As Range
        Dim RNG As Range
        Vclear
        RR = Cells.Find("*", Cells(Rows.Count, Columns.Count), SearchOrder:=xlByRows, _
                SearchDirection:=xlPrevious).Row
        Set RNG = Range("E5:E154")
        Dim changed as Boolean ' ADDED
        changed = False ' ADDED not really necessary but I make defaults explicit
        For Each cell In RNG
            If Not Range("A" & cell.Row) = "" Then
                If Not Range("B" & cell.Row) = "" And _
                        Not Range("C" & cell.Row) = "" Then
                    cell.Value = "cdrtpy -n " & Replace(Range("C" & cell.Row), ".xml", "") & " -o " _
                            & Range("A" & cell.Row) & " -f " & Range("B" & cell.Row) & " -s" & " -b"
                        changed = True ' ADDED
                End If
               End If
        Next cell
        If Changed Then Call Vclear_validation ' ADDED If/Then
    End Sub
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    10-04-2011
    Location
    zurich
    MS-Off Ver
    Excel 2003
    Posts
    34

    Re: Exception Handling through Macros

    Thanks a lot !.

    Regards,
    Swathi

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1