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

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
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,214,606
Messages
6,120,485
Members
448,967
Latest member
visheshkotha

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