. find (VBA) not working

dannyok90

Board Regular
Joined
Aug 30, 2016
Messages
115
Hi All,

The code bellow is looking for values (Materials*) but its not finding en exact match, its finding any cell with Materials or any part of the word in it and copy and pasting from there :/

Any ideas? :)


Thanks,
Dan


Code:
Sub MaterialsInsertNewRow()
    ' Materials Controls
    Dim b As Object, cs As Integer, LR As Integer
    Dim rng As Range
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual
    
    LR = Sheet1.Range("E:E").Find("Materials*", , xlValues, xlWhole, , xlNext).Row - 2
    Set b = ActiveSheet.Buttons(Application.Caller)
With b.TopLeftCell
Sheet1.Unprotect Password:="rse1"
    cs = .Row
    Sheet1.Range("E:E").Find("Materials*", , xlValues, xlWhole, , xlNext).Offset(-1, 0).EntireRow.Copy
    Rows(cs).Offset(.Rows.Count + 0).Insert
    Rows(cs).Offset(.Rows.Count + 0).Hidden = False
Sheet1.Protect Password:="rse1"
  End With
  
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
  
End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
If you only want to find the row in column E that has the value 'Materials' perhaps you could use Application.Match instead of Find.
Code:
Sub MaterialsInsertNewRow()
    ' Materials Controls
Dim b As Object, cs As Integer, LR As Integer
Dim rng As Range
Dim Res As Variant

    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual
    
    Res = Application.Match("Materials", Sheet1.Columns(5), 0)
    
    If Not IsError(Res) Then
        LR = Res - 2
    Else
        MsgBox "Materials row not found!", vbExclamation
        Exit Sub
    End If
    
    Set b = ActiveSheet.Buttons(Application.Caller)
    
    With b.TopLeftCell
        Sheet1.Unprotect Password:="rse1"
        cs = .Row
        Sheet1.Rows(LR - 1).Copy
        Rows(cs).Offset(.Rows.Count + 0).Insert
        Rows(cs).Offset(.Rows.Count + 0).Hidden = False
        Sheet1.Protect Password:="rse1"
    End With
  
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
  
End Sub
 
Upvote 0
Hi Norie,
Thanks!
The code works however its still not finding the exact value, so:
I have a total row thats called Named materials in Column E (5) and above it in a hidden row is
the row thats copied and inserted hence the -2
above that are rows the rows that people add info to, if one of the cells have materials in as a value then
your code stops at that cell and copies the cel above that rather than looking down to the totals row and copying
the hidden row above it.
i tried adding an asterix to "materials*" and adding an asterixs coloured in white to the actual cell but no such look
is there a way to make the value its lookinf for explicit?
Thanks,
Dan
 
Upvote 0
Dan

What exactly are you looking for?

Is it 'Materials' or 'Named Materials'?
 
Upvote 0
Dan

What exactly are you looking for?

Is it 'Materials' or 'Named Materials'?


Hi Norie,

I’m looking for “materials*”

The asterix makes it unique because in the same column it could say materials in another row.

I need to find the materials with the asterix by it.

Hope that makes sense...

Thanks,
Dan
 
Upvote 0
Dan

That could be the problem, * is used as a wildcard character to find partial matches, that's probably why you had a problem with Find and it's definitely why Application.Match didn't work properly.
 
Upvote 0
Hi Norie

Thanks for your reply!
In the end I just moved the column that the I was looking for the match in, simple fix but works.. ill keep in mind the asterix being a wild card for future use.


Also, thanks for the bit of code, its a lot cleaner than using .find!


Cheers,
Dan
 
Upvote 0
If you literally need to match or find 'materials*' then you could use eg:

=MATCH("materials~*",A1:A100,0)

or with find look for:

materials~*

and match entire cell contents.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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