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
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,036
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!
 

Forum statistics

Threads
1,085,031
Messages
5,381,303
Members
401,733
Latest member
Kabasa007

Some videos you may like

This Week's Hot Topics

Top