Clearing contents of cells in different sheets with the help of a listbox & a command button

blackhawk56z

New Member
Joined
May 25, 2016
Messages
8
Hi I am a total newbie at VBA and I have searched the forum quite extensively but haven't found anything that is specific to what I need.

I have created a multi-select listbox (activeX control) in which I displayed the names of each worksheet. What I need for the command button is to help me clear contents (Range "B2:B15" and Range "D3:D5") for a range of cells in those worksheets that I have selected in the listbox.

Most threads here keep throwing me solutions on how to clear the contents in the listboxes themselves instead of what i need.

Below is my code:
Code:
Private Sub clrcnt_click()
Worksheets(ListBox1.MultiSelect).ClearContents
End Sub


Sub UserForm_Initialize()
Dim N As Long
For N = 2 To ActiveWorkbook.Sheets.Count
    ListBox1.AddItem ActiveWorkbook.Sheets(N).Name
Next N
End Sub
[\code]

Forgive me if this looks stupid I just started learning vba for a few days only.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Update: Oops I pasted the code wrongly.

Hi I am a total newbie at
<acronym title="visual basic for applications" style="border-width: 0px 0px 1px; border-bottom-style: dotted; border-bottom-color: rgb(0, 0, 0); cursor: help; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);">VBA</acronym> and I have searched the forum quite extensively but haven't found anything that is specific to what I need.

I have created a multi-select listbox (activeX control) in which I displayed the names of each worksheet. What I need for the command button is to help me clear contents (Range "B2:B15" and Range "D3:D5") for a range of cells in those worksheets that I have selected in the listbox.

Most threads here keep throwing me solutions on how to clear the contents in the listboxes themselves instead of what i need.

Below is my code:
Rich (BB code):
Rich (BB code):
Private Sub clrcnt_click()
Worksheets(ListBox1.MultiSelect).ClearContents
End Sub


Sub UserForm_Initialize()
Dim N As Long
For N = 2 To ActiveWorkbook.Sheets.Count
ListBox1.AddItem ActiveWorkbook.Sheets(N).Name
Next N
End Sub


Forgive me if this looks stupid I just started learning <acronym title="visual basic for applications" style="border-width: 0px 0px 1px; border-bottom-style: dotted; border-bottom-color: rgb(0, 0, 0); cursor: help; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);">vba</acronym> for a few days only.
 
Upvote 0

Forum statistics

Threads
1,223,756
Messages
6,174,320
Members
452,555
Latest member
colc007

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