+ Reply to Thread
Results 1 to 16 of 16

How do you count the number of rows when you have multiple non-continues selections

Hybrid View

klonbeck How do you count the number... 01-23-2014, 03:08 PM
xladept Re: How do you count the... 01-23-2014, 04:16 PM
klonbeck Re: How do you count the... 01-23-2014, 04:50 PM
xladept Re: How do you count the... 01-23-2014, 05:02 PM
klonbeck Re: How do you count the... 01-23-2014, 05:14 PM
xladept Re: How do you count the... 01-23-2014, 05:25 PM
klonbeck Re: How do you count the... 01-23-2014, 05:28 PM
xladept Re: How do you count the... 01-23-2014, 05:40 PM
klonbeck Re: How do you count the... 01-23-2014, 06:00 PM
xladept Re: How do you count the... 01-23-2014, 06:29 PM
klonbeck Re: How do you count the... 01-23-2014, 07:03 PM
klonbeck Re: How do you count the... 01-23-2014, 06:47 PM
xladept Re: How do you count the... 01-23-2014, 06:50 PM
Izandol Re: How do you count the... 01-23-2014, 07:12 PM
klonbeck Re: How do you count the... 01-23-2014, 07:28 PM
xladept Re: How do you count the... 01-23-2014, 09:53 PM
  1. #1
    Registered User
    Join Date
    11-14-2012
    Location
    Lebanon, Oregon
    MS-Off Ver
    Excel 2010
    Posts
    47

    How do you count the number of rows when you have multiple non-continues selections

    I have put together this bit of code to help speed up our electrical interconnect reports and it works great except that it only works on a single selection of several continues rows (click row 2, Shift+ Click row 5). If I attempt to use Ctrl+Click and select several different rows the code falls apart.

    Ideally I would like to be able to make any number of Shift+Click and Ctrl+Click Selections and then run my code.

    I think I only need to know the number of rows selected.

    the purpose of this code is to flip the rows selected by the user so that what ever data was on the right hand would be moved to the left hand. The attached picture shows that some of the data that I am working with.
    Capture.PNG

    All of the data is flipped about column I. The data to be flipped is located between columns C & O, this range has also been formatted as text.

    Can anyone help? I have been trying but can't find the answer.

    Thanks in advance.

    Sub FlipRows()
    '
    ' FlipRows Macro
    ' Flip selected signal(s)
    '
    'User selects selects the desired rows an then they go to dialog-special-constants selects
    'the range of cells between, and including, columns "C" and "O" that are with in the user selected rows.
    
    Dim C As Long
    Dim I As Long, J As Long
    Dim r As Long
    Dim NewData As Variant
    Dim OldData As Variant
    Dim rng As Range
    Dim test
    Dim test2
    
    '''''Check if there is data
        If Range("A1").Value <> "CONDUIT NUM" Then
            Exit Sub
        End If
    '''''Flip selected rows about the WIRENO column !!!!At this time only one continues selection can be flipped more then one selection will cause error!!!!
        Selection.SpecialCells(xlCellTypeConstants, 2).Select
        Set rng = Selection
        OldData = rng.Value 'the values of the selected range is stored in OldData
        ReDim NewData(1 To rng.Rows.Count, 1 To rng.Columns.Count) 'Arrey for number of rows(value changes) and number of columns (value should always be 13) the user selected
        For I = 1 To UBound(OldData, 1) 'number of rows to process
            r = r + 1
                For J = UBound(OldData, 2) To 1 Step -1 'number of columns to proces
                    C = C + 1
                    NewData(r, C) = OldData(I, J) 'current row being processed from OldData. flips cell values about column "I" staring with the right side moving moving to the left. The values new location is stored in NewData.
                Next J
            C = 0
        Next I
        rng.Value = NewData 'the new value locations are applied to the current selection
    End Sub

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How do you count the number of rows when you have multiple non-continues selections

    Hi Klon.

    This works:

    Next I
        rng = NewData 'the new value locations are applied to the current selection
    End Sub
    *Get rid of that .Value in the penultimate line
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    11-14-2012
    Location
    Lebanon, Oregon
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: How do you count the number of rows when you have multiple non-continues selections

    xladept,

    I removed the .value, but it seems to be performing the same as it did before.
    What was the purpose of removing the .value?

    If I Ctrl+Click row 2 and Ctrl+Click row 4 it still does not flip the data.

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How do you count the number of rows when you have multiple non-continues selections

    Well, it worked for me?? - The reason to remove the value qualifier is that you're replacing the whole range and the whole range has no distinct value i.e. it's inappropriate

  5. #5
    Registered User
    Join Date
    11-14-2012
    Location
    Lebanon, Oregon
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: How do you count the number of rows when you have multiple non-continues selections

    That makes sense.
    Sorry for being inappropriate.

    What kind of selection did you do?
    If I Ctrl+Click row 2 & Ctrl+Click row 3 then run the code, it works fine.

    If I Ctrl+Click row 2 & Ctrl+Click row 4 then run the code, it does not work.

    If I Click row 2 & Shift+Click row 6 & Ctrl+Click row 7 then run the code, it works fine.

    If I Click row 2 & Shift+Click row 6 & Ctrl+Click row 8 then run the code, it does not work.

    The problem seems to be happening here.
    OldData = rng.Value 'the values of the selected range is stored in OldData
    If I step through the code and look at the details of this array, it is only showing 5 rows of data even though I really selected 6 rows. (Click row 2 & Shift+Click row 6 & Ctrl+Click row 8)

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How do you count the number of rows when you have multiple non-continues selections

    Hi Klon,

    Well - you haven't posted a sample book so I used my utility forum book, what I did was highlight six rows from column c to column o - then I played your macro and it ran real slick!

    If you'll post your sample book, I'll be better able to advise you

  7. #7
    Registered User
    Join Date
    11-14-2012
    Location
    Lebanon, Oregon
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: How do you count the number of rows when you have multiple non-continues selections

    I suppose that this might be helpful.
    WIREFRM2.xls

  8. #8
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How do you count the number of rows when you have multiple non-continues selections

    Hi Klon,

    It works when I highlight a range from c to o - any amount of rows??

  9. #9
    Registered User
    Join Date
    11-14-2012
    Location
    Lebanon, Oregon
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: How do you count the number of rows when you have multiple non-continues selections

    The Code should work for any number of within a single selection set; ie. Click row 2 Shift+Click row X, or Click & Hold row 2 drag down to row X and release.
    The Click & drag selection looks the same as a shift selection.
    Click and Drag Selection.PNG

    It also seems to work if the user uses Ctrl+Click row 2 & Ctrl+Click row 3 & Ctrl+Click row 4... this also seems to work fine.
    Ctrl+Click selection.PNG

    If the user skips a row during the Ctrl+Click selection, as shown below. the data in the last row (7) will be trashed.
    Ctrl+Click selection and skipped row.PNG
    This will also happen if rows 2-5 are selected with a Click Drag or Click & Shift+Click selections.

    Hopefully I am making sense.

  10. #10
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How do you count the number of rows when you have multiple non-continues selections

    It's perfectly understandable what you're saying - however, they all work for me

  11. #11
    Registered User
    Join Date
    11-14-2012
    Location
    Lebanon, Oregon
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: How do you count the number of rows when you have multiple non-continues selections

    I just found something that I think I can use to solve my problem, but I don't know how to use it.
    If you set the selection as an object and then look at the details of that object in the Locals window, you will find that the object has Areas. Under the Areas you find item1, item2... each item represent a selection area. for example row 2 would be item1 and row 4 would be item2.
    Under the item (1 or 2) there is value2. under value2 there is value2(#), where # represents the number of rows in that selection group. if you expand value2(1) you will find the individual cell values for the first row of that selection.

    So I think if I set the selection as an object and then starting with the first selection set (item1), move those values into the array and then do the same thing on the next selection set in the object.
    problem is I have no idea how to extract data that is imbedded that deep.
    I think it would be something like; object.Areas.Item1.Value2.Value2(1),Value2(1,1).value would return the value of the first cell in the first row of the first selection set.

  12. #12
    Registered User
    Join Date
    11-14-2012
    Location
    Lebanon, Oregon
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: How do you count the number of rows when you have multiple non-continues selections

    So none of the data is trashed when you select row 2 and row 4?
    That would be a first for me. I have never had a situation where I couldn't run my own code but someone else could.
    I don't think this would cause any problems but I am running this code from a ribbon command. although when I originally created it I was running it right out of VBA, and I have had the same problem this whole time.

  13. #13
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How do you count the number of rows when you have multiple non-continues selections

    Hi Klon,

    No - I was wrong - the non-contiguous rows, whether above or below the first are all trashed -

    As the doctor said "Don't do that"

  14. #14
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: How do you count the number of rows when you have multiple non-continues selections

    This is not tested:
        For each rng in Selection.Areas
        OldData = rng.Value 'the values of the selected range is stored in OldData
        ReDim NewData(1 To rng.Rows.Count, 1 To rng.Columns.Count) 'Arrey for number of rows(value changes) and number of columns (value should always be 13) the user selected
        For I = 1 To UBound(OldData, 1) 'number of rows to process
            r = r + 1
                For J = UBound(OldData, 2) To 1 Step -1 'number of columns to proces
                    C = C + 1
                    NewData(r, C) = OldData(I, J) 'current row being processed from OldData. flips cell values about column "I" staring with the right side moving moving to the left. The values new location is stored in NewData.
                Next J
            C = 0
        Next I
        rng.Value = NewData 'the new value locations are applied to the current selection
    next rng
    Note: always use .Value (or .Value2)
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  15. #15
    Registered User
    Join Date
    11-14-2012
    Location
    Lebanon, Oregon
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: How do you count the number of rows when you have multiple non-continues selections

    IT WORKS!!!!!!!!

    Here is the finished code.
    Sub ReverseRows()
    '
    ' ReverseRows Macro
    ' Flip selected signal
    '
    'User selects selects the desired rows an then they go to dialog-special-constants selects
    'the range of cells between, and including, columns "C" and "O" that are with in the user selected rows.
    
    Dim C As Long
    Dim I As Long, J As Long
    Dim r As Long
    Dim NewData As Variant
    Dim OldData As Variant
    Dim rng As Range
    
    '''''Check if there is data
        If Range("A1").Value <> "CONDUIT NUM" Then
            Exit Sub
        End If
    '''''Flip selected rows about the WIRENO column !!!!At this time only one continues selection can be flipped more then one selection will cause error!!!!
        Selection.SpecialCells(xlCellTypeConstants, 2).Select
        Set rng = Selection
        For Each rng In Selection.Areas
            OldData = rng.Value 'the values of the selected range is stored in OldData
            ReDim NewData(1 To rng.Rows.Count, 1 To rng.Columns.Count) 'Arrey for number of rows(value changes) and number of columns (value should always be 13) the user selected
            For I = 1 To UBound(OldData, 1) 'number of rows to process
                r = r + 1
                    For J = UBound(OldData, 2) To 1 Step -1 'number of columns to proces
                        C = C + 1
                        NewData(r, C) = OldData(I, J) 'current row being processed from OldData. flips cell values about column "I" staring with the right side moving moving to the left. The values new location is stored in NewData.
                    Next J
                C = 0
            Next I
            r = Empty
            rng = NewData 'the new value locations are applied to the current selection
        Next rng
    End Sub
    Thank you Izandol & xladept! I really appreciate the help you provided!

  16. #16
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How do you count the number of rows when you have multiple non-continues selections

    You're welcome! And, thanks for the rep! Izandol - thank you

    BTW -
    Set rng = Selection
    is unnecessary.
    Last edited by xladept; 01-23-2014 at 11:29 PM.

+ 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. [SOLVED] Using a function to count the number of rows in which multiple values are true
    By jimbowl in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 04-17-2013, 06:19 PM
  2. search multiple columns and count number of rows the keywords appear
    By vincegc8 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-22-2013, 02:01 PM
  3. Count number of rows with multiple criteria
    By spinx in forum Excel General
    Replies: 5
    Last Post: 09-17-2010, 09:17 AM
  4. [SOLVED] Count number of cells based on 2 selections in the same range.
    By jilaba in forum Excel - New Users/Basics
    Replies: 12
    Last Post: 02-16-2010, 11:22 AM
  5. Count number of rows in multiple arrays with identical values
    By Steven Fleck in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-29-2008, 05:59 AM

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