+ Reply to Thread
Results 1 to 3 of 3

Searching through a non-contiguous range

Hybrid View

  1. #1
    Registered User
    Join Date
    03-03-2016
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    14

    Searching through a non-contiguous range

    I have a UserForm for updating details on a sheet by searching for a reference in a range and updating the cell to the right of that using offset. I'm using this method to elimate the dreaded human error factor.

    I found the below code down the back of the internet and have adapted it to suit my workbook. It uses a string (in TextBox_Reference) and searches in column R and stamps the value from Textbox_Date to the corresponding cell (next column over).

    I need to somehow adapt this so if it doesn't find the value in column R it checks V and then Z and then AD. In reality there will be many columns to check.

    Could someone please help me amend this so it searches through a non-contiguous range?
    Each column in the range will start from row 2 and end at the same "LastRow" which is defined earlier in the code.

    Many thanks in advance.

    Kerry.


    Dim FindString As String
    Dim Rng As Range
    FindString = TextBox_IssueReference
    If Trim(FindString) <> "" Then
        With Sheets("Daily Data").Range("R2:R" & LastRow) 
            Set Rng = .Find(What:=FindString, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not Rng Is Nothing Then
                Rng.Offset(0, 1) = CDate(TextBox_Date)
            Else
                MsgBox "Reference not found."
            End If
        End With
    End If

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Searching through a non-contiguous range

    Maybe:

    Sub VBAClueless()
    Dim i As Long, rng As Range, FindString As String
    FindString = TextBox_IssueReference
    If Trim(FindString) <> "" Then
        With Sheets("Daily Data")
            For i = 18 To .UsedRange.Columns.Count Step 4
                Set rng = .Range(.Cells(2, i), .Cells(LastRow, i)).Find(What:=FindString, LookIn:=xlValues, LookAt:=xlWhole)
                    If Not rng Is Nothing Then
                        rng.Offset(0, 1) = CDate(TextBox_Date)
                        Exit For
                    End If
                    If i >= .UsedRange.Columns.Count Then MsgBox "Reference not found."
                Set rng = Nothing
            Next i
        End With
    End If
    End Sub

  3. #3
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Searching through a non-contiguous range

    Hi there,

    It's not easy to test this without access to your worksheet and UserForm, but try using the following code as a replacement for the code you've posted:

    
    
        Dim sStringToFind   As String
        Dim rFoundCell      As Range
        Dim vColumn         As Variant
    
        sStringToFind = TextBox_IssueReference
    
        If Trim(sStringToFind) <> vbNullString Then
    
            For Each vColumn In Array("R", "V", "Z", "AD")
    
                With Sheets("Daily Data").Range(vColumn & "2:" & vColumn & iLastRow)
    
                    Set rFoundCell = .Find(What:=sStringToFind, _
                                           After:=.Cells(.Cells.Count), _
                                           LookIn:=xlValues, _
                                           LookAt:=xlWhole, _
                                           SearchOrder:=xlByRows, _
                                           SearchDirection:=xlNext, _
                                           MatchCase:=False)
    
                    If Not rFoundCell Is Nothing Then
                          rFoundCell.Offset(0, 1) = CDate(TextBox_Date)
                    Else: MsgBox "Reference not found."
                    End If
    
                End With
    
            Next vColumn
    
        End If
    The highlighted values can be changed, deleted or added to as per your requirements.


    It's also likely that this statement can be simplified to:

    
                    Set rFoundCell = .Find(What:=sStringToFind, _
                                           LookIn:=xlValues, LookAt:=xlWhole)

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

+ 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. excel vba range property non contiguous range
    By Ramo13541 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-02-2016, 08:23 PM
  2. [SOLVED] Using scripting dictionary to add contiguous group of items from non contiguous range
    By stnkynts in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-29-2014, 06:12 PM
  3. [SOLVED] How to sum non-contiguous columns applied as a formula on contiguous cells
    By figo12 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 10-09-2013, 01:07 PM
  4. [SOLVED] How to delete multiple contiguous (and/or) non-contiguous rows, in an excel table
    By jimmalk in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-03-2012, 05:48 AM
  5. Searching non consecutive or contiguous words
    By wvollbon in forum Excel General
    Replies: 0
    Last Post: 01-31-2012, 07:35 PM
  6. Contiguous "Range" from Non Contiguous Ranges
    By DonkeyOte in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 01-03-2011, 03:23 PM
  7. Copy and Paste an array (contiguous & non contiguous ranges)
    By Xrull in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-09-2010, 09:17 AM

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