Advice to change or speed up the way this code operates at present

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,890
Office Version
  1. 2007
Platform
  1. Windows
The code in question is supplied below.

On my worksheet i have customers names in column B

I start by opening my userform & i enter the customers name in TextBox2 Example TOM JONES.
The code looks down column B on my worksheet for all the TOM JONES.

Each time TOM JONES makes a purchase 3 digits are added to the end of his name,So lets say in this case the search has found TOM JONES 001 & TOM JONES 002

Back to userform now, When i leave TextBox2 on this occurance 003 will be added to TOM JONES in TextBox2, I complete all the other TextBoxes & the values are sent to my worksheet.

This all works fine & never an issue.
Now at present the last row with values on my worksheet is 2316

The issue i have is that one particular customer has made numerous purchases overtime & his 3 digits are now 105
The issue is when i go to leave TextBox2 the search of which i assume looks from row 2316 right back until row 7 is now taking some time before his next 3 digits are added to his name.
Im experiencing lag with this one customer.

Can we alter the code below to speed things up or do you think the code just needs to be altered for another to overcome this lag
First time customers or customers that only have 002 003 004 etc after their name doesnt seem to be an issue & leaving TextBox2 is pretty much no problem.

This customers buys from me pretty much every month so maybe restrict & only this customer to have the code search maybe back the last 100 rows each time.
So maybe some kind of IF code for JOES BLOGGS etc etc to check back 100 rows or Exit Sub if its another customer, Just a thought



Private Sub TextBox2_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Dim fndRng As Range
Dim findString As String
Dim i As Integer
Dim wsPostage As Worksheet

findString = Me.TextBox2.Value
If Len(findString) = 0 Then Exit Sub

Set wsPostage = ThisWorkbook.Worksheets("POSTAGE")
i = 1
Do
Set fndRng = Nothing
Set fndRng = wsPostage.Range("B:B").Find(What:=findString & Format(i, " 000"), _
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
If Not fndRng Is Nothing Then
i = i + 1
Cancel = True
End If
Loop Until fndRng Is Nothing

Me.TextBox2.Value = findString & Format(i, " 000")
Cancel = False

End Sub
 
Last edited:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Will this work for you ?
VBA Code:
Private Sub TextBox2_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    Dim fndRng As Range
    Dim findString As String
    Dim wsPostage As Worksheet
    Dim purchaseNum As Long

findString = Me.TextBox2.Value
If Len(findString) = 0 Then Exit Sub

Set wsPostage = ThisWorkbook.Worksheets("POSTAGE")

With wsPostage
    Set fndRng = .Range("B:B").Find(What:=findString, LookIn:=xlValues, LookAt:=xlPart, _
                     SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
    If Not fndRng Is Nothing Then
        purchaseNum = Right(fndRng, 3)
    Else
        purchaseNum = 0
    End If
End With

TextBox2.Value = findString & Format(purchaseNum + 1, " 000")

End Sub
 
Upvote 0
Solution
Just be aware that this code relies on the data always being sorted by the key of customer name and sales number. If you ever use a different sort order, you run the risk of generating a key that already exists. If that is the case, you could put a sort in your code that displays the form, but before the .Show.
 
Upvote 0
My original or the advised one ?
I never sort anything as the worksheet is from top to bottom & each customers number is increased by 1 so 001 002 003 004 & so on.
So im sure all is fine.
 
Upvote 0
I was referring to the code kindly supplied by @NoSparks. Since your data is always in the right order, there is no issue ... but things change, so be ready for that by inserting the sort command now while you're thinking about it. It won't make any difference to the functionality, but it will help to ensure that you always get the right answer.
 
Upvote 0
Good morning @ipbr21054,
my suggestion is based on previous posts and file you had linked to

I never sort anything as the worksheet is from top to bottom & each customers number is increased by 1 so 001 002 003 004 & so on.
So im sure all is fine.
and it is, but as the saying goes Never say Never, so I wouldn't dispute what @CephasOz is saying
 
Upvote 0
I will rephrase what I said.

From my userform I send the values to the next row after the last row with values.

This is what I mean by I don’t or have no need to sort anything.

If you think adding some code to just cover myself then please advise.
 
Upvote 0

Forum statistics

Threads
1,226,112
Messages
6,189,044
Members
453,521
Latest member
Chris_Hed

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top