Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Drop-Down Macro

I found the following macro during some internet scrounging. If you have a
drop-down data-validation list in Column C, it builds an accumulative list in
a cell from items selected from the list. (IE, if my validation list is of
Jeeves, Wooster, and Tuppy, each time I select Jeeves from the drop down, it
will add it to the cell with a comma: "Jeeves, Jeeves, Jeeves, Jeeves,
Jeevesr":

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
If Target.Count 1 Then GoTo exitHandler

On Error Resume Next
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler

If rngDV Is Nothing Then GoTo exitHandler

If Intersect(Target, rngDV) Is Nothing Then
'do nothing
Else
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If Target.Column = 3 Then
If oldVal = "" Then
'do nothing
Else
If newVal = "" Then
'do nothing
Else
Target.Value = oldVal _
& ", " & newVal
End If
End If
End If
End If

exitHandler:
Application.EnableEvents = True
End Sub

Unfortunately, I really know almost (in fact, maybe exactly) nothing about
VBA. I DO know that I have a workbook of 10-odd sheets, each one of which has
three to four columns which have drop-down validations which I need to apply
this macro to. I know how to change the column the macro refers to (changing
"3" in the line "If Target.Column = 3 if" to the correct column number), but
I don't know how to make it so thiat macro works for multiple columns in each
sheet (say, Columns 4, 8, 10, and 18) all the time.

What is the best way to do this? I know this part of the newsgroup is for
users who know what they're doing, but any help you can provide to a total
neophyte would be much appreciated!

Desperate in Columbus

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Drop-Down Macro

Michael,

Try the version below.

HTH,
Bernie
MS Excel MVP


Private Sub Worksheet_Change(ByVal Target As Range)
Dim oldVal As String
Dim newVal As String
Dim myType As Variant

If Target.Count 1 Then Exit Sub

'Make it only work on columns 4, 8, 10, and 18
If Target.Column < 4 And _
Target.Column < 8 And _
Target.Column < 10 And _
Target.Column < 18 Then Exit Sub

On Error GoTo exitHandler
myType = Target.Validation.Type

Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If oldVal = "" Then
'do nothing
Else
If newVal = "" Then
'do nothing
Else
Target.Value = oldVal _
& ", " & newVal
End If
End If

exitHandler:
Application.EnableEvents = True
End Sub


"Michael Link" wrote in message
...
I found the following macro during some internet scrounging. If you have a
drop-down data-validation list in Column C, it builds an accumulative list in
a cell from items selected from the list. (IE, if my validation list is of
Jeeves, Wooster, and Tuppy, each time I select Jeeves from the drop down, it
will add it to the cell with a comma: "Jeeves, Jeeves, Jeeves, Jeeves,
Jeevesr":

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
If Target.Count 1 Then GoTo exitHandler

On Error Resume Next
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler

If rngDV Is Nothing Then GoTo exitHandler

If Intersect(Target, rngDV) Is Nothing Then
'do nothing
Else
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If Target.Column = 3 Then
If oldVal = "" Then
'do nothing
Else
If newVal = "" Then
'do nothing
Else
Target.Value = oldVal _
& ", " & newVal
End If
End If
End If
End If

exitHandler:
Application.EnableEvents = True
End Sub

Unfortunately, I really know almost (in fact, maybe exactly) nothing about
VBA. I DO know that I have a workbook of 10-odd sheets, each one of which has
three to four columns which have drop-down validations which I need to apply
this macro to. I know how to change the column the macro refers to (changing
"3" in the line "If Target.Column = 3 if" to the correct column number), but
I don't know how to make it so thiat macro works for multiple columns in each
sheet (say, Columns 4, 8, 10, and 18) all the time.

What is the best way to do this? I know this part of the newsgroup is for
users who know what they're doing, but any help you can provide to a total
neophyte would be much appreciated!

Desperate in Columbus



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Drop-Down Macro

Thanks for your suggestion! It looks like that will work! interestingly, I
received other suggestions from some other folks not on the newsgroup, and
they all seem to work. Definitely a "more than one way to skin a cat" sort of
moment.

"Bernie Deitrick" wrote:

Michael,

Try the version below.

HTH,
Bernie
MS Excel MVP


Private Sub Worksheet_Change(ByVal Target As Range)
Dim oldVal As String
Dim newVal As String
Dim myType As Variant

If Target.Count 1 Then Exit Sub

'Make it only work on columns 4, 8, 10, and 18
If Target.Column < 4 And _
Target.Column < 8 And _
Target.Column < 10 And _
Target.Column < 18 Then Exit Sub

On Error GoTo exitHandler
myType = Target.Validation.Type

Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If oldVal = "" Then
'do nothing
Else
If newVal = "" Then
'do nothing
Else
Target.Value = oldVal _
& ", " & newVal
End If
End If

exitHandler:
Application.EnableEvents = True
End Sub


"Michael Link" wrote in message
...
I found the following macro during some internet scrounging. If you have a
drop-down data-validation list in Column C, it builds an accumulative list in
a cell from items selected from the list. (IE, if my validation list is of
Jeeves, Wooster, and Tuppy, each time I select Jeeves from the drop down, it
will add it to the cell with a comma: "Jeeves, Jeeves, Jeeves, Jeeves,
Jeevesr":

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
If Target.Count 1 Then GoTo exitHandler

On Error Resume Next
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler

If rngDV Is Nothing Then GoTo exitHandler

If Intersect(Target, rngDV) Is Nothing Then
'do nothing
Else
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If Target.Column = 3 Then
If oldVal = "" Then
'do nothing
Else
If newVal = "" Then
'do nothing
Else
Target.Value = oldVal _
& ", " & newVal
End If
End If
End If
End If

exitHandler:
Application.EnableEvents = True
End Sub

Unfortunately, I really know almost (in fact, maybe exactly) nothing about
VBA. I DO know that I have a workbook of 10-odd sheets, each one of which has
three to four columns which have drop-down validations which I need to apply
this macro to. I know how to change the column the macro refers to (changing
"3" in the line "If Target.Column = 3 if" to the correct column number), but
I don't know how to make it so thiat macro works for multiple columns in each
sheet (say, Columns 4, 8, 10, and 18) all the time.

What is the best way to do this? I know this part of the newsgroup is for
users who know what they're doing, but any help you can provide to a total
neophyte would be much appreciated!

Desperate in Columbus




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro with Drop Down Box Maggie Excel Discussion (Misc queries) 1 February 20th 07 10:00 PM
Run macro from drop list Jeze77 Excel Discussion (Misc queries) 0 April 6th 06 05:23 PM
Drop Down Box & Macro A.S. Excel Discussion (Misc queries) 3 January 24th 06 04:21 PM
Macro Drop Down Box chalky[_6_] Excel Programming 5 January 13th 06 05:19 PM
Macro that changes Drop Down Box peekbo[_5_] Excel Programming 5 July 8th 04 03:59 AM


All times are GMT +1. The time now is 08:07 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"