+ Reply to Thread
Results 1 to 21 of 21

Finding gaps within rows of numbers

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-31-2008
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    149

    Finding gaps within rows of numbers

    Hello,

    Please see attached.

    Via macro, I am trying to establish where there is a gap in numbers, signified by a "-". The 2 cases of this are E3 and E5. Note that B7 isn't gap because it is the first numer in the sequence. Nor is L6, as it is the last.

    I'd be grateful for some code to generate the TRUE and FALSE values in column A.

    Thanks in advance.
    Attached Files Attached Files
    Last edited by bd528; 01-24-2011 at 12:34 PM.

  2. #2
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Finding gaps within rows of numbers

    Try in A2

    =IF(COUNTIF(C2:K2,"-")<>0,TRUE,FALSE)

    Drag/Fill Down

    Hope this helps
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Finding gaps within rows of numbers

    In A2:
    PHP Code: 
    =countif(C2:K2,"-")>
    Last edited by snb; 01-23-2011 at 01:21 PM.



  4. #4
    Forum Contributor
    Join Date
    12-31-2008
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: Finding gaps within rows of numbers

    Thanks for the replies. For both cases, I cant figure out how to use the code within a macro. Can someone explain how it should be formatted/coded?

    Thanks again.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Finding gaps within rows of numbers

    Hello bd528,

    The attached workbook has the formulas entered into column "A".
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  6. #6
    Forum Contributor
    Join Date
    12-31-2008
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: Finding gaps within rows of numbers

    Thanks for your reply.

    What I require is a macro that adds the code to insert that code into the sheet in cells A10:A15. Its doing this that I'm having trouble with.

    Thanks.

  7. #7
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Finding gaps within rows of numbers

    Try this
    Sub test()
        Dim MasterLastRow As Long
        MasterLastRow = Range("B" & Rows.Count).End(xlUp).Row
        
        Range("A1").Resize(MasterLastRow, 1).Formula = "=COUNTIF(C1:K1," & Chr(34) & "-" & Chr(34) & ")>0"
    End Sub

    Assuming your data begins in B1 and Column A is free to use, "MasterLastRow" can be from your call.

    Hope this helps

  8. #8
    Forum Contributor
    Join Date
    12-31-2008
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: Finding gaps within rows of numbers

    Quote Originally Posted by Marcol View Post
    Try this
    Sub test()
        Dim MasterLastRow As Long
        MasterLastRow = Range("B" & Rows.Count).End(xlUp).Row
        
        Range("A1").Resize(MasterLastRow, 1).Formula = "=COUNTIF(C1:K1," & Chr(34) & "-" & Chr(34) & ")>0"
    End Sub

    Assuming your data begins in B1 and Column A is free to use, "MasterLastRow" can be from your call.

    Hope this helps
    That piece of code worked perfectly thanks.

    Unfortunately, after applying it to my actual spreadsheet, it turns out I didn't explain my problem thoroughly.

    Please see attached.

    As per the attached, there can be many "-" per row, and not a "gap", as in row 8 - so this is FALSE. Row 3 has many "-" and also a gap at AD, so it's value in column J is TRUE.
    Attached Files Attached Files

  9. #9
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Finding gaps within rows of numbers

    Try this with your new sample sheet
    Option Explicit
    
    Sub test()
        Dim MasterLastRow As Long, StartRow As Long
        Dim RowNo As Long, CellNo As Long
        Dim rng As Range
        
        MasterLastRow = Range("K" & Rows.Count).End(xlUp).Row
        StartRow = 12
        For RowNo = StartRow To MasterLastRow
            Set rng = Range("K" & RowNo & ":AH" & RowNo)
            If WorksheetFunction.CountIf(rng, "-") = 24 Then
                Range("J" & RowNo) = False
            Else
                For CellNo = 1 To 21
                    If IsNumeric(rng(CellNo)) And rng(CellNo + 1) = "-" And IsNumeric(rng(CellNo + 2)) Then
                        Range("J" & RowNo) = True
                        Exit For
                    Else
                        Range("J" & RowNo) = False
                    End If
                Next
            End If
        Next
    End Sub
    Attached Files Attached Files

  10. #10
    Forum Contributor
    Join Date
    12-31-2008
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: Finding gaps within rows of numbers

    Quote Originally Posted by Marcol View Post
    Try this with your new sample sheet
    Option Explicit
    
    Sub test()
        Dim MasterLastRow As Long, StartRow As Long
        Dim RowNo As Long, CellNo As Long
        Dim rng As Range
        
        MasterLastRow = Range("K" & Rows.Count).End(xlUp).Row
        StartRow = 12
        For RowNo = StartRow To MasterLastRow
            Set rng = Range("K" & RowNo & ":AH" & RowNo)
            If WorksheetFunction.CountIf(rng, "-") = 24 Then
                Range("J" & RowNo) = False
            Else
                For CellNo = 1 To 21
                    If IsNumeric(rng(CellNo)) And rng(CellNo + 1) = "-" And IsNumeric(rng(CellNo + 2)) Then
                        Range("J" & RowNo) = True
                        Exit For
                    Else
                        Range("J" & RowNo) = False
                    End If
                Next
            End If
        Next
    End Sub
    This works pretty well. However I added a value to AC18, re-ran the macro, and it still came through as FALSE. (I copied the value from AF18 to AC18). Any thoughts?

  11. #11
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Finding gaps within rows of numbers

    Please don't quote whole posts in your replies, the Moderators don't like it..
    Only quote a part of the post if it is really needed for clarity

    Try this
    Option Explicit
    
    Sub test()
        Dim MasterLastRow As Long, StartRow As Long
        Dim NumberOfColumns As Long
        Dim RowNo As Long, CellNo As Long
        Dim rng As Range
        
        MasterLastRow = Range("K" & Rows.Count).End(xlUp).Row
        StartRow = 12
        NumberOfColumns = Range("AH:AH").Column - Range("K:K").Column + 1
        
        For RowNo = StartRow To MasterLastRow
            Set rng = Range("K" & RowNo & ":AH" & RowNo)
            If WorksheetFunction.CountIf(rng, "-") = NumberOfColumns Then
                Range("J" & RowNo) = False
            Else
                For CellNo = 1 To NumberOfColumns - 1
                    If IsNumeric(rng(CellNo)) And rng(CellNo + 1) = "-" Then
                        Range("J" & RowNo) = True
                        Exit For
                    Else
                        Range("J" & RowNo) = False
                    End If
                Next
            End If
        Next
    End Sub
    Attached Files Attached Files
    Last edited by Marcol; 01-24-2011 at 09:45 AM. Reason: Changed code to better explain and make more versatile

  12. #12
    Forum Contributor
    Join Date
    12-31-2008
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: Finding gaps within rows of numbers

    Marcol, I think we're nearly there. I added a little extra data (attached). For some reason, the results on rows 25 & 25 are incorrect??
    Attached Files Attached Files

  13. #13
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Finding gaps within rows of numbers

    Okay, try this
    Option Explicit
    
    Sub test()
        Dim MasterLastRow As Long, StartRow As Long
        Dim NumberOfColumns As Long
        Dim RowNo As Long, CellNo As Long
        Dim ctr As Long
        Dim rng As Range
        Dim IsNumber As Boolean
        
        MasterLastRow = Range("K" & Rows.Count).End(xlUp).Row
        StartRow = 12
        NumberOfColumns = Range("AH:AH").Column - Range("K:K").Column + 1
        
        For RowNo = StartRow To MasterLastRow
            Set rng = Range("K" & RowNo & ":AH" & RowNo)
            If WorksheetFunction.CountIf(rng, "-") = NumberOfColumns Then
                Range("J" & RowNo) = False
            Else
                For CellNo = 1 To NumberOfColumns - 1
                    If IsNumeric(rng(CellNo)) And rng(CellNo + 1) = "-" Then
                        ctr = ctr + 1
                    End If
                    If ctr > 0 Then
                        If IsNumeric(rng(CellNo + 1)) Then
                        IsNumber = True
                        Exit For
                        End If
                    End If
                Next
                    If ctr > 0 And IsNumber Then
                        Range("J" & RowNo) = True
                    Else
                        Range("J" & RowNo) = False
                    End If
            End If
            ctr = 0
            IsNumber = False
        Next
    End Sub
    Attached Files Attached Files

  14. #14
    Forum Contributor
    Join Date
    12-31-2008
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    149

    Re: Finding gaps within rows of numbers

    Marcol, this now works perfectly. Thanks for the time spent on this.

    I'll make sure I read through your code and learn what's going on.

    Thanks again.

  15. #15
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Finding gaps within rows of numbers

    Happy to have helped.

+ 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