Update Data in Listobject

maria90

New Member
Joined
Apr 9, 2012
Messages
38
Dear Experts

What is the most elegant method to update data in a listobject (with a userform)?

This is the table tblCustomers:

IDNameStreetPostal CodePhoneMobile

<tbody>
</tbody>

The userform frmCustomer has:
txtName , txtStreet, txtPostalCode, txtPhone, txtMobile


Is this the correct procedure if I want to update a value - if the ID is given?

1) Search for the column containing the (customer) ID
2) Once the row number has been found, update the values in the rows with the userform data.


I see that Listobjects has a DataBodyRange.Cells. Is Cells(rows/columns) what I need to look at or is there another way?
I obviously want to make sure that the data entered will be converted to the same number/cell format.

Any feedback is greatly appreciated!

Maria
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Let's assume that Sheet1 contains the listobject, and that the userform also contains a textbox for the ID. Try...

Code:
[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]

[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] CommandButton1_Click()
    [COLOR=darkblue]Dim[/COLOR] rFound [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] sID [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    sID = Me.txtID
    [COLOR=darkblue]With[/COLOR] Worksheets("Sheet1").ListObjects("tblCustomers").ListColumns(1).DataBodyRange
        [COLOR=darkblue]Set[/COLOR] rFound = .Find(what:=sID, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    [COLOR=darkblue]If[/COLOR] [COLOR=darkblue]Not[/COLOR] rFound [COLOR=darkblue]Is[/COLOR] [COLOR=darkblue]Nothing[/COLOR] [COLOR=darkblue]Then[/COLOR]
        rFound.Offset(, 1).Value = Me.txtName
        rFound.Offset(, 2).Value = Me.txtStreet
        rFound.Offset(, 3).Value = Me.txtPostalCode
        rFound.Offset(, 4).Value = Me.txtPhone
        rFound.Offset(, 5).Value = Me.txtMobile
    [COLOR=darkblue]Else[/COLOR]
        MsgBox "Customer ID '" & sID & "' not found.", vbExclamation
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
End [COLOR=darkblue]Sub[/COLOR]

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,213,583
Messages
6,114,480
Members
448,574
Latest member
bestresearch

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