Autorun macro when i change value in column 8 in a table

Golaidron

Board Regular
Joined
Jan 23, 2013
Messages
83
Office Version
  1. 365
Platform
  1. Windows
How can i get this macro to run everytime i change value in column 8 in a table.

Dim ws As Worksheet
Dim table As ListObject
Dim tblRows As ListRows
Dim tblRow As ListRow


Set ws = Sheet1
Set table = ws.ListObjects("Table1")

Set tblRows = table.ListRows

For Each tblRow In tblRows
If table.DataBodyRange(tblRow.Index, 8) = "Defect" Then
table.DataBodyRange(tblRow.Index, 10) = "New test"
ElseIf table.DataBodyRange(tblRow.Index, 8) = "Ok" Then
table.DataBodyRange(tblRow.Index, 10) = table.DataBodyRange(tblRow.Index, 6) + 366
ElseIf table.DataBodyRange(tblRow.Index, 8) = "" Then
table.DataBodyRange(tblRow.Index, 10) = ""
End If

Next tblRow
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Put your code in the sheet's 'Worksheet_Change' event.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H:H")) Is Nothing Then
    'Code to be executed
End If
End Sub
 
Upvote 0
Try this code, which goes in the module of the worksheet which has the table on it.
VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
Dim table As ListObject
Dim tblRows As ListRows
Dim tblRow As ListRow

    Set table = Me.ListObjects("Table1")

    If Intersect(Target, table.ListColumns(8).DataBodyRange) Is Nothing Then Exit Sub
    
    Application.EnableEvents = False
    
    Set tblRows = table.ListRows

    For Each tblRow In tblRows
        If table.DataBodyRange(tblRow.Index, 8) = "Defect" Then
            table.DataBodyRange(tblRow.Index, 10) = "New test"
        ElseIf table.DataBodyRange(tblRow.Index, 8) = "Ok" Then
            table.DataBodyRange(tblRow.Index, 10) = table.DataBodyRange(tblRow.Index, 6) + 366
        ElseIf table.DataBodyRange(tblRow.Index, 8) = "" Then
            table.DataBodyRange(tblRow.Index, 10) = ""
        End If

    Next tblRow
    
    Application.EnableEvents = True
    
End Sub
 
Upvote 0
Thanks, but i get an issue with the tblRow.Index. Can this row.index be reset after en update?
 
Upvote 0
What issue do you get?

Do you actually need tblRow.Index and the loop?
 
Upvote 0
Hi Norie, I don't need tblrow.index, most of this formula is copyed from others. If you have a better idea, please share.
 
Upvote 0
Do you want to make changes to other columns in the table based on what value is entered in column 8?
 
Upvote 0
Hi Norie

This is what i want to happen.

When im in row x (in table) and make a change to column 8 (Status), then row x and column 10 will either show "ASAP" or Date in column 6 +366 days
The macro doesn't have to go through every row i table every time I change a value i column 8. Just the row i am changing.

Here is my workbook.
Autorun marco

Please help and make me look good in front of my boss. ;)
 
Upvote 0

Forum statistics

Threads
1,214,638
Messages
6,120,676
Members
448,977
Latest member
moonlight6

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