+ Reply to Thread
Results 1 to 6 of 6

Add loop to this section of my Macro

Hybrid View

  1. #1
    Registered User
    Join Date
    07-26-2012
    Location
    New York, NY
    MS-Off Ver
    Excel 2010
    Posts
    5

    Add loop to this section of my Macro

    hi there -

    I currently have a functioning macro that has a section that finds "s.w." in the contents of a cell in one column (column A) and then pastes a certain formula into the corresponding cell in an adjacent column (column E) . I want this particular part of my macro to loop through till the last row with data in column A or until there are no more "S.W" cell contents.

    This is what I want to loop:



    Dim r As Range
        Set r = ActiveSheet.Range("A11:A500").Find(What:="*S.W.*", LookAt:=xlPart)
        If Not r Is Nothing Then r.Offset(, 4).Formula = "=GL(R7C9,R3C9,RC[-2],R4C9)"


    The full macro looks like this:



    Sub Portfolio2GL()
    'DELETE SW
    Dim x As Integer
         
        With Columns("C:C")
            .Replace What:="sw", Replacement:=""
        End With
    
    'ADD ZERO
    'Declarations
    
    Dim cl As Range
    Dim I As Long, endrow As Long
    
    Application.ScreenUpdating = False
        'Converts the C column format to Text format
        Columns("C:C").NumberFormat = "@"
        'finds the bottom most row
        endrow = ActiveSheet.Range("C500").End(xlUp).Row
        '## Or, for Excel 2003 and prior: ##'
        'endrow = ActiveSheet.Range("C___").End(xlUp).Row
    
        'loop to move from cell to cell
        For I = 11 To endrow - 1
            Set cl = Range("C" & I)
            With cl
            'The Do-While loop keeps adding zeroes to the front of the cell value until it hits a length of 3
                Do While Len(.Value) < 3
                    .Value = "0" & .Value
                Loop
            End With
        Next I
    Application.ScreenUpdating = True
    
    'ADD MAIN FORMULA 
    
    Dim lastRow As Long
       lastRow = Range("D500").End(xlUp).Row - 1
       Range("E11").FormulaR1C1 = "=GL(R6C9,R3C9,RC[-2],R4C9)+GL(R5C9,R3C9,RC[-2],R4C9)"
       Range("E11").AutoFill Destination:=Range("E11:E" & Cells(Rows.Count, 2).End(xlUp).Row)
    
    ' REPLACE MAIN FORMULA with S.W. formula where necessary
    
    Dim r As Range
    
        Set r = ActiveSheet.Range("A11:A500").Find(What:="*S.W.*", LookAt:=xlPart)
        If Not r Is Nothing Then r.Offset(, 4).Formula = "=GL(R7C9,R3C9,RC[-2],R4C9)"
    
    End Sub
    THANKS!
    Last edited by alansidman; 12-02-2013 at 08:13 PM. Reason: code tags

  2. #2
    Forum Contributor
    Join Date
    03-28-2013
    Location
    *
    MS-Off Ver
    Excel 2010
    Posts
    226

    Re: Add loop to this section of my Macro

    are you looking for this?

    dim myCel as Range
    Dim lr As Long
    for each myCel in range("A11:A" & lr)
         if mycel.value="S.W." then
                   mycel.Offset(, 4).Formula = "=GL(R7C9,R3C9,RC[-2],R4C9)"
    next myCel

    or this

    Dim myCel As Range
    Dim r As Range
    Dim lr As Long
        lr = Cells(Rows.Count, 1).End(xlUp).Row
    For Each myCel In Range("a1:a" & lr)
         Set r = myCel.Find(What:="*S.W.*", LookAt:=xlPart)
          If Not r Is Nothing Then myCel.Offset(, 4).Formula = "=GL(R7C9,R3C9,RC[-2],R4C9)"
    Next myCel
    Last edited by venkatpvc; 12-02-2013 at 08:04 PM.
    Give Feedback and Click(*)

  3. #3
    Registered User
    Join Date
    07-26-2012
    Location
    New York, NY
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Add loop to this section of my Macro

    The 2nd one did wonders! Thank you for your help

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,743

    Re: Add loop to this section of my Macro

    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



    (I have added them for you today. Please read all the forum rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  5. #5
    Forum Contributor
    Join Date
    03-28-2013
    Location
    *
    MS-Off Ver
    Excel 2010
    Posts
    226

    Re: Add loop to this section of my Macro

    glad it helpful, thank you for the feedback.

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,743

    Re: Add loop to this section of my Macro

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ 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. Replies: 1
    Last Post: 06-16-2013, 07:36 AM
  2. Deleting Row Section without Disrupting Formula Range Outside of Deleted Section
    By JeffNYG23 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-14-2013, 09:10 AM
  3. I need a macro that will let me loop a section of the vba code x number of times
    By tuckejam in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-28-2013, 02:44 PM
  4. Moving section headings from below section to above section..
    By Fayebaline in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 02-24-2011, 06:58 AM
  5. move that Row From the Waiting section to the completed section automaticly
    By jjsaw5 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-21-2007, 06:50 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