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

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
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,223,897
Messages
6,175,270
Members
452,628
Latest member
dd2

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