+ Reply to Thread
Results 1 to 5 of 5

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

Hybrid View

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

    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
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    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

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

    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
    47

    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
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,637

    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.6.0 RC 1