Change a section of code when calling it

Eddy2

New Member
Joined
Mar 2, 2017
Messages
27
Hi
I have a simple code i want to call in several occasions. Is it possible to change "qwr" when calling it?

Code:
ActiveSheet.ListObjects(1).ListColumns("qwr").DataBodyRange.ClearContents

Cheers!
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
You have a code something like this
Code:
Sub ClearTableColumn(col_Header As String)
  ActiveSheet.ListObjects(1).ListColumns(col_Header).DataBodyRange.ClearContents
End Sub

Then when you want to clear a column you use, say
Code:
Sub test()
  ClearTableColumn "qwr"
End Sub
.. and next time it might be
Code:
Sub test()
  ClearTableColumn "abc def"
End Sub
 
Upvote 0
This works. Thanks!


You have a code something like this
Code:
Sub ClearTableColumn(col_Header As String)
  ActiveSheet.ListObjects(1).ListColumns(col_Header).DataBodyRange.ClearContents
End Sub

Then when you want to clear a column you use, say
Code:
Sub test()
  ClearTableColumn "qwr"
End Sub
.. and next time it might be
Code:
Sub test()
  ClearTableColumn "abc def"
End Sub
 
Upvote 0
This works. Thanks!
You are welcome.

I was thinking that my "use" examples weren't really very good. This one might be better.
Code:
Sub test()
  Dim ColsToClear As Variant, itm As Variant
  
  ColsToClear = Array("qwr", "abc def", "Date")
  For Each itm In ColsToClear
    ClearTableColumn CStr(itm)
  Next itm
End Sub
 
Upvote 0
.. and it didn't have to be in a separate routine, that was just one way of doing it. You could also have a structure like this
Code:
Sub test2()
  Dim ColsToClear As Variant, itm As Variant
  
  ColsToClear = Array("qwr", "abc def", "Date")
  For Each itm In ColsToClear
    ActiveSheet.ListObjects(1).ListColumns(itm).DataBodyRange.ClearContents
  Next itm
End Sub
 
Last edited:
Upvote 0
Even Better. Cheers!

.. and it didn't have to be in a separate routine, that was just one way of doing it. You could also have a structure like this
Code:
Sub test2()
  Dim ColsToClear As Variant, itm As Variant
  
  ColsToClear = Array("qwr", "abc def", "Date")
  For Each itm In ColsToClear
    ActiveSheet.ListObjects(1).ListColumns(itm).DataBodyRange.ClearContents
  Next itm
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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