Excel VBA: Replace values in Listobject

Massimo Scola
Massimo Scola used Ask the Experts™
on
I am importing some data with a MS QUERY from another workbook and the data is put into a table called CUSTOMERS.

The listobject has several columns and one of them is "First Order Date".
There is one value that I want to remove (not hide with a cell format): 00.01.1900  00:00:00

Is there a neat way to delete this value in the entire listobject with VBA?
I do not mind if the data is converted to a string if that makes it easier and the cell is empty

Thanks

Massimo
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
Hi,

pls try
Sub Macro1()
Application.ScreenUpdating = False
For Each c In Range("Table1[First Order Date]")
    If c.Value = 0 Then
        c.ClearContents
    End If
Next
Application.ScreenUpdating = True
End Sub

Open in new window

Regards
ShumsManaging Director/Excel VBA Developer
Distinguished Expert 2018
Commented:
Hi,

Try below:
Sub DeleteRows_Table()
Dim Ws As Worksheet
Dim j As Long
Application.ScreenUpdating = False
Set Ws = ActiveSheet
With Ws.ListObjects("CUSTOMERS")
    For j = .ListRows.Count To 1 Step -1
        If .ListRows(j).Range(1, .ListColumns("First Order Date").Index) = "00.01.1900  00:00:00" Then
        .ListRows(j).Delete
        End If
    Next j
End With
Application.ScreenUpdating = True
End Sub

Open in new window

Top Expert 2016
Commented:
corrected code if the Table is customers
Sub Macro1()
Application.ScreenUpdating = False
For Each c In Range("Customers[First Order Date]")
    If c.Value = 0 Then
        c.ClearContents
    End If
Next
Application.ScreenUpdating = True
End Sub

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Massimo ScolaSoftware Engineer

Author

Commented:
Thanks guys ..the codes work like a charm.
ShumsManaging Director/Excel VBA Developer
Distinguished Expert 2018

Commented:
You're Welcome Massimo! We're glad we're able to help
Massimo ScolaSoftware Engineer

Author

Commented:
I do have one more follow up question though ..

What if I want to apply this code to every column of the listobject?
How would you amend the code?
ShumsManaging Director/Excel VBA Developer
Distinguished Expert 2018

Commented:
Do you want answer here or you gonna raise a new question?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial