Consulting

Results 1 to 4 of 4

Thread: Non Looping For Next Loop

  1. #1
    VBAX Contributor
    Joined
    Sep 2007
    Posts
    119
    Location

    Non Looping For Next Loop

    Hi there,

    I have a data sheets with all raw data. The extraction of data based on currency in Column A and copy the the required data to the respective currency tab. The for next statement only working for the 1st found item only. Can anyone repair the error in the coding per test file attached.


    Thanks
    Attached Files Attached Files

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    I think you'll get closer without the 2 lines marked with a ---------------------

    You keep reseting LRDest


    Option Explicit
    
    Sub FCLedger()
    
        Dim WSFCData As Worksheet
        Dim WS As Worksheet
        Dim LRData As Long
        Dim LRDest As Long
        Dim i As Long
        Dim FCname As Range
        Dim shtName As String
        
        Set WSFCData = Worksheets("Txn Data")
        
        With WSFCData
            LRData = .Cells(.Rows.Count, "A").End(xlUp).Row
          
            For i = 2 To LRData
               'If .Range("A2:A" & i) = shtName Then
               For Each FCname In .Cells(i, 1)
                   shtName = FCname.Text
                   Set WS = Worksheets(shtName)
        
                   LRDest = WS.Cells(.Rows.Count, "A").End(xlUp).Row + 1
    '--------               LRDest = 4
                   WS.Range("A4:J" & LRDest).Clear
        
                    Sheets(shtName).Range("A" & LRDest) = .Range("A" & i).Value
                   ' WSUSD.Range("A" & LRUSD) = .Range("A" & i)
                    Sheets(shtName).Range("B" & LRDest) = .Range("B" & i).Value
                    Sheets(shtName).Range("C" & LRDest) = .Range("C" & i)
                    'WSUSD.Range("C" & LRUSD) = .Range("N" & i)
                    'WSUSD.Range("G" & LRUSD) = .Range("C" & i)
                    'WSUSD.Range("D" & LRUSD) = .Range("D" & i)
                    'WSUSD.Range("E" & LRUSD) = .Range("F" & i)
                    'WSUSD.Range("G" & LRUSD) = .Range("H" & i)
                    'WSUSD.Range("H" & LRUSD) = .Range("E" & i)
                   ' WSUSD.Range("I" & LRUSD) = .Range("G" & i)
                   ' WSUSD.Range("K" & LRUSD) = .Range("L" & i)
                   ' WSUSD.Range("F4").Formula = "=sum(F3+D4-E4)"
                    'WSUSD.Range("J4").Formula = "=sum(J3+H4-I4+K4)"
                    
    '---------                LRDest = LRDest + 1
               Next
           Next
            'WSUSD.Range("F4:F" & LRUSD - 1).FillDown
            'WSUSD.Range("J4:J" & LRUSD - 1).FillDown
            'WS.Range("B4:B" & LRDest - 1) = Format(Date, "dd-mmm-yy")
            
             Sheets(shtName).Range("B4:B" & LRDest - 1) = Format(Date, "dd-mmm-yy")
        End With
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Contributor
    Joined
    Sep 2007
    Posts
    119
    Location
    Quote Originally Posted by Paul_Hossler View Post
    I think you'll get closer without the 2 lines marked with a ---------------------

    You keep reseting LRDest


    Option Explicit
    
    Sub FCLedger()
    
        Dim WSFCData As Worksheet
        Dim WS As Worksheet
        Dim LRData As Long
        Dim LRDest As Long
        Dim i As Long
        Dim FCname As Range
        Dim shtName As String
        
        Set WSFCData = Worksheets("Txn Data")
        
        With WSFCData
            LRData = .Cells(.Rows.Count, "A").End(xlUp).Row
          
            For i = 2 To LRData
               'If .Range("A2:A" & i) = shtName Then
               For Each FCname In .Cells(i, 1)
                   shtName = FCname.Text
                   Set WS = Worksheets(shtName)
        
                   LRDest = WS.Cells(.Rows.Count, "A").End(xlUp).Row + 1
    '--------               LRDest = 4
                   WS.Range("A4:J" & LRDest).Clear
        
                    Sheets(shtName).Range("A" & LRDest) = .Range("A" & i).Value
                   ' WSUSD.Range("A" & LRUSD) = .Range("A" & i)
                    Sheets(shtName).Range("B" & LRDest) = .Range("B" & i).Value
                    Sheets(shtName).Range("C" & LRDest) = .Range("C" & i)
                    'WSUSD.Range("C" & LRUSD) = .Range("N" & i)
                    'WSUSD.Range("G" & LRUSD) = .Range("C" & i)
                    'WSUSD.Range("D" & LRUSD) = .Range("D" & i)
                    'WSUSD.Range("E" & LRUSD) = .Range("F" & i)
                    'WSUSD.Range("G" & LRUSD) = .Range("H" & i)
                    'WSUSD.Range("H" & LRUSD) = .Range("E" & i)
                   ' WSUSD.Range("I" & LRUSD) = .Range("G" & i)
                   ' WSUSD.Range("K" & LRUSD) = .Range("L" & i)
                   ' WSUSD.Range("F4").Formula = "=sum(F3+D4-E4)"
                    'WSUSD.Range("J4").Formula = "=sum(J3+H4-I4+K4)"
                    
    '---------                LRDest = LRDest + 1
               Next
           Next
            'WSUSD.Range("F4:F" & LRUSD - 1).FillDown
            'WSUSD.Range("J4:J" & LRUSD - 1).FillDown
            'WS.Range("B4:B" & LRDest - 1) = Format(Date, "dd-mmm-yy")
            
             Sheets(shtName).Range("B4:B" & LRDest - 1) = Format(Date, "dd-mmm-yy")
        End With
    End Sub

  4. #4
    VBAX Contributor
    Joined
    Sep 2007
    Posts
    119
    Location
    I had tried remove the mentioned 2 lines but still not avail.

Posting Permissions

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