Fill a range of cells with a value

Tomsbdc

New Member
Joined
May 25, 2015
Messages
4
Hi, I'm trying to create a macro in a cell that when clicked will clear the number values posted in range a2:a15 with the number 1; how do I do this? I'm not familiar with macros. Thanks
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Tomsbdc,

Welcome to the MrExcel forum.

1. What version of Excel, and, Windows are you using?

2. Are you using a PC or a Mac?

So that we can get it right on the first try:

3. What is the worksheet name?

4. What cell are you going to click to activate the Worksheet_SelectionChange event?
 
Upvote 0
Tomsbdc,

Sample raw data with the Yellow cell is the cell to click on to activate the Worksheet_SelectionChange event code.


Excel 2007
A
1
22
33
44
55
66
77
88
99
1010
1111
1212
1313
1414
1515
16
Sheet1


After you click in/on cell A1, you get this:


Excel 2007
A
1
21
31
41
51
61
71
81
91
101
111
121
131
141
151
16
Sheet1



1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Select the worksheet in which your code is to run
3. Right click on the sheet tab and choose View Code to open the Visual Basic Editor
4. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
5. Press the keys ALT + Q to exit the Editor, and return to Excel

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' hiker95, 05/25/2015, ME857104
If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub
With Application
  .EnableEvents = False
  .ScreenUpdating = False
  Me.Range("A2:A15").Value = 1
  .ScreenUpdating = True
  .EnableEvents = True
End With
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then select cell A1.
 
Upvote 0
Hiker95 - Thanks very much, this worked very well and I was able to 'add' to the code to refill another area as well, making my sheet work even better. I appreciate the help!

Tomsbdc,

Sample raw data with the Yellow cell is the cell to click on to activate the Worksheet_SelectionChange event code.

Excel 2007
A
1
22
33
44
55
66
77
88
99
1010
1111
1212
1313
1414
1515
16

<colgroup><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1



After you click in/on cell A1, you get this:

Excel 2007
A
1
21
31
41
51
61
71
81
91
101
111
121
131
141
151
16

<colgroup><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1




1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Select the worksheet in which your code is to run
3. Right click on the sheet tab and choose View Code to open the Visual Basic Editor
4. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
5. Press the keys ALT + Q to exit the Editor, and return to Excel

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' hiker95, 05/25/2015, ME857104
If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub
With Application
  .EnableEvents = False
  .ScreenUpdating = False
  Me.Range("A2:A15").Value = 1
  .ScreenUpdating = True
  .EnableEvents = True
End With
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then select cell A1.
 
Upvote 0
Tomsbdc,

Thanks for the feedback x 2.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,737
Members
449,050
Latest member
excelknuckles

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