VBA to change data validation selection when closing the file

MMEmt

New Member
Joined
Dec 27, 2022
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Mostly newb at coding, but can follow it ok.
I've looked for, and probably overlooked, the answer to this. When the workbook closes, one worksheet needs to have the values of some data validation dropdowns set back to either blank, or to the first selection in the list. Having tried multiple versions of different code that others posted, success has still eluded me.

It should fit in the code below. Right now I have it in a stand alone Sub TestMe for, ironically, testing. Eventually it will run when the workbook is closed.

VBA Code:
Sub TestMe()

'need to reset "Drop Down 7", "Drop Down 8", and "Drop Down 9" on worksheet named "Trip Sheet Generator" to either blank or default values from data validation on Other_Data and Car List worksheets
 'enter code here
   
'This section deletes Output to Print worksheet
For Each ws In ThisWorkbook.Worksheets
    If ws.Name = "Output to Print" Then
        Application.DisplayAlerts = False
        Sheets("Output to Print").Delete
        Application.DisplayAlerts = True
    End If
Next
'This forces a save on the workbook

ThisWorkbook.Save
MsgBox "This Workbook is Saved"
   
   
End Sub

Can anybody point me in the right direction? It is much appreciated!
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
There's a number of ways you could do this. If, for example, your "Drop Down 7", "Drop Down 8", and "Drop Down 9" are references to specific cells (let's say cells B2, D2 and F2) on the "Trip Sheet Generator" sheet, and you wanted to leave them blank on workbook close, you could use this:

VBA Code:
Sheets("Trip Sheet Generator").Range("B2,D2,F2") = ""

Alternatively, if you wanted to leave them with the default value on closing, and the data validation lists happened to be named ranges (let's say "NamedRange" for the sake of this demo), then you could use something like this:

VBA Code:
Sheets("Trip Sheet Generator").Range("B2") = _
Sheets("Other_Data").Range("NamedRange").Cells(1)

We really need more information to assist you further, but hopefully, this has pointed you in the right direction. For future reference, look at using the XL2BB add in to provide us with a sample of your sheet to work with.
 
Upvote 0
Thank you for your response. I got the XL2BB set up (and learned about Trust Center and Add-in Options along the way). I hope that this attempt turns out well and gives you everything you need.

Oh, I have not used named as ranges as I haven't learned to use them, but if needed to accomplish this task I certainly will.

Here is the Trip Sheet Generator Sheet. I do not believe that I have the dropdowns associated to the cells themselves. I don't know how to do that, and when I run the code you provided (Sheets("Trip Sheet Generator").Range("B2,D2,F2") = ""), the dropdowns remain with the last selection still shows.

Trip Sheets Project Demo.xlsm
BCDEFGHI
1MME Trip Sheet Generator
2
3Select the vehicle to generate the Trip Sheet for:Select the Month:Select the Year:Instructions:
41Add/Remove names as needed on the Location-People page
52Add/Remove vehicles as needed on Car List
63Go to Trip Sheet Generator
74Select Vehicle from Dropdown
85Select the Month
96Select the Year
107Click the Macro Button
118Review
129Print number of copies needed
1310Repeat as necessary
1411Save
15
16
17
18
19
20
21
22
23
24
Trip Sheet Generator


Here is the Location-People sheet:
Trip Sheets Project Demo.xlsm
AB
1ABCDHermione Granger.
2Elizabeth Bennet.
3Matilda Wormwood.
4Atticus Finch.
5Katniss Everdeen.
6Jane Eyre.
7Gandalf
8Eowyn from The Lord of the Rings
9
10EFGHJay Gatsby
11Holden Caulfield
12Humbert Humbert
13Leopold Bloom
14Rabbit Angstrom
Location-People


Car List Sheet:
Trip Sheets Project Demo.xlsm
A
12002 CHEVY BUS @ 9TH AVE - POLSON #1
22004 Silver DodgeTruck @ PWAC/NT #2
32010 DODGE CARAVAN @ MVGH #3
42004 BLUE DODGE TRUCK @ RAC/TBN #4
52020 FORD BUS @ OVGH - POLSON #5
61997 Ford Bus @
71994 BUICK WAGON @ PWAC #7
82005 Chevy Uplander (Medical) @ OVGH #8
92007 CHRYSLER @ PWAC - #9
102009 Ford Focus @ PWAC #10
112011 HONDA PILOT @ POLSON/PWAC TRANSPORTATION #11
121991 Buick Regal @ PWAC - #12
132021 Toyota Sienna @ ADMIN #13
142018 Toyota RAV4 @ Admin #14
152004 Dodge Caravan @ BSGH #15
162006 Dodge Van (Medical) @ BSGH - #16
172006 Chrysler @ MVGH - RONAN #17
182012 Chrysler 200 @ PWAC/LEAD's VEHICLE #18
192015 Ford Bus @ BSGH - #19
202017 Toyota Sienna @ MVGH - #20
212016 DODGE GRAND CARAVAN @ ROSR Ronan - #21
222017 DODGE VAN W-SIDE RAMP @ ADMIN / NURSE/ MEDICAL - #22
232016 Suburu Outback @ PWAC - #23
Car List


Other_Data Sheet:
Trip Sheets Project Demo.xlsm
ABCDEFGHIJKLMNOPQRST
1January2022ODOMETERTEST COMPANY
2February2023AT BEGINNINGDRIVER'S TRIP RECORD
3March2024
4April
5MayODOMETER
6JuneAT END
7July
8August
9SeptemberDATE
10October
11NovemberINITIALS
12December
13DEPARTURE
14TIME
15ARRIVAL
16TIME
Other_Data


This is the macro that is ran when clicking on the Generate Output to Print File button:
VBA Code:
Sub OutputToFile()
Dim ws As Worksheet
Dim dd As DropDown
Dim DDVal As String
Dim iRange, iCells As Range
Dim i As Long


'delete and make new OutputToPrint sheet
For Each ws In ThisWorkbook.Worksheets
    If ws.Name = "Output to Print" Then
        Application.DisplayAlerts = False
        Sheets("Output to Print").Delete
        Application.DisplayAlerts = True
    End If
Next

Sheets.Add(After:=Sheets("Trip Sheet Generator")).Name = "Output to Print"
Worksheets("Output to Print").Activate
Windows(1).DisplayGridlines = False


'Copy corporate info

 Sheets("Other_Data").Select
    Range("T1:T2").Select
    Selection.Copy
    Sheets("Output to Print").Select
    Range("B1").Select
    ActiveSheet.Paste

'Copy Vehicle info and paste into Output to Print

Set dd = Sheets("Trip Sheet Generator").DropDowns("Drop Down 7")
DDVal = dd.List(dd.ListIndex)
ThisWorkbook.Sheets("Output to Print").Range("C78").Value = DDVal

'Copy Month and paste into Output to Print

Set dd = Sheets("Trip Sheet Generator").DropDowns("Drop Down 8")
DDVal = dd.List(dd.ListIndex)
ThisWorkbook.Sheets("Output to Print").Range("H2").Value = DDVal

'Copy year and paste into Output to Print
Set dd = Sheets("Trip Sheet Generator").DropDowns("Drop Down 9")
DDVal = dd.List(dd.ListIndex)
ThisWorkbook.Sheets("Output to Print").Range("I2").Value = DDVal


'Copy and paste Odometer, etc info
   Sheets("Other_Data").Select
    Range("H1:R16").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Output to Print").Select
    Range("C62").Select
    ActiveSheet.Paste
    
'Copy and paste Locations and Names

  Sheets("Location-People").Select
    Range("A1:B58").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Output to Print").Select
    Range("B4").Select
    ActiveSheet.Paste
    
'Format Sheets

    'Corporate and data
    Sheets("Output to Print").Select
    With Range("B1:B2").Font
        .Name = "Arial"
        .Size = 10
        .FontStyle = "Bold"
    End With
    
        With Range("H2:I2").Font
        .Name = "Arial"
        .Size = 10
        .FontStyle = "Bold"
    End With

    'Names
    
    'Sheets("Output to Print").Select
    With Range("C4:C61").Font
        .Name = "Calibri Light"
        .Size = 9
        .FontStyle = "Bold"
    End With

    'Odometer formatting
   ' Sheets("Output to Print").Select
    With Range("C62:C77").Font
        .Name = "Albertus Extra Bold"
        .Size = 8
    End With

    'Autofit columns
    'Sheets("Output to Print").Select
    Worksheets("Output to Print").Columns("C:C").AutoFit
    
    'Merge cells for car and format
    'Sheets("Output to Print").Select
    Range("C78:M78").Select
    
    With Selection
        .Interior.ColorIndex = 6
        .HorizontalAlignment = xlCenter
        .MergeCells = True
        .Name = "Arial"
        With .Font
            .Size = 14
            .FontStyle = "Bold"
            .Underline = xlUnderlineStyleSingle
        End With
        
    End With
    
    'Format cell borders
    'Whole grid goes from B4 to M61
    'Sheets("Output to Print").Select
    Set iRange = Range("C4:M60")
    For Each iCells In iRange
        iCells.BorderAround _
            LineStyle:=xlContinuous, _
            Weight:=xlThin
        Next iCells
    'Now do the same for D3 through M3
    'Sheets("Output to Print").Select
    Set iRange = Range("D3:M3")
    For Each iCells In iRange
        iCells.BorderAround _
            LineStyle:=xlContinuous, _
            Weight:=xlThin
        Next iCells
        
                            
' Find blank spaces and do a thick underline
   ' Sheets("Output to Print").Select
    Set iRange = Range("B2:C60")
    For Each iCells In iRange
    If IsEmpty(iCells) Then
        'if the next row down is not empty then
        If Not IsEmpty(iCells.Offset(1, 0)) Then
        For i = 0 To 10
          iCells.Offset(0, i).Borders(xlEdgeBottom).Weight = xlThick
        Next i
        End If
    End If
        Next iCells

End Sub
And again, here is the macro I am trying to work on to reset the dropdowns back to empty
VBA Code:
Sub TestMe()

Dim ws As Worksheet
 
  
    Sheets("Trip Sheet Generator").Range("B2,D2,F2") = ""
    
    For Each ws In ThisWorkbook.Worksheets
    If ws.Name = "Output to Print" Then
        Application.DisplayAlerts = False
        Sheets("Output to Print").Delete
        Application.DisplayAlerts = True
    End If
Next
    'ThisWorkbook.Save
    'MsgBox "This Workbook is Saved"
    
    

    
End Sub
 
Upvote 0
As I can't edit my own posts here are 3 things:
1) The fields to be cleared are "C5,E5,G5", not the "B2",etc, ones I had in the code.
2) When doing the XL2BB it doesn't seem to capture the buttons and dropdowns, so here is the screenshot of that:
1672256169233.png
 

Attachments

  • 1672256120742.png
    1672256120742.png
    29.2 KB · Views: 4
Upvote 0
Let's take this one step at a time. Try changing this line:
Sheets("Trip Sheet Generator").Range("B2,D2,F2") = ""

To this:
VBA Code:
Sheets("Trip Sheet Generator").Range("C5,E5,G5") = ""
 
Upvote 0
Solution
Thanks again for assisting. I made that change and it still doesn't clear the dropdowns. (A few minutes pass....)

Ah, I just looked carefully and realized that the dropdown are "floating" over those cells. Somehow I made the data validation/dropdown outside of the cells, rather than in them. It works as it should with your code.

Thank you, thank you!
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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