Search button with the ability to find, edit and add value +VBA

Status
Not open for further replies.

kar2rost

Banned user
Joined
Jul 6, 2022
Messages
20
Office Version
  1. 2019
Platform
  1. Windows
Hello, dear friends
I want to write VBA code
which is as follows
1- After clicking on it, a window will open and ask "Enter the number"
2- After entering the number, search in column A of sheet 1 and select it
and show the value of the cell in front of it in column B in the popup that has the message "close" and "edit"
3- After clicking on edit, it will show me a list from which I can choose (names of people)
To edit and record the corresponding value in column B whose corresponding value is searched in column A
4- If there is no first search value, it will give a message to "close" the window or "create" it and say "Do you want to add?"
5- After clicking on "Yes", it will add the same number to the end of column A and show the same list again so that I can choose the new created value from among them in column B
6- I want the sheet to be locked so that the rest of the cells cannot get any value except column A and column B
7-It should be mentioned that the sheet A has a numerical value and the sheet B has text value
 
Last edited:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
SAMPLE DATA
 

Attachments

  • Untitled.jpg
    Untitled.jpg
    29.6 KB · Views: 7
Upvote 0
i write this code and it is work
VBA Code:
Option Explicit

' Function to search for a value in column A of Sheet1 and display the corresponding value in column B.
Sub SearchAndDisplay()
    Dim searchNumber
    searchNumber = InputBox("Enter the number")
    
    ' Check if a value is entered
    If searchNumber = "" Then
        Exit Sub
    End If
    
    Dim ws
    Set ws = ThisWorkbook.Sheets("Sheet1")
    
    Dim lastRow
    lastRow = ws.Cells(ws.Rows.Count, "A").End(-4162).Row
    
    Dim foundCell
    Set foundCell = ws.range("A1:A" & lastRow).Find(searchNumber, LookIn:=xlValues, LookAt:=xlWhole)
    
    If Not foundCell Is Nothing Then
        Dim result
        result = MsgBox("Value found: " & foundCell.Offset(0, 1).value & vbCrLf & "Do you want to edit?", vbYesNo)
        
        If result = vbYes Then
            EditValue foundCell
        End If
    Else
        Dim addNew
        addNew = MsgBox("Value not found. Do you want to add it?", vbYesNo)
        
        If addNew = vbYes Then
            ' Check if the value is a duplicate
            If IsDuplicateValue(searchNumber, ws) Then
                MsgBox "Duplicate value found. Cannot add."
            Else
                AddNewValue searchNumber
            End If
        End If
    End If
End Sub

' Function to edit the value in column B for a given cell.
Sub EditValue(cell)
    Dim name
    name = InputBox("Choose a name")
    
    cell.Offset(0, 1).value = name
End Sub

' Function to add a new value to column A and edit the corresponding value in column B.
Sub AddNewValue(number)
    Dim ws
    Set ws = ThisWorkbook.Sheets("Sheet1")
    
    Dim lastRow
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
    ws.Cells(lastRow + 1, "A").value = number
    
    EditValue ws.Cells(lastRow + 1, "A")
End Sub

' Function to check if a value is a duplicate in column A.
Function IsDuplicateValue(value, ws) As Boolean
    Dim lastRow
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
    Dim rng As range
    Set rng = ws.range("A1:A" & lastRow)
    
    Dim cell As range
    For Each cell In rng
        If cell.value = value Then
            IsDuplicateValue = True
            Exit Function
        End If
    Next cell
    
    IsDuplicateValue = False
End Function

I have a form
I want to edit and add a new value in column B
Open the form and use the form buttons as initialization
Can anyone help?
 
Upvote 0
Yet again you have cross-posted without links despite numerous warnings.
Account banned.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,224,975
Messages
6,182,112
Members
453,089
Latest member
boonga

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