Resetting an entire table column from VBA

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,676
Office Version
  1. 365
Platform
  1. Windows
I am trying to work with this table in some VBA code:

Lunch log.xlsm
BCDEF
4Name#LastFreqNext
5Moss, Pete110/04/99 0 mo 
6Bacque, Helen310/21/99 1 mo11/20/99
7Horne, Henrietta13/03/99 6 mo8/30/99
8Andertal, Neal14/16/99 12 mo4/10/00
9Baath, Anita110/14/99  
Names
Cell Formulas
RangeFormula
F5:F8F5=IF(AND([@Freq]>0,MyIsDate([@Last])),[@Last]+([@Freq]*30),"")
F9F9=IF([@Freq]>0,[@Last]+([@Freq]*30),"")


This code is supposed to reset the # column to zeroes. The first statement works, the second one does not. Why is that?

VBA Code:
Const rnTblNames As String = "Names"
Const rnColNum   As String = "Names[#]"
Dim NameRows As Long
Dim iName As Variant

NameRows = Range(rnTblNames).rows.Count
For iName = 1 To NameRows
  Range(rnTblNames)(iName, colNNum) = 0  'This works
  Range(rnColNum)(iName) = 0             'This does not work
Next iName

Is there a statement that will reset the entire column to zero without looping?

Thanks
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
You seem to be doing a bit of work on Tables at the moment ;)

I might be worth setting up ListObject variable in your code to make it easier to work with.
(Note: In the below if you use Range instead of DataBodyRange it will include the columns headings)

VBA Code:
Sub TableColReset_JM_Mod()

    Const rnTblNames As String = "Names"
    Const rnColName   As String = "#"
   
    Dim tblData As ListObject
    Set tblData = Range(rnTblNames).ListObject
    tblData.ListColumns(rnColName).DataBodyRange = 0
   
End Sub

Your "rnColNum" was defined as a String, if you really wanted to use a number then

VBA Code:
    Dim colNo As Long
    colNo = 3
    tblData.ListColumns(colNo).DataBodyRange = 0
 
Last edited:
Upvote 0
You seem to be doing a bit of work on Tables at the moment ;)
Quite right -- and I think a lot of it is your fault. 🤔🤩

I might be worth setting up ListObject variable in your code to make it easier to work with.
Is ListObject the VBA name for Table? That is, is it an object that is defined as a table in the sheet?

(Note: In the below if you use Range instead of DataBodyRange it will include the columns headings)

VBA Code:
Sub TableColReset_JM_Mod()

    Const rnTblNames As String = "Names"
    Const rnColName   As String = "#"
   
    Dim tblData As ListObject
    Set tblData = Range(rnTblNames).ListObject
    tblData.ListColumns(rnColName).DataBodyRange = 0
   
End Sub
So I can refer to the columns by their names (headings), right? I have been assigning constants to the column numbers so I could use subscripts.

This gives me more to work on. (sigh) 😘
 
Upvote 0
Is ListObject the VBA name for Table? That is, is it an object that is defined as a table in the sheet?
I know how you love MS. Yes Tables is actually a bit of an unfortunate term since it is just too common and hence makes searching quite difficult.
Tables were originally called Lists in Excel and hence ListObjects in VBA. In fact although most people use Ctrl+T to create a table, Ctrl+T is used for something else in some languages and it is actually Ctrl+L that works for everyone

So I can refer to the columns by their names (headings), right? I have been assigning constants to the column numbers so I could use subscripts.
Yes, you can use column headings (I actually added the number option to my previous post after you had already seen it)

I have found the link below to be handy reference page.
The VBA Guide To ListObject Excel Tables
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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