Update row if record found

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,404
Office Version
  1. 2016
Platform
  1. Windows
I'm trying to update the row on a sheet if a record is found, I'm trying this but it's not working;

Code:
Set ws = Worksheets("Training")
Set SearchRange = Range("A2", Range("A65536").End(xlUp))
Set FindRow = SearchRange.Find(ComboBox1.Value, LookIn:=xlValues, lookat:=xlWhole)
Set Row = FindRow.Row
realDate = Me.Controls.Item("TextDate").Text
ws.Cells(Row, 4).Value = DateSerial(Year(realDate), Month(realDate), Day(realDate))
ws.Cells(Row, 5).Value = Me.Controls.Item("ComboType").Text
ws.Cells(Row, 6).Value = Me.Controls.Item("ComboInst1").Text
ws.Cells(Row, 7).Value = Me.Controls.Item("ComboInst2").Text

I'm getting Type Mismatch with this highlighted (within FindRow.Row) ;

Code:
.Row
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi,
change this:

Code:
Set Row = FindRow.Row

to this:

Code:
Row = FindRow.Row

Where your Row variable would be declared as a Long Data type - although personally, I would use something like r instead of Row.

Dave
 
Upvote 0
Hi Dave,

Thanks - but now I'm getting a run time error 91, 'Object variable or with block variable not set' with the following highlighted;

Code:
Row = FindRow.Row
 
Upvote 0
Change your variable Row to something else like r

Code:
           r = FindRow.Row
            realDate = Me.Controls.Item("TextDate").Text
            ws.Cells(r, 4).Value = DateSerial(Year(realDate), Month(realDate), Day(realDate))
            ws.Cells(r, 5).Value = Me.Controls.Item("ComboType").Text
            ws.Cells(r, 6).Value = Me.Controls.Item("ComboInst1").Text
            ws.Cells(r, 7).Value = Me.Controls.Item("ComboInst2").Text

r should be declared as Long Data type

see if this solves your problem.

Dave
 
Upvote 0
That's works great Dave, thanks - I think part of my problem may have been I had Row as Range but regardless you've solved the issue!
 
Upvote 0
That's works great Dave, thanks - I think part of my problem may have been I had Row as Range but regardless you've solved the issue!

I did state in my first post that Row variable would be declared as a Long Data type
but Row is one of those words in VBA best avoided for use as a variable.

Also, you may want to consider adding a test to your code to cover search values not found e.g.

Code:
        If Not FindRow Is Nothing Then
            r = FindRow.Row
            'rest code
        Else
            MsgBox Me.ComboBox1.Value & Chr(10) & "Record Not Found", 48, "Not Found"
        End If

Glad resolved for you.

Dave
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,731
Members
448,987
Latest member
marion_davis

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