myValue from Inputbox carries into next sub

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
834
Hello, how do I ensure myValue when entered in the inputbox in the first sub below, carries through to the section without being asked again?

Many thanks.

Code:
Sub D42_Forecast_Macro()

Dim myValue As Variant


    myValue = InputBox("Enter the railway period, e.g. 1804")

End Sub

Code:
Public Sub Copy_Cells(myValue As String)

    InputBox myValue
    Dim destcell As Range, r As Long
    Dim fileSpec As String, folderPath As String, fileName As String
    
    cellValue1 = Workbooks("Forecast v.s. D42 Statement Template.xlsx").Sheets("Revenue").Range("U6")
      
    fileSpec = "C:\Users\jamesco\OneDrive for Business\PMO - Schedule 4 - Sharing best practice\2. CrossCountry Trains\Revenue\" & myValue & "\" & cellValue1 & ""

End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hello jamescooper,

Declare your variable "myValue" as Public at the top of the Module. It will retain it's value until the "D42_Forecast" is run again.

Code:
Public myValue As Variant


Sub D42_Forecast_Macro()


    myValue = InputBox("Enter the railway period, e.g. 1804")


End Sub


Public Sub Copy_Cells(myValue As String)


    Dim destcell As Range, r As Long
    Dim fileSpec As String, folderPath As String, fileName As String
    
    cellValue1 = Workbooks("Forecast v.s. D42 Statement Template.xlsx").Sheets("Revenue").Range("U6")
      
    fileSpec = "C:\Users\jamesco\OneDrive for Business\PMO - Schedule 4 - Sharing best practice\2. CrossCountry Trains\Revenue\" & myValue & "\" & cellValue1 & ""


End Sub
 
Upvote 0
Hello jamescooper,

Declare your variable "myValue" as Public at the top of the Module. It will retain it's value until the "D42_Forecast" is run again.

Code:
Public myValue As Variant


Sub D42_Forecast_Macro()


    myValue = InputBox("Enter the railway period, e.g. 1804")


End Sub


Public Sub Copy_Cells(myValue As String)


    Dim destcell As Range, r As Long
    Dim fileSpec As String, folderPath As String, fileName As String
    
    cellValue1 = Workbooks("Forecast v.s. D42 Statement Template.xlsx").Sheets("Revenue").Range("U6")
      
    fileSpec = "C:\Users\jamesco\OneDrive for Business\PMO - Schedule 4 - Sharing best practice\2. CrossCountry Trains\Revenue\" & myValue & "\" & cellValue1 & ""


End Sub

Thanks when I call my 2nd sub,

Call Copy_Cells

It doesn't seem to do anything, do I still have to put

Dim myvalue as Variant in both modules?

Thanks.
 
Upvote 0
Hello jamescooper,

Sorry about that, I should have mentioned that point in my previous post. Excellent troubleshooting! Glad I could help.
 
Upvote 0
All you need to do is remove this from the original code.
Code:
    InputBox myValue
 
Upvote 0

Forum statistics

Threads
1,215,972
Messages
6,128,030
Members
449,414
Latest member
sameri

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