'Update Table (ListObject) Row Values, based on first cell value
I want to update data in any table using this format:
Dim Update As String
Dim FindUpdate As Range
Update = Range("USER_NAME").value
With Sheets("Settings")
Set FindUpdate = .Range("USERS_TABLE").Find(What:=Update, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext)
.Cells(FindUpdate.row, 6).value = Computer
End With
This code works but it requires me to put this table at the beginning of the Sheet.
I want to change values in a table (ListObjects("USERS_TABLE")) regardless of where it's positioned in the worksheet. Changing the range to a table's name USERS_TABLE did not do the trick.
Ideally I want to change the above code to:
Dim Update As String
Update = Range("USER_NAME").value ' This is a Namedrange
' declare a variables that will point to a table I want to access
dim ws as Worksheet
Set ws = ActiveSheet
Dim table_row as ListRow
Set table_row = ws.ListObject("USERS_TABLE").ListRows.**SOMETHING_TO_HELP_ACCESS_THE_TABLE_ROW_HERE**
Dim FindUpdate As Range
With table_row ' REPLACE SHEETS WITH TABLE_ROW
Set FindUpdate = .**Row?**.Find(What:=Update, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext)
' AND CAUSE IT'S A TABLE, MAYBE A COLUMN HERE?
.**Cells(or Range)?**(FindUpdate.**row**, 6).value = Computer
End With
Solution 1:[1]
TO @VBasic2008, sorry for the confusion, but... This is what I have... It works I can add rows using this
Public Sub Add_Table_Row()
Dim ws As Worksheet
Set ws = ActiveSheet
Dim add_row As ListRow
Set add_row = ws.ListObjects("Names_Table").ListRows.Add(1)
With add_row
.Range(3) = "Sample Text"
End With
End Sub
I want to use the above method to update Table values, like this...
Public Sub Update_Table_Row()
Dim ws As Worksheet
Set ws = ActiveSheet
Dim update_row As ListRow
Set update_row = ws.ListObjects("Names_Table").ListRows ' STUCK HERE
With update_row
.Range(3) = "Change This Text"
End With
End Sub
Solution 2:[2]
is it work?
Public Sub Update_Table_Row()
Dim ws As Worksheet
Set ws = ActiveSheet
Dim update_row As ListRow
Set update_row = ws.ListObjects("Names_Table").ListRows ' STUCK HERE
With update_row
.Range(3) = "Change This Text"
End With
End Sub
Solution 3:[3]
Sorry for the late reply... Found a simple solution to ADD and UPDATE a Table (ListObjects.ListRow) Row
1. Add Table Row with values
Sub Add_Product()
' Declare Products Database Variable
Dim Products_Table As ListObject
Set Products_Table = Sheets("Products").ListObjects("PRODUCT_DATABASE")
' Declare Add Product Variable
Dim Product_New_Row As ListRow
Set Product_New_Row = Products_Table.ListRows.Add(1)
With Product_New_Row
.Range(1) = "PRODUCTSKU01" ' 1st Column
.Range(2) = "New Product Name" ' 2nd Column
.Range(5) = 1000 ' 5th Column
End With
End Sub
2. Change Table Row Values
Sub Update_Product()
' Declare Products Database Variable
Dim Products_Table As ListObject
Set Products_Table = Sheets("Products").ListObjects("PRODUCT_DATABASE")
' Declare Search Value
Dim Find_Value As String
Find_Value = "PRODUCTSKU01" ' 1st Column of the Products Database
' Declare Product Row
Dim Product_Update_Row As Range
Set Product_Update_Row = Products_Table.DataBodyRange.Find( _
What:=Find_Value, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext _
)
' Update Product
With Product_Update_Row
' Change column # value
.Cells(1, 2).value = "Change Product Name"
End With
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SilasWorx |
| Solution 2 | robart wil |
| Solution 3 | SilasWorx |
