VBA Add a value to several cells based on a click in another cell

RuiFlora

Board Regular
Joined
Feb 28, 2014
Messages
58
Hello. I know this could be a widely discussed question but I deeply looked for an answer and wasn't able to find the best one.
I am trying to make a code which fill the value "x" to a range of cells (by columns), based on a single click in one other cell. Moreover i want that one another click on the main cell with the "x" turns blank the other ones. It may sounds confusing so i put an example.

The code I made up so far is the following, but it is not working right:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)


Dim regionInt As Range
Dim regionCell As Range

Set region = Worksheets("regiontest").Range("region")
Set regionfield = Worksheets("regiontest").Range("regionfield")

Set regionInt = Intersect(Target, Sheet3.Range("region"))
Set regionCell = Intersect(Target, Sheet3.Range("regionfield"))


If Not regionInt Is Nothing Then
For Each regionCell In regionInt
If regionCell.Value = "" Then
regionCell.Value = "x"
ElseIf regionCell.Value = "x" Then
regionCell.Value = ""
End If
Next
Sheet3.Range("A1").Select
End If

Set regionInt = Nothing
Set regionCell = Nothing

End Sub


Regionxx
Africax
Americaxx
Asiaxx
Europexx

<tbody>
</tbody>


In sum, the main goal is to click in front of "Region" and fill all the cells below (by column), And be able to invidividually remove one of them if necessary. And if we want to, click again in front of "region" to remove all of them.

Many thanks if someone can help me!

Best
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
RuiFlora,

I'm not sure how you are wanting to use your various named ranges but maybe this simple example will give you some ideas.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 1 Then Exit Sub
If Target.Cells.Count > 1 Then Exit Sub
If Target.Row = 1 Then
Lr = Cells(Rows.Count, 1).End(xlUp).Row
If Target = "" Then
Target.Resize(Lr, 1) = "*"
Else: Target.Resize(Lr, 1) = ""
End If
Exit Sub
End If
Target = ""
End Sub

Hope that helps.
 
Last edited:
Upvote 0
RuiFlora,

I'm not sure how you are wanting to use your various named ranges but maybe this simple example will give you some ideas.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 1 Then Exit Sub
If Target.Cells.Count > 1 Then Exit Sub
If Target.Row = 1 Then
Lr = Cells(Rows.Count, 1).End(xlUp).Row
If Target = "" Then
Target.Resize(Lr, 1) = "*"
Else: Target.Resize(Lr, 1) = ""
End If
Exit Sub
End If
Target = ""
End Sub

Hope that helps.

Thank you for your quick answer.

Maybe I have too much ranges named. The thing is, I have this table inserted between other ones. Therefore, I can't count till the end of the columns, thats why I have so much ranges. If i can handle this one, i'll just have to change the ranges names for other tables. What I want is to click in front of "region" and fill with "x" the cells below, ending in the end of the range.

Thanks
 
Upvote 0
Please tell me how you have defined your ranges, relative to the column of countries.
 
Upvote 0
Please tell me how you have defined your ranges, relative to the column of countries.

I tested the code you sent me and it's almost there!

The range I want to click to set all the "x" is the line of "region" except the cell of region (B2:E2)
The range i want to fill and edit further if necessary is (B3:E5)

If i would be able to add "x" in the regions even after I remove them as well, it would be great!

Thank you very much
 
Upvote 0
Try this.....

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, Range("B1:E5")) Is Nothing Then Exit Sub
If Target.Row = 1 Then
If Target = "" Then
Target.Resize(5, 1) = "*"
Else: Target.Resize(5, 1) = ""
End If
Exit Sub
End If
If Target = "" Then
Target = "*"
Else
Target = ""
End If
End Sub
 
Upvote 0
Try this.....

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, Range("B1:E5")) Is Nothing Then Exit Sub
If Target.Row = 1 Then
If Target = "" Then
Target.Resize(5, 1) = "*"
Else: Target.Resize(5, 1) = ""
End If
Exit Sub
End If
If Target = "" Then
Target = "*"
Else
Target = ""
End If
End Sub

Works like a charm! Many thanks
 
Upvote 0

Forum statistics

Threads
1,215,331
Messages
6,124,311
Members
449,152
Latest member
PressEscape

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