+ Reply to Thread
Results 1 to 11 of 11

Fastest method to search BT column for value starting with "27W" and then adjust adjacent

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    298

    Fastest method to search BT column for value starting with "27W" and then adjust adjacent

    What is the fastest method to search the "BT" column (entire used column) for any value starting with "27W", and then change the value of the BV column in the same row to "my string"?
    Last edited by ks100; 09-02-2015 at 02:24 PM.

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Fastest method to search BT column for value starting with "27W" and then adjust adjac

    This should be fairly quick

    Sub ks100()
    Dim ws As Worksheet:    Set ws = Sheets("Sheet1")
    Dim arr As Variant
    Dim i As Long
    
    Application.ScreenUpdating = False
    arr = ws.Range("BT1:BT" & ws.Range("BT" & Rows.Count).End(xlUp).Row)
    
    For i = LBound(arr, 1) To UBound(arr, 1)
        If Left(CStr(arr(i, 1)), 3) = "27W" Then
            ws.Range("BV" & i) = "my string"
        End If
    Next i
    Application.ScreenUpdating = True
    
    End Sub
    If you are happy with my response please click the * in the lower left of my post.

  3. #3
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    298

    Re: Fastest method to search BT column for value starting with "27W" and then adjust adjac

    Ahh, I should have pointed out that "27W" was just an example so the script would need to adapt to an ever increasing number size. Basically the number portion is a ticket number and the W is a subcopy of that ticket.

    the actual search value I would be using is TICKET & "W" where ticket is a string containing my ticket ID.

    The value of TICKET could be any number of digits, is there any way to adapt it for that? My apologies for not being clear in the first post.

  4. #4
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Fastest method to search BT column for value starting with "27W" and then adjust adjac

    I can objectively say this is the absolute fastest method, but it will be close to the fastest. And it will be noticeably MUCH faster then looping through every cell if you have a large data set ( > 10,000 rows).

    Sub ks100()
    ms = "my string"
    Application.ScreenUpdating = False
    With Range("BT:BT")
        Set c = .Find("27W*", Lookat:=xlWhole)
        If Not c Is Nothing Then
        FirstAdd = c.Address
        Do
        Range("BV" & c.Row).Value = ms
        Set c = .FindNext(c)
        Loop While Not c Is Nothing And c.Address <> FirstAdd
        End If
    End With
    Application.ScreenUpdating = True
    End Sub
    I'm interested in starting a career working with VBA, if anyone knows of any opportunities!

  5. #5
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    298

    Re: Fastest method to search BT column for value starting with "27W" and then adjust adjac

    Quote Originally Posted by walruseggman View Post
    I can objectively say this is the absolute fastest method, but it will be close to the fastest. And it will be noticeably MUCH faster then looping through every cell if you have a large data set ( > 10,000 rows).

    Sub ks100()
    ms = "my string"
    Application.ScreenUpdating = False
    With Range("BT:BT")
        Set c = .Find("27W*", Lookat:=xlWhole)
        If Not c Is Nothing Then
        FirstAdd = c.Address
        Do
        Range("BV" & c.Row).Value = ms
        Set c = .FindNext(c)
        Loop While Not c Is Nothing And c.Address <> FirstAdd
        End If
    End With
    Application.ScreenUpdating = True
    End Sub
    This worked amazing thank you! I apologize to the first poster in this thread for not being more clear in my request.

  6. #6
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Fastest method to search BT column for value starting with "27W" and then adjust adjac

    FWIW I just compared the two methods on 10,000 plus rows. My method took half the time.

    0.234375 vs 0.484375

  7. #7
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Fastest method to search BT column for value starting with "27W" and then adjust adjac

    @stnkynts

    Sounds like you've a Data File with Timer...test this one if you will please.

    Option Explicit
    
    Sub Macro1()
       Dim ws           As Worksheet
       Dim LR           As Long
    
       Set ws = Sheets("Sheet1")
       With ws
          LR = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
                           SearchDirection:=xlPrevious).Row
          If Not .AutoFilterMode Then
             .Rows("1:1").AutoFilter   '<---Header Row
          End If
          .Range("BT1:BT" & LR).AutoFilter Field:=1, Criteria1:="=27W*", _
                                           Operator:=xlAnd
          .Range(.Cells(2, "BV"), .Cells(LR, "BV")).SpecialCells(xlCellTypeVisible).Value = "my string"
          .AutoFilterMode = False
       End With
    End Sub
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  8. #8
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Fastest method to search BT column for value starting with "27W" and then adjust adjac

    the actual search value I would be using is TICKET & "W" where ticket is a string containing my ticket ID.

    The value of TICKET could be any number of digits, is there any way to adapt it for that?
    Try:

    Set c = .Find("*W*", Lookat:=xlWhole)

  9. #9
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Fastest method to search BT column for value starting with "27W" and then adjust adjac

    @jaslake

    Very nice. 0.109375

  10. #10
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Fastest method to search BT column for value starting with "27W" and then adjust adjac

    @stnkynts

    Thanks for doing that

  11. #11
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Fastest method to search BT column for value starting with "27W" and then adjust adjac

    Quote Originally Posted by ks100 View Post
    What is the fastest method to search the "BT" column (entire used column) for any value starting with "27W", and then change the value of the BV column in the same row to "my string"?
    If you really want a fastest method you might consider the following code.
    Hopefully some of the speed testers will test it and give results.
    It takes account of what I understand are your modified needs in your post#4
    Sub klk()
    
    Dim a, b(), i&, u&
    
    a = Intersect(ActiveSheet.UsedRange, Range("BT:BT"))
    ReDim b(1 To UBound(a), 1 To 1)
    
    For i = 1 To UBound(a)
        u = InStr(a(i, 1), "W")
        If u > 1 Then _
            If IsNumeric(Left(a(i, 1), u - 1)) Then _
                b(i, 1) = "mystring"
    Next i
    
    Range("BV1").Resize(UBound(a)) = b
    
    End Sub

+ 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. Find a word "screenshot" and print value "yes" in adjacent Column
    By prat123 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-21-2015, 03:04 PM
  2. Replies: 11
    Last Post: 12-10-2014, 03:00 PM
  3. Update Macro to search column Bfor "Fail" and "Exception"
    By programct in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-10-2014, 02:32 PM
  4. Data Analysis ("Fastest column"?)
    By DataGeekXLS in forum Excel General
    Replies: 8
    Last Post: 01-25-2013, 08:43 PM
  5. Replies: 2
    Last Post: 09-25-2012, 12:53 AM
  6. Replies: 2
    Last Post: 06-29-2012, 06:32 AM
  7. Replies: 2
    Last Post: 05-05-2008, 04:51 PM

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