For each listrow - update data

dotsent

Board Regular
Joined
Feb 28, 2016
Messages
89
Office Version
  1. 365
Platform
  1. Windows
HI! I'm a beginner (at best) with VBA loops and would need to adjust data in Excel table rows, depending on a single column cell value. I want to loop through all rows for this table and adjust data depending on value in 3rd column ("Check"). If column "Check" value is 1, insert value "Disable" to 2nd column ("Status"). Would be nice to make the code a little bit more advanced, so that under the same condition, "Check" = 1 would also clear contents from 4th column ("Quantity") but if one thing works, it's already great.

I think I should use For Each listrow function here and run IF-clause inside each loop, but as simple as it looks, I'm not sure how to address correct listcolumn within a single listrow (e.g. address correct cell). Thanks!

VBA Code:
Sub Update_listrow()

Dim lstobj As ListObject
Dim lstrw As ListRow

Set lstobj = ActiveSheet.ListObjects("Table1")

For Each lstrw In lstobj.ListRows

'If...

Next lstrw

End Sub

.
listobject.jpg
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
You could try something like this which uses the column headers, i.e. Check, Quantity and Status, to identify the columns of interest as we loop through the rows.
VBA Code:
Option Explicit

Sub Update_Table()
Dim lstobj As ListObject
Dim lstrw As ListRow

Set lstobj = ActiveSheet.ListObjects("Table1")

    For Each lstrw In lstobj.ListRows

        If Intersect(lstrw.Range, lstobj.ListColumns("Check").Range).Value = 1 Then
            Intersect(lstrw.Range, lstobj.ListColumns("Status").Range).Value = "Disable"
            Intersect(lstrw.Range, lstobj.ListColumns("Quantity").Range).Clear
        End If

    Next lstrw

End Sub
[/vba]
 
Upvote 0
Thank you very much! That's exactly what I needed. Haven't come across Intersect function before.
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,987
Members
452,373
Latest member
TimReeks

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