How to update a specific cell in a table from VBA?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,535
Office Version
  1. 365
Platform
  1. Windows
What is the VBA code I would need to use to update the Quan cell for Item C? I want to use Table addressing, not "D8". That is, I will have found item C and now I want to update its Quan value.

Suppose I have the name of the table stored in the VBA variable rnTable.

Tables.xlsx
CD
5ItemQuan
6A10
7B25
8C
9D12
10E5
VBA Access
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Maybe
VBA Code:
rnTable.DataBodyRange.Cells(3, rnTable.ListColumns("Quan").Index).Value = "xxx"
or
VBA Code:
rnTable.ListColumns("Quan").DataBodyRange(3, 1).Value = "zzzz"
 
Upvote 0
Maybe
VBA Code:
rnTable.DataBodyRange.Cells(3, rnTable.ListColumns("Quan").Index).Value = "xxx"
or
VBA Code:
rnTable.ListColumns("Quan").DataBodyRange(3, 1).Value = "zzzz"
Do I first have to search the table for the row that "C" is on? How do I do that?
 
Upvote 0
Maybe something like...
VBA Code:
Sub FindC()
    Dim ColRng As Range, FindRow As Range
    Dim rnTable As ListObject
    
    Set rnTable = ActiveSheet.ListObjects(1)
    
    Set ColRng = rnTable.ListColumns("Item").Range
    Set FindRow = ColRng.Find(What:="C", After:=ColRng(1))

    If Not FindRow Is Nothing Then MsgBox FindRow.Row - rnTable.HeaderRowRange.Row

End Sub
 
Upvote 0
Maybe something like...
VBA Code:
Sub FindC()
    Dim ColRng As Range, FindRow As Range
    Dim rnTable As ListObject
   
    Set rnTable = ActiveSheet.ListObjects(1)
   
    Set ColRng = rnTable.ListColumns("Item").Range
    Set FindRow = ColRng.Find(What:="C", After:=ColRng(1))

    If Not FindRow Is Nothing Then MsgBox FindRow.Row - rnTable.HeaderRowRange.Row

End Sub
If I load the table into a VBA array, then any changes I make are only to the VBA copy, right?

ListObjects, on the other hand, are "links" to the actual data in the sheet. So any changes I make will be updated in the sheet, right?
 
Upvote 0
I got this code to work to allow the user to modify the Quan value for any item in the table. It seems a little clunky. Is there anything I can do to simplify it or is there a better way?

VBA Code:
Sub ListObjectTest2()
Const rnTable As String = "TblTest"
Dim loTable As ListObject
Dim ColRng As Range
Dim FindRow As Range
Dim Item As String
Dim RowNum As Long
Dim QuanCol As Long
Dim Value As Variant

' Define the ListObject variable for the table
On Error GoTo BadTableName
  Set loTable = Range(rnTable).ListObject
On Error GoTo 0
  GoTo TableNameOK
BadTableName:
  MsgBox "Invalid table name (" & rnTable & ")", vbOKCancel
  Exit Sub
TableNameOK:

QuanCol = loTable.ListColumns("Quan").Index

Item = InputBox("Enter the item or 'qq' to quit", MyName)
If UCase(Item) = "QQ" Then Exit Sub
Set ColRng = loTable.ListColumns("Item").Range
Set FindRow = ColRng.Find(What:=Item, After:=ColRng(1))
If FindRow Is Nothing Then
  MsgBox "Item '" & Item & "' not found"
Else
  RowNum = FindRow.Row - loTable.HeaderRowRange.Row
  Value = InputBox("Enter new value for Item '" & Item & "'")
  loTable.DataBodyRange(RowNum, QuanCol) = Value
End If

MsgBox "Done", vbOKCancel

End Sub

Thanks
 
Upvote 0

Forum statistics

Threads
1,215,247
Messages
6,123,857
Members
449,129
Latest member
krishnamadison

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