For each listrow - update data

dotsent

Board Regular
Joined
Feb 28, 2016
Messages
80
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

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,295
Office Version
  1. 365
Platform
  1. Windows
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]
 

dotsent

Board Regular
Joined
Feb 28, 2016
Messages
80
Office Version
  1. 365
Platform
  1. Windows
Thank you very much! That's exactly what I needed. Haven't come across Intersect function before.
 

Watch MrExcel Video

Forum statistics

Threads
1,132,913
Messages
5,655,919
Members
418,253
Latest member
TheJackal26

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
Top