Pass value to a double click procedure

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,368
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
If have a double click procedure that will add some new rows. As you can see the .Resize is hard coded right now, but I would like to grab a number from the user as they input their desired number rows.

How do I pass the value from the Sub back to the double click?

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Cancel = True
    Application.ScreenUpdating = False
    If Target.Column = 1 Then
        On Error Resume Next
        Target.EntireRow.Copy
       
        Range("A" & Target.Row).Offset(1, 0).Resize(12).Insert Shift:=xlDown
        Range("A" & Target.Row).Offset(1, 0).Resize(12).Value = "Add"
    End If
    Application.ScreenUpdating = True
End Sub

VBA Code:
Public Sub MyInputBox()

    Dim msg As String: msg = "Please enter a number in the box below for the number of records you would like to add"
    Dim MyInput As String: MyInput = InputBox(msg, "Adding additional records", "Enter your input number here!")
   
    If MyInput = "Enter your input number here!" Or MyInput = "" Then
      Exit Sub
    End If
   
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi
you normally use a Function to return value to the calling procedure.

see if this update to your codes helps

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim NoRows      As Variant
    
    If Target.Column = 1 Then
        Cancel = True
        NoRows = GetInputBox
        If NoRows <> False Then
            Application.ScreenUpdating = False
            
            Target.EntireRow.Copy
            
            Range("A" & Target.Row).Offset(1, 0).Resize(NoRows).Insert Shift:=xlDown
            Range("A" & Target.Row).Offset(1, 0).Resize(NoRows).Value = "Add"
        End If
    End If
    With Application
        .CutCopyMode = False
        .ScreenUpdating = True
    End With
End Sub

VBA Code:
Function GetInputBox() As Variant
    
    Const strPrompt     As String = "Please enter a number in the box below For the number of records you would Like To add"
    Const strDefault    As String = "Enter your input number here!"
    Const strTitle      As String = "Adding additional records"
    
    Do
        GetInputBox = InputBox(strPrompt, strTitle, strDefault)
        'cancel pressed
        If StrPtr(GetInputBox) = 0 Then GetInputBox = False: Exit Function
    Loop Until Val(GetInputBox) > 0
    
End Function

Dave
 
Upvote 0
Solution

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

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