VBA Question

NotVeryGoodAtBI

New Member
Joined
Mar 11, 2024
Messages
3
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
Platform
  1. Windows
Hello, I have posted my excel VBA code below, I made it so when I double clicked on cells AP6:AP85 the user would be prompted to choose "Fixed" or "Variable" to use in subroutine get_supplier_rate. But when a user picks Fixed or Variable it replaces the original numeric value that was in cells AP6:AP85. I've tried a stored procedure but couldn't get it to work. Does anyone know what I can do so the user input choice of Fixed or Variable will only be used in the get_supplier_rate function and not replace the numeric values in cells AP6:AP85?



VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim cellRow As Integer
    If Not Intersect(Target, Me.Range("X6:X85")) Is Nothing Then
        cellRow = Target.Row
        Call maturity_simulated_data(Me.Cells(cellRow, 1), Me.Cells(cellRow, 24))
    ElseIf Not Intersect(Target, Me.Range("AP6:AP85")) Is Nothing Then
        cellRow = Target.Row
       
        ' Apply data validation to the cell
        With Me.Range("AP" & cellRow).Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:="Fixed,Variable"
            .IgnoreBlank = True
            .InCellDropdown = True
            .ShowInput = True
            .ShowError = True
        End With
       
        ' Prompt message for the user to make a selection
        MsgBox "Please select rate type from the drop-down menu.", vbInformation
       
        ' Cancel the double click event to avoid unexpected behavior
        Cancel = True
       
    End If
End Sub

' Worksheet change event to handle the selection made from the drop-down menu
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cellRow As Integer
   
    If Not Intersect(Target, Me.Range("AP6:AP85")) Is Nothing Then
        cellRow = Target.Row
        If Target.Value = "Fixed" Then
            Call get_supplier_rate(Me.Cells(cellRow, 1), "Fixed")
            Sheets("Supplier rate").Visible = True
            Sheets("Supplier rate").Select
        ElseIf Target.Value = "Variable" Then
            Call get_supplier_rate(Me.Cells(cellRow, 1), "Variable")
            Sheets("Supplier rate").Visible = True
            Sheets("Supplier rate").Select
        End If
    End If
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Why don't you just pop up an inputbox for them to enter whichever one they want (you could prompt with 1 for Fixed and 2 for Variable to make it easier) rather than changing the cells at all?
 
Upvote 0
Why don't you just pop up an inputbox for them to enter whichever one they want (you could prompt with 1 for Fixed and 2 for Variable to make it easier) rather than changing the cells at all?
Unfortunately that's just what I was tasked with, I originally had an input box where user will input F or V.
 
Upvote 0
I don't think whoever told you to do it this way really thought it through. ;)

You could do something like this:

Code:
' Worksheet change event to handle the selection made from the drop-down menu
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cellRow As Integer
   
    If Not Intersect(Target, Me.Range("AP6:AP85")) Is Nothing Then
        cellRow = Target.Row
        Dim currentValue
        currentValue = Target.Value
        with Application
             .enableevents = False
             .Undo
             .Enableevents = true
        End with
        If currentValue = "Fixed" Or currentValue = "Variable" Then
            Call get_supplier_rate(Me.Cells(cellRow, 1), currentValue)
            Sheets("Supplier rate").Visible = True
            Sheets("Supplier rate").Select
        End If
    End If
End Sub
 
Upvote 1
Solution

Forum statistics

Threads
1,223,734
Messages
6,174,189
Members
452,550
Latest member
southernsquid2

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