+ Reply to Thread
Results 1 to 2 of 2

Loop and append

Hybrid View

DJP1339 Loop and append 07-12-2011, 05:14 PM
DJP1339 Re: Loop and append 07-12-2011, 05:36 PM
  1. #1
    Registered User
    Join Date
    07-12-2011
    Location
    Ny
    MS-Off Ver
    Excel 2007
    Posts
    3

    Loop and append

    I am a total newbie when it comes to VBA and excel though, not to programming in general. It seems that I am having quite a time trying to decipher VBA. What I want to do, is loop through an unknown number of rows in a column and then if it contains a specific acronym it will check the same row ina different column it will append one of two strings depending on the second cell's contents. This code doesn't seem to work but perhaps it will help elucidate what I'm getting at.
    Sub AppendMacro()
    '
    'Append Macro
    'Keyboard Shortcut: Ctrl+l
    '
    Dim c As Range
    
    For Each c In Range("S:S")
    
        If c.Value = "USAA" Or c.Value = "U.S.A.A" Then
    
        ActiveCell.Offset(0, 2).Select
    
         If ActiveCell.Value = "AM" Then
    
           ActiveCell.Value = ActiveCell.Value & "8-10"
    
           End If
    
        End If
    Next c
    
    End Sub
    One thing I know is tht the value of the cell isn't exactly going to be USAA or U.S.A.A but will contain those sets of characters. Also, I'm pretty sure I'm misunderstanding how ActiveCell works

    (Cross posted on stack overflow: http://stackoverflow.com/questions/6...oop-and-append)
    Last edited by DJP1339; 07-12-2011 at 05:35 PM. Reason: Solved

  2. #2
    Registered User
    Join Date
    07-12-2011
    Location
    Ny
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Loop and append

    Solved by GSerg over at Stack overflow:
    "
    ]Dim c As Range
    For Each c In Range("S:S").Cells
        If instr(c.Value, "USAA") > 0 Or instr(c.Value, "U.S.A.A") > 0 Then
            With c.Offset(0, 2)
                If .Value = "AM" then .Value = .Value & "8-10"
            End With
        End If
    Next c
    InStr finds first instance of a string inside another string.
    ActiveCell is the currently selected cell in the active window, not the loop counter. It is advisable to avoid using ActiveCell (and .Select) in code, unless you actually want the user to select a cell and then act upon it.
    With...End With is just a handy way to temporarily capture a reference to c.Offset(0, 2), to avoid explicit variable for it or calling it three times in a row. Without this block, it would be
    If c.Offset(0, 2).Value = "AM" Then
        c.Offset(0, 2).Value = c.Offset(0, 2).Value & "8-10"
    End If
    "
    Last edited by DJP1339; 07-12-2011 at 05:37 PM. Reason: forgot code tags

+ 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