+ Reply to Thread
Results 1 to 4 of 4

Error handling loop vba

Hybrid View

JSmith1504 Error handling loop vba 04-21-2015, 07:12 AM
Norie Re: Error handling loop vba 04-21-2015, 07:17 AM
Kyle123 Re: Error handling loop vba 04-21-2015, 07:21 AM
JSmith1504 Re: Error handling loop vba 04-21-2015, 07:26 AM
  1. #1
    Registered User
    Join Date
    02-11-2014
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    27

    Error handling loop vba

    Hi all

    I have a for loop as follows:

    'initiate inner loop
    For j = 1 To LastRowCashAcc

    'Buy side checks (includes generic such as Tcodes and base currency)
    If BUYcashStr = wbDeb.Worksheets("All Cash Accounts").Cells(j, 2) Then
    'Check buy cash account currencies
    BUYacc_ccy = wbDeb.Worksheets("All Cash Accounts").Cells(j, 4)
    End If
    Next j

    When BUYcashStr does not appear in the wbDeb worksheet I obviously get an error. I need to handle this to skip to the next iteration i.e. Next j?

    Many thanks

    Jack

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Error handling loop vba

    Jack

    Do you definitely get an error?

    I ask becaus I can't quite see how no finding match for a value would cause an error.

    By the way, your code is already set up to skip to the next iteration of the loop if BUYcashStr isn't found in the current row on 'All Cash Accounts.

    PS Ever considered using VLOOKUP?
    If posting code please use code tags, see here.

  3. #3
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Error handling loop vba

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

  4. #4
    Registered User
    Join Date
    02-11-2014
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: Error handling loop vba

    Hi I have truncated the loop apologies - I think the error actually comes further down when trying to calculate pcTrans (type mismatch error)

     
    
    'Initiate loop
    For i = 12 To LastRowFX
    
        'Delete warnings and remove formatting
        Range(Cells(i, 15), Cells(i, 20)).ClearContents
        Range(Cells(i, 1), Cells(i, 15)).Font.Color = vbBlack
        Range(Cells(i, 15), Cells(i, 20)).Interior.Color = xlNone
    
        'Find location of first and second dash in the CIF (usually 5th and 13th digits)
        dash1 = WorksheetFunction.Find("-", Cells(i, 2))
        dash2 = WorksheetFunction.Find("-", Cells(i, 2), dash1 + 1)
        startCIF = Mid(Cells(i, 2), 1, dash2 - 1)
        
        'Add cash suffix to midcif to get BUY and SELL settle cash accounts
        buyStr = Cells(i, 5)
        sellStr = Cells(i, 8)
        BUYcashStr = startCIF & buyStr
        SELLcashStr = startCIF & sellStr
        
        'initiate inner loop
        For j = 1 To LastRowCashAcc
            
            'Buy side checks (includes generic such as Tcodes and base currency)
            If BUYcashStr = wbDeb.Worksheets("All Cash Accounts").Cells(j, 2) Then
                'Check buy cash account currencies
                BUYacc_ccy = wbDeb.Worksheets("All Cash Accounts").Cells(j, 4)
                'Check type of cash account
                BUYacc_type = wbDeb.Worksheets("All Cash Accounts").Cells(j, 3)
                'Check base currency
                Base_ccy = wbDeb.Worksheets("All Cash Accounts").Cells(j, 6)
                'Enter T-code
                Cells(i, 13) = wbDeb.Worksheets("All Cash Accounts").Cells(j, 8)
                
                'Check %P/F for buy transactions
                If Cells(i, 4) <> 0 Then
                QuoteCCY = Cells(i, 3)
                    'Loop to find rate
                    For ii = 1 To LastRowFXLkup
                        'Define buy ccy and buy multiplier
                        QuoteCCY_Lookup = wbDeb.Sheets("FX Rates").Cells(ii, 1)
                        FXrate = wbDeb.Sheets("FX Rates").Cells(ii, 2)
                        
                        'Get rate for ccy and store as varibale
                        If QuoteCCY = QuoteCCY_Lookup Then
                            USD_amt = wsFX.Cells(i, 4) * FXrate
                            pcTrans = USD_amt / wbDeb.Worksheets("All Cash Accounts").Cells(j, 7)
                        End If
                    Next ii
                End If
                
                'Check %P/F for sell transactions
                If Cells(i, 4) = 0 Then
                QuoteCCY = Cells(i, 6)
                    'Loop to find rate
                    For ii = 1 To LastRowFXLkup
                        'Define buy ccy and buy multiplier
                        QuoteCCY_Lookup = wbDeb.Sheets("FX Rates").Cells(ii, 1)
                        FXrate = wbDeb.Sheets("FX Rates").Cells(ii, 2)
                        
                        'Get rate for ccy and store as varibale
                        If QuoteCCY = QuoteCCY_Lookup Then
                            USD_amt = wsFX.Cells(i, 7) * FXrate
                            pcTrans = USD_amt / wbDeb.Worksheets("All Cash Accounts").Cells(j, 7)
                        End If
                    Next ii
                End If
    
    
            End If
            
            'Sell side checks
            If SELLcashStr = wbDeb.Worksheets("All Cash Accounts").Cells(j, 2) Then
                'Check buy cash account currencies
                SELLacc_ccy = wbDeb.Worksheets("All Cash Accounts").Cells(j, 4)
                'Check type of cash account
                SELLacc_type = wbDeb.Worksheets("All Cash Accounts").Cells(j, 3)
            End If
            
        Next j

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. For Loop and Error Handling
    By peetman in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-27-2015, 08:00 AM
  2. [SOLVED] Error Handling in a loop
    By Turtleman10 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-29-2013, 10:27 AM
  3. Error Handling in a Loop
    By amdk8800 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-28-2013, 03:46 PM
  4. Error handling within a Do Loop
    By Boondoggler in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-01-2012, 12:15 AM
  5. Error handling within loop
    By DesignMind in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-22-2009, 01:51 PM

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