update workbook when row added or deleted

Beneindias

Board Regular
Joined
Jun 21, 2022
Messages
120
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi all,

I have vba code to update the workbook when there are changes in a table.

My problem is that if I add a row to the table, it calls the code and updates the workbook, but if I delete a row, nothing hapens.

My code is this on:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim col As Range
    
    'Sets range to column "Código" in table "Obras"
    Set col = ListObjects("Obras").ListColumns("Código").Range
    
    'Checks if cell is inside range of column "Código"
    If Not Intersect(col, Target) Is Nothing Then

        'Selection IS inside the range.
        ThisWorkbook.RefreshAll

    End If
End Sub

So, this is working when I add a row, but not when I delete a row.
Is there a way to change this, or do I need to find another solution?

Thnak you all
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Are you sure it's not working?

It works for me. Try changing the line ThisWorkbook.RefreshAll to MsgBox ("Table changed") and see when you get the message.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim col As Range
    
    'Sets range to column "Código" in table "Obras"
    Set col = ListObjects("Obras").ListColumns("Código").Range
    
    'Checks if cell is inside range of column "Código"
    If Not Intersect(col, Target) Is Nothing Then

        'Selection IS inside the range.
        MsgBox ("Table changed")

    End If
End Sub
 
Upvote 0
This script works for me. I used the one with Message Box
Not sure what this does:
ThisWorkbook.RefreshAll
 
Upvote 0
I tried the code with the MsgBox again, and it only works when I add a row to the table.
If I delete a row, it does nothing.

And no, I don't have more code in that sheet.

Meanwhile, I made it work with this code:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim What As String

    Application.ScreenUpdating = False
    
    'Atualiza as tabelas quando é feita alguma alteração na folha "Tabelas Apoio"
    ThisWorkbook.RefreshAll
    
    'Atualiza as tabelas se for apagada alguma linha da tabela de obras na folha "Tabelas Apoio"
    If Target.Rows.Count = Rows.Count Then
        What = "Column " & Target.Column & " Deleted along with " & Target.Columns.Count - 1 & " additonal columns"
        ThisWorkbook.RefreshAll
    End If
    
    Application.ScreenUpdating = True
    
End Sub

It's not the most elegant solution, but it's updating the workbook.
Maybe I will polish it and add the intersect to this code, when I have a bit more free time.

Thanks
 
Upvote 0
Solution
This block will only execute if you change every row in the sheet at once:

VBA Code:
    If Target.Rows.Count = Rows.Count Then
        What = "Column " & Target.Column & " Deleted along with " & Target.Columns.Count - 1 & " additonal columns"
        ThisWorkbook.RefreshAll
    End If

and all it will do is refresh all a second time, since your code already does that once whenever any change is made (which seems very inefficient).
 
Upvote 0
This block will only execute if you change every row in the sheet at once:

VBA Code:
    If Target.Rows.Count = Rows.Count Then
        What = "Column " & Target.Column & " Deleted along with " & Target.Columns.Count - 1 & " additonal columns"
        ThisWorkbook.RefreshAll
    End If

and all it will do is refresh all a second time, since your code already does that once whenever any change is made (which seems very inefficient).
It doesn't refresh when I delete a row of the table!!!
 
Upvote 0
I thought you said you had it fixed??
 
Upvote 0

Forum statistics

Threads
1,224,936
Messages
6,181,855
Members
453,068
Latest member
DCD1872

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