checking validation for all column while pasting a row

Copper Contributor

hi all, 

i need to check validation while pasting a row , for all column having drop down such that value for drop down must be from drop down list else it should give msg for error while allowing other cell values to paste 

can someone help me with this? 

thanks for your help

1 Reply

@nemo7512 

You can achieve this by using a combination of Excel's Data Validation feature and a VBA macro.

Here is a step-by-step guide:

1. Set up Data Validation:

    • Select the range of cells in your worksheet where you have dropdown lists.
    • Go to the Data tab on the Excel ribbon.
    • Click on Data Validation in the Data Tools group.
    • In the Data Validation dialog box, choose List from the Allow dropdown.
    • In the Source field, enter the reference to your dropdown list (e.g., =$A$1:$A$5 if your dropdown list is in cells A1:A5).
    • Click OK to apply the data validation.

2. Write a VBA Macro:

    • Press Alt + F11 to open the Visual Basic for Applications (VBA) editor.
    • Click Insert on the menu and select Module to insert a new module.
    • Copy and paste the following VBA code into the module window:

Vba Code is untested, please backup your file.

Sub PasteWithValidationCheck()
    Dim ws As Worksheet
    Dim rng As Range
    Dim cell As Range
    Dim isValid As Boolean
    
    ' Specify the worksheet and range where you have your dropdown lists
    Set ws = ThisWorkbook.Sheets("Sheet1")
    Set rng = ws.Range("A1:A5") ' Adjust range as needed
    
    ' Check if the pasted values are valid based on data validation
    isValid = True
    For Each cell In rng
        If Not cell.Validation.Value(cell.Value) Then
            isValid = False
            Exit For
        End If
    Next cell
    
    ' If any cell contains an invalid value, display an error message
    If Not isValid Then
        MsgBox "One or more cells contain invalid values!", vbExclamation
        Exit Sub
    End If
    
    ' If all values are valid, proceed with pasting
    On Error Resume Next
    ws.Paste Destination:=ws.Range("A" & ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1)
    On Error GoTo 0
    
    If Err.Number <> 0 Then
        MsgBox "Unable to paste values!", vbExclamation
    End If
End Sub

3. Assign the Macro to a Button:

    • Insert a button on your worksheet from the Developer tab (if Developer tab is not visible, enable it from Excel options).
    • Right-click on the button and choose Assign Macro.
    • Select the PasteWithValidationCheck macro and click OK.

4. Testing:

    • Now, whenever you want to paste a row with dropdowns, click on the button you assigned the macro to.
    • The macro will check if the values to be pasted are valid based on the data validation rules you set up. If any value is invalid, it will display an error message. Otherwise, it will paste the row.

This setup ensures that only rows with valid dropdown values are pasted, preventing invalid data from being entered into your worksheet. Adjust the ranges and sheet names in the code to match your specific workbook.The text, steps and the code were edited with the help of AI.

 

Note: If you use Excel Online (Excel for the web) have limited support for VBA macros, and certain features like assigning macros to buttons or running VBA code directly within the worksheet are not available. Additionally, the ability to interact with the Excel application via VBA is not available in the online versions. In Excel Online, you can use some basic formulas and functionalities, but advanced features like VBA macros are not supported. As a result, achieving the same functionality as described in the solution may not be possible directly in Excel Online. You need a desktop version to integrate the macro into the file and then use it in Excel Online (possibly with restrictions).

 

My answers are voluntary and without guarantee!

 

Hope this will help you.

Was the answer useful? Mark as best response and Like it!

This will help all forum participants.