Variable with InputBox and several subsets in the module

This is the beginning of my code:

Private FilesPath As String
Private CostCentersPath As String
Private FinalPath As String
Private CurrentName As String
Private CostCenters As Worksheet
Private Final As Workbook
Private Template As Worksheet

Sub ReadySetGo()

FilesPath = "O:\MAP\04_Operational Finance\Accruals\Accruals_Swiss_booked\2017\Month End\10_2017\Advertising\automation\" 'path change ("automation")
CostCentersPath = FilesPath & "CostCenters.xlsx"
CurrentName = InputBox("Please adjust the final file name:", , "ABGR_2017-10_FINAL.xls.xlsx")
FinalPath = FilesPath & CurrentName

Set CostCenters = Workbooks("CostCenters.xlsx").Worksheets("Cost Center Overview")
Set Final = Workbooks(CurrentName)
Set Template = Workbooks("Template.xlsm").Worksheets("Template")


End Sub

Sub ReadySetGo is used only to assign values ​​to variables and is called from other modules of the module. But obviously, with this method, I get an input window that appears every time sub is called. Is there any other way, besides the Workbook.Open event, to pass the CurrentName variable the value to other subsets of the module to avoid multiple InputBoxes?

Thanks Bartek

+4
source share
2 answers

In general, there are many good ways to do what you want, depending on how your application is settled. Probably the easiest way to write:

If CurrentName <> vbNullString Then 
      InputBox("Please adjust the final file name:", , "ABGR_2017-10_FINAL.xls.xlsx")
End if

, -. .


- CurrentName :

If Len(Range("A1")) = 0 Then
    Range("A1") = InputBox("Please ...")    
End if
CurrentName = Range("A1")

, Singleton https://en.wikipedia.org/wiki/Singleton_pattern, , CurrentName . - / vba

+2

Vityata, CurrentName , ,

 Static CurrentName As String
    Static HasName As Boolean
    If Not HasName Then
        CurrentName = InputBox("Please adjust the final file name:", , "ABGR_2017-10_FINAL.xls.xlsx")
        HasName = True
    End If
+1

Source: https://habr.com/ru/post/1689786/


All Articles