简体   繁体   中英

Excel Macro for data validation modification

In cell A5, a list validation is populated ( Cat A, Cat B and Cat C). When the user choose category A, cell C5 is populated by a formula that tells the user to fill in the cell. Once the user goes to Cell C5, there is no formula but a drop down or a list validation of (yes or no).

If the user then goes back to cell A5 and chooses category C, cell C5 will be populated the original formula that cell the user to again the cell. Once the user goes to cell C5, there again no formula but a drop down or a list validation (yes/no)

Is it possible to write a simple macro for this? Anyone please? Thank you so much.

I think I know how but need more thoughts.

This is pretty simple code, but not easy to figure out if you haven't written VBA before. It will require two subroutines. In your VBE double click on the worksheet in the "Project - VBAProject" pane where you want this to live. Mine is being built for workbook "Book1.xls" in the tab "Sheet1"

在此处输入图片说明

We are going to use subroutines based on worksheet events. Events are things that happen on the front end (like selecting a cell) that fire code in VBA.

  1. SelectionChange - This worksheet event fires whenever a user changes cell/range selections. We will use it to detect if the user switched to cell C5

  2. Change - This worksheet event fires when someone Changes a cell's value. We use it to see if A5 was changed

In the code editor pane in your VBE, place the following:

Private Sub Worksheet_Change(ByVal Target As Range)     
    'This subroutine fires when a cell value changes in this worksheet.

    'did someone change something specifically in cell A5?
    If Not Intersect(Target, Sheet1.Range("A5")) Is Nothing Then

        'Is the value A or C?
        If Sheet1.Range("A5").Value = "A" Or Sheet1.Range("A5").Value = "C" Then
            'Remove any data validation for this cell:
            Sheet1.Range("C5").Validation.Delete

            'and change the value of C5 to "Fill in this cell"
            Sheet1.Range("C5").Value = "Fill in this cell"

        End If
    End If              
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    'This subroutine fires when a user selects a different cell or range.
    'So... it fires ALL The time so the next line is super important.

    'Did someone change selection specifically to cell C5?
    If Not Intersect(Target, Sheet1.Range("C5")) Is Nothing Then

        'Is the value currently "Fill in this cell"?
        If Sheet1.Range("C5").Value = "Fill in this cell" Then

            'Empty the cell
            Sheet1.Range("C5").Value = ""

            'Add data validation to some list somewhere
            With Sheet1.Range("C5").Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                xlBetween, Formula1:="=$J$1:$J$4"  'This the range that the list exists in
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = ""
                .ErrorTitle = ""
                .InputMessage = ""
                .ErrorMessage = ""
                .ShowInput = True
                .ShowError = True
            End With
        End If
    End If
End Sub

I've noted the important stuff with comments. You'll probably need to monkey a bit with it to fit your needs. Most notably that cell validation code. Currently the data validation for C5 is set to J1:J4 . Change that to fit your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM