Results 1 to 2 of 2

Thread: VBA For each row in visible selection

  1. #1
    Junior Member
    Join Date
    Jun 2017
    Posts
    1
    Rep Power
    0

    VBA For each row in visible selection

    I have wrote function that will change few cells for each row in selection. Lets assume function will set text in few cells for each row.
    If there is no filter and i have just selected 1 cell function runs OK
    if there is no filter and i have selected more than 1 cell/row its OK
    If there is filter and i have selected more than 1 cell/row its OK

    **if there is Filter and i have just selected 1 cell funtions goes wrong**

    In my file i have header, i set filter on some field for example column 1 to look for all "Sales" and only for those i would like to change "Sales" to "Buys"
    However my function change even header then goes for all selected rows and at the end it runs till the end of worksheet.

    Here is my code
    Code:
        Sub ChangeText(control as IRibbonControl)
        dim sRange as Range
        dim sSelect as Variant
        set sSelect = Selection.SpecialCells(xlCellTypeVisible)
        
        for each sRange in sSelect.Rows
        Select Case Cells(sRange.Row, 3)
        Case "Sales"
        Cells(sRange.Row,3) = 'Buys"
        case else
        Cells(sRange.Row,3) = "Internal"
        End Select
        Next
        End Sub
    This is just sample, in main code i have multiple cases
    what i figured out is that Selection.SpecialCells(xlCellTypeVisible).Areas.Co unt returns 2 when i filter data and just select 1 cell. Could you please advise me on this?
    Last edited by Drac0; 03-02-2018 at 02:48 PM.

  2. #2
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,122
    Rep Power
    10
    Hi

    Welcome to board!

    not sure, but does this make any good ?

    Code:
    Dim r   As Range
        
        Set sSelect = Selection.SpecialCells(xlCellTypeConstants)
        
        For Each sRange In sSelect.Areas
            For Each r In sRange.Rows
                Select Case Cells(r.Row, 3)
                Case "Sales"
                    Cells(r.Row, 3) = "Buys"
                Case Else
                    Cells(r.Row, 3) = "Internal"
                End Select
            Next
        Next
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

Similar Threads

  1. Conditional Selection of cells and its row
    By Prabhu in forum Excel Help
    Replies: 1
    Last Post: 07-20-2015, 05:04 PM
  2. Create Or Update A Hyperlink Index Of Visible Sheets
    By MrBlackd in forum Excel and VBA Tips and Tricks
    Replies: 1
    Last Post: 06-24-2014, 09:03 AM
  3. vba, worksheet visible issue
    By xander1981 in forum Excel Help
    Replies: 1
    Last Post: 03-13-2014, 04:06 PM
  4. Replies: 3
    Last Post: 04-08-2012, 08:05 AM
  5. Mail Range or Selection using VBA
    By LalitPandey87 in forum Excel Help
    Replies: 1
    Last Post: 11-03-2011, 09:00 AM

Posting Permissions

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