Modify Code found on the Web

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Below is the original code that I found which will find the word "Excel" in worksheet named "Source" from Range("A1:A10000") and the return the range address for the word.

Code:
Sub x()    Dim rngX As Range
    
    Set rngX = Worksheets("Source").Range("A1:A10000").Find(" Excel", lookat:=xlPart)
    If Not rngX Is Nothing Then
        MsgBox "Found at " & rngX.Address
    End If


End Sub

I would like to modify the above code to find the product code (sPrdCde) from the Worksheet(formTitle) but instead of using Range("A1:A10000"), I would like to use the variable finalRow.

Code:
Sub Test()

Dim ws_count As Integer, i As Integer, finalRow As Integer, x As Integer


lDz = 0
lCs = 0
sUOM = " "


ActiveWorkbook.Worksheets(formTitle).Activate
finalRow = Cells(Rows.Count, 2).End(xlUp).Row


Call findItem


End Sub



Code:
Sub findItem()    
    Dim rngItem As Range
    
    Set rngItem = Worksheets(formTitle).finalRow.Find(sPrdCde, lookat:=xlPart)
    If Not rngItem Is Nothing Then
        MsgBox "Found at " & rngItem.Address
    End If


End Sub

Thank You
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Something like
Code:
Sub Test()

Dim ws_count As Integer, i As Integer, finalRow As Integer, x As Integer
Dim Rng As Range

lDz = 0
lCs = 0
sUOM = " "


With ActiveWorkbook.Worksheets(formTitle)
   Set Rng = .Range("B2", .Range("B" & Rows.Count).End(xlUp))
End With
MsgBox findItem(Rng, "abc")


End Sub
Function findItem(Rng As Range, sPrdCde As String) As String
    Dim rngItem As Range
    
    Set rngItem = Rng.find(sPrdCde, lookat:=xlPart)
    If Not rngItem Is Nothing Then
       findItem = rngItem.Address
    End If
End Function
 
Upvote 0
Thank you. That worked great. I did, however, had to modify your code just a little bit.
 
Upvote 0
Glad you sorted it & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

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