+ Reply to Thread
Results 1 to 5 of 5

Thread: How to get a macro to run on all selected cells in a sheet?

  1. #1
    Registered User
    Join Date
    12-13-2011
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    29

    How to get a macro to run on all selected cells in a sheet?

    Hi,

    I am just starting to learn macros and I wrote one that appends a "-p" to a cell. How can I get this to work on all selected cells?
    can someone tell me how I can run the same macro on all selected cells in a column??

    
    Sub MarkAsStandAlone()
    '
    ' MarkAsStandAlone Macro
    '
    ' Keyboard Shortcut: Ctrl+Q
    '
       Dim cellvalue As String
       Dim R As String
       Dim C As String
    
    
        R = ActiveCell.Row
        C = ActiveCell.Column
        cellvalue = ActiveCell.Value
        ActiveCell = cellvalue & "-i"
    
    End Sub
    Cheers,

    Thanks

  2. #2
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,031

    Re: How to get a macro to run on all selected cells in a sheet?

    How about:

    Sub AppendP()
    Dim c As Object
    For Each c In Selection
        c.Value = c.Value & "-p"
    Next c
    
    End Sub
    Click the * below to say thanks

    Girls sleep with guys who use photoshop, but marry the ones who work with Excel

    Corduroy
    pillows: They're making headlines!

    Did you mean: recursion
    http://www.google.com/search?hl=en&q=recursion

  3. #3
    Registered User
    Join Date
    12-13-2011
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    29

    Re: How to get a macro to run on all selected cells in a sheet?

    Works like a charm

  4. #4
    Registered User
    Join Date
    12-13-2011
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    29

    Re: How to get a macro to run on all selected cells in a sheet?

    Hi Kyle,

    I just noticed. I i make a selection in a filtered column then it applies the -p to all the values that are not in the filter as well.. how can we go about doing this?

  5. #5
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    MSO2007 on WinXP/MSO2000 on Win7/winXP
    Posts
    2,180

    Re: How to get a macro to run on all selected cells in a sheet?

    Option Explicit
    
    Sub AppendP()
    Dim c As Range
    For Each c In Selection.SpecialCells(xlVisible)
       
        c.Value = c.Value & "-p"
    Next c
    
    End Sub
    ---
    Ben Van Johnson

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0