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"?
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.
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.
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.
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!
FWIW I just compared the two methods on 10,000 plus rows. My method took half the time.
0.234375 vs 0.484375
@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.
Try: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?
![]()
Set c = .Find("*W*", Lookat:=xlWhole)
@jaslake
Very nice. 0.109375
@stnkynts
Thanks for doing that![]()
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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks