I have aproblem with this VBA code within my chnage event. When I cahnge the value in…
- User
I have aproblem with this VBA code within my chnage event. When I cahnge the value in column A, it searches a required folder for certain files. If the file is found, it opens a form and I can proceed with the sheet. However, if the required file is not found, it does not allow the value entered in column A to stay. I need to build into the code below a solution where if the requied file is not found, it still alows my entry into column A to remain. Further down the code there is an ELSE Target.Value = "", should this also be within an If statement.
If Not Intersect(Target, Range("A:A")) Is Nothing And Target.Value <> "" Then Me.Unprotect Password:="edit" Application.EnableEvents = False Range("C" & calcRow).Calculate Range("K" & calcRow).Calculate ' CHECK IF PROVIDER ENTERED HAS A PENDING QUOTE Dim rngPending As Range Dim cell As Range Dim found As Boolean Set rngPending = ThisWorkbook.Worksheets("PENDING QUOTES").Range("B2:B42") If Not Intersect(Target, Me.UsedRange) Is Nothing Then For Each cell In rngPending If cell.Value = Target.Value Then found = True Exit For End If Next cell
codeIf found Then Dim cellValue As String cellValue = ActiveCell.Value Dim UserForm As UserForm4 Set UserForm = New UserForm4 UserForm.InitializeForm cellValue UserForm.Caption = "Pending Quotes for " & Target.Value Dim dict As Object Set dict = CreateObject("Scripting.Dictionary") ' Create a dictionary to store unique values UserForm.fieldValue = Target.Value UserForm.Show Else Target.Value = "" Range("C" & calcRow).Calculate Range("K" & calcRow).Calculate ThisWorkbook.Worksheets("QUOTES").Activate End If End If End IfApplication.EnableEvents = True Me.Protect Password:="edit" CellEmpty:
- Assistant
- User
If the required file is not found, I still want my entry to remain, but I do not need a message. I also need a part within the code that will calculate Range C & K if I clear the entry I made in column A if the value in I of the same row is empty.
- Assistant
- User
Does this work if the required file is not found: Private Sub Worksheet_Change(ByVal Target As Range) Dim calcRow As Long calcRow = Target.row If Target.cells.CountLarge > 1 Then Exit Sub 'only one cell at a time If Not Intersect(Target, Range("A:A")) Is Nothing And Target.Value <> "" Then Me.Unprotect Password:="edit" Application.EnableEvents = False
If Target.Value = "" Then Range("C" & calcRow).Calculate Range("K" & calcRow).Calculate GoTo CellEmpty
' CHECK IF PROVIDER ENTERED HAS A PENDING QUOTE Dim rngPending As Range Dim cell As Range Dim found As Boolean Set rngPending = ThisWorkbook.Worksheets("PENDING QUOTES").Range("B2:B42") 'If Not Intersect(Target, Me.UsedRange) Is Nothing Then For Each cell In rngPending If cell.Value = Target.Value Then found = True Exit For End If Next cell
code - Assistant
- User
Below is th VBA code for my UserForm4. It all works well, But the only issue is when I click the submit button, it pops up a message saying I have selected either 0, or 1, or 2. The nubered position of the item I had selected from a list box. Is there a way for it to pop up the message and show me the actual value of the item I selected.
Public Sub InitializeForm(cellValue As String) ' Reset the ListBox before adding new items ListBox1.Clear ' Populate the ListBox with pending quotes for the given cellValue selectedValue = cellValue Dim cell As Range For Each cell In ThisWorkbook.Worksheets("PENDING QUOTES").Range("B2:B42") If cell.Value = selectedValue Then ListBox1.AddItem cell.Offset(0, -1).Value End If Next cell End Sub
Private Sub CommandButton1_Click() ' Process the selected quote (if any) If ListBox1.ListIndex > -1 Then selectedFile = ListBox1.Value ' Do something with the selected quote MsgBox "You selected: " & ListBox1.ListIndex Call OpenFile End If Unload Me End Sub
- Assistant
To modify your UserForm to show the actual value of the item selected from the ListBox when the submit button is clicked, you can change the message displayed in the
MsgBoxwithin yourCommandButton1_Clicksubroutine. Instead of showing the index of the selected item (which isListBox1.ListIndex), you should show the actual value (which isListBox1.Value).Here's the revised code for the
CommandButton1_Clickevent:vbaPrivate Sub CommandButton1_Click() ' Process the selected quote (if any) If ListBox1.ListIndex > -1 Then selectedFile = ListBox1.Value ' Show the actual value of the selected item instead of the index MsgBox "You selected: " & selectedFile Call OpenFile Else MsgBox "No item selected." End If Unload Me End Sub - User
I have also added a second button to close the form. Is the code used correct: Private Sub CommandButton2_Click() Unload Me End Sub
- Assistant
Yes, the code you provided for closing the form is correct. The
Unload Mestatement will close the UserForm and free up the associated resources. Here’s the code snippet for the second button that you have added:vbaPrivate Sub CommandButton2_Click() Unload Me End SubExplanation:
Unload Me: This command unloads the UserForm from memory, which means that any data or settings within the form will be discarded. If you have any code that needs to run for cleanup or finalization (like saving user inputs or state), it's a good practice to handle that before callingUnload Me.
Additional Suggestions: