VBA Best Coding Practice: Connecting Named Range from one worksheet to another Named Range on another worksheet

Viewed 29

I am creating a Excel program that automates the creation of a document using files given to it by the user. The Excel program needs to keep a paper trail of all the files selected by the user. The "interface" sheet allows the user to click a button and select the files (from a file picker window) needed to create this document. Various files are required to fill in various data fields of the final document. Each button has a data field name that corresponds to the file that the user is supposed to select. For example, for the "Part Number" button, the user selects the document that contains all the part number information. The Excel has a Checklist sheet that saves all the file selected by the user (aka the paper trail). So, on the Checklist sheet there is a "Part Number" section where the file name, file path, and optional comments are stored (see attached snapshots). Interface User Sheet, Checklist Sheet
What is the best coding implementation to link the data field name (aka Part Number) from the Interface sheet to the corresponding data field on the Checklist sheet? A couple of things to note:

  1. I want all the buttons on the interface sheet to call the same Button_Click sub. The sub needs to identify which data field the button was clicked for. See first code block for button click sub.
  2. In the future, additional data fields might be added to the interface sheet. When this happens additional fields on the checklist sheet will need to be added to correspond to the new data fields on the interface sheet. The sub that finds the corresponding Checklist field should use Named Ranges. Code using Cell address references will need to be updated if new data fields are added. Its much easier to add more Named Ranges to the code. The second code block is my first attempt at linking the interface data field names to the checklist data field cells.

I've removed all the file handling code, as my main concerns are regarding the second code block.

Public Sub Btn_Data_Field_Click()
Dim rng As Range
Dim DataName As String, ChecklistNameRange as string
Dim FileName as String, FilePath as String

Set rng = ActiveSheet.Buttons(Application.Caller).TopLeftCell 'Find button's cell address
DataName = ActiveSheet.Range(r.Address).Offset(0, 2).Name 'Store the data field's Named Range

'Code prompts user for file(s)
'FilePathArray = Function calls FilePicker()
'For Each e in FilePathArray... 'Loop through each file selected.
    'FileName = GetFileName(e)
    'FilePath = GetFilePath(e)
    'GetChecklistNameRange links the interface data field Named Range to the Checklist Named Range
    CheckListNameRange = GetChecklistNameRange(DataName)

    'Add FileName, FilePath, and comments to the correct Named Range on the Checklist sheet.
    Call AddToChecklist(CheckListNameRange, FileName, FilePath, "example text")
'Next e

'more code
End Sub
Public Function GetChecklistNameRange(NameRng As String) As String
Dim str As String
Select Case NameRng
Case "Int_PartNum"   'Interface sheet Named Range for Part Number
    str = "CL_PartNum"  'Checklist sheet Named Range for Part Number
Case "Int_Sequence"
    str = "CL_PartNum"
Case "Int_MassSpec"
    str = "CL_MS"
'Case etc
'    str = ....
'Case etc
'    str = ....
'Case etc
'    str = ....
End Select
GetChecklistNameRange = str
End Function

This second code feels sloppy. And I've read articles stating to never use Select Case like this. I could get ride of all of these issues by just having each button on the interface sheet have it's own Button_Click() sub. This Excel project opens and closes a lot of Excel workbooks (no way around this) so it already runs somewhat slowly. Anything I can do to speed up the code is a big win for me and my team. Lastly, if you have any suggestions on good VBA books that discuss how to build large Excel programs I would gladly appreciate it. I'm a self taught VBA coder (I'm sure it shows) for my company. I typically work on small macros for them, but now I'm getting inquiries for large scale projects. (Any book recommendations for introductory Cloud databasing using VBA would be greatly appreciated too).

Thank you kindly, Qstein

0 Answers
Related