Extend range

Profi10

New Member
Joined
May 6, 2012
Messages
45
Hi,

Does anyone know why my code breaks if i change Target.address from cell G4 to a range G4:G100

Thanks

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Address = "$G$4" Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & ", " & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi,

Does anyone know why my code breaks if i change Target.address from cell G4 to a range G4:G100

Thanks

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Address = "$G$4" Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & ", " & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub
Please explain in words with specific details what you're attempting to achieve
 
Upvote 0
So, you have a data validation list in range ("G4")
And you want to use a sheet change event code to do something when you select a value from the list in Range (G4). It that true?

To start with I do not believe you can select more than one value at a time in a data validation list
The sheet change event will run when you change the value in range (G4)
 
Upvote 0
So, you have a data validation list in range ("G4")
And you want to use a sheet change event code to do something when you select a value from the list in Range (G4). It that true?

To start with I do not believe you can select more than one value at a time in a data validation list
The sheet change event will run when you change the value in range (G4)
I have a data validation list in range G4:G100 however it is only letting me select multiple items from the drop down list in cell G4 while in cells G5:G100 I can only select one item from the drop down list
 
Upvote 0
As per image in cell G4 I can select numerous items while in any other cell from G5 onwards I can only select one item from the list
 

Attachments

  • Screenshot_20220906-142355_Outlook.jpg
    Screenshot_20220906-142355_Outlook.jpg
    23.3 KB · Views: 5
Upvote 0
Have figured out that if I add the following it works however if you have to do it for hundreds of cells it is very time consuming

If Target.Address = "$G$4" Or Target.Address = "$G$5"
 
Upvote 0
Well, I see nothing in your image

So, I think your wanting to enter certain values in a Range Like G4 to G33

So, after you enter the value in G4 what do you want to happen?

Are you saying if you already have "Alpha" in G4 and then you select Value Bravo in G4 you want
G4 to now have Alpha Bravo?
 
Upvote 0
Yes I want G4 to have values Alpha, Bravo etc. but I also want to be able to do that across other cells in column G.
 
Upvote 0

Forum statistics

Threads
1,214,793
Messages
6,121,619
Members
449,039
Latest member
Mbone Mathonsi

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