VBA Data Validation List Box

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,113
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I understand that I can have a Data Validation List Box where it runs a sub such as:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("B2")) Is Nothing Then
        Select Case Range("B2")
        
            Case "October": Test
            Case "November": Test
            Case "December": Test
                        
        End Select
    End If
    End Sub

But I'm also noticing that the sub here listed as the "Test" macro has to be placed within Microsoft Excel Objects in the Sheet or ThisWorkbook to function.

Is there anyway this Private Sub can be rewritten to say something like:

Code:
Sub Worksheet_List()

When ...

Worksheets(1).Cell ("B2")Selected

Then ...

    If Not Intersect(Target, Range("B2")) Is Nothing Then
        Select Case Range("B2")
        
            Case "October": Test
            Case "November": Test
            Case "December": Test
                        
        End Select
    End If
    End Sub

Please let me know.

Thank you,
pinaceous
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Look in VBA above the code window for any sheet. If you select Worksheet from the pulldown on the left, you can now see all the options for actions that occur when you do things on your sheet. SelectionChange is a trigger that gets called when you cursor around or click on a cell. Now, how do you test for the cell you need??

The code below tests which cell got selected and reacts. Be careful, because even if you select a range of cells and "B2" is one of them, it will still react, that's why I added the code to not react when there is more than one cell selected.

1602344633085.png


VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim i As Range
  
  If Target.Count > 1 Then Exit Sub
  Set i = Intersect(Target, Range("B2"))
  If Not i Is Nothing Then
    'Go do something
  End If
  
End Sub
 

Attachments

  • 1602344555295.png
    1602344555295.png
    10.2 KB · Views: 4
Upvote 0
Hi Jeffrey,

Okay, thank you for improving upon the code.

But is there any way I can take the private out of the code from the objects sheets and place it into a regular module?

Thanks,
pinaceous
 
Upvote 0
Okay Jeffrey,

I can deal with it being a private sub but how where would I put several macros together in running the "test" in post #1?

How would they be organized?

For example,

If "Test" is structured like:

VBA Code:
Sub Test ()

Call Open

Call DoSomething

Call DoSomethingElse

Call Close

End Sub

Where would I put all of this?

In the same worksheet?

Thanks,
pinaceous
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,539
Members
449,038
Latest member
Guest1337

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