Apologies I’ve I’ve not posted this correctly. I don’t know how to put my vba coding into its own window.
I’ve been relearning to use excel and vba try to automate an excel monthly meal plan for a seniors’ home in which I live. After several months, I’ve accomplished a lot. That said, I’ve been exploring how I might optimize my coding, reduce redundant variable, and restructure the file by expanding the versatility of some subs. To that end, I’ve integrated Debug.Print to help. What an eye-opener!!!!
Just within my first few subs I saw things happening I needed to understand to better design my subs and functions. In truth, I have yet to apply functions, but their structure seems to be more complicated for my aging brain. Two important things at this point I hope can be explained as they could vastly improve the entire structure of my file. I’ve embedded comments in my coding to try to limit the length of my question and provide context
Further context (You might have to review my code and debug printout below. It’s not going well for me trying to post this): A sub uses LdRow (stored in txtcRow) to populate formMealPlan. Pretty much whatever subs follow Build_ddnUnitNo() need the value placed into txtcRow.
Questions:
- Before cleaning everything up for the sake of preparing this for your guidance, the debug.print yielded something along the lines of Build_ddnUnitNo(D112) where those sub were just …. Sub Subname() …. That being the case, how might I better take advantage of what was/is going on here?
- Should a global variable set in Worksheet coding not work for the entire project? (I hope I’m using the correct terminology to be understood.)
- I’ve tried turning a sub into a function but run into problems in knowing just how to set up the variable when their options are so vast. And while I’m pretty adept at resolving whatever my issues are, but this one, for all its efficiencies, could be just to time consuming than just creating the workarounds. At least for me.
Thank you for your time and patience.
` Option Explicit ”” Worksheet Code
Public WksFlag As Boolean
Public WksRow As Integer, LdRow As Integer
Sub Worksheet_BeforeDoubleClick(ByVal WksUn As Range, WksFlag As Boolean)
Subroutine = "SUB: Wks_B4DblClick"
Debug.Print "---> Start " & Subroutine & "(" & WksUn & ", " & WksFlag & ")"
If WksUn.Column = 1 Then 'WksUn for Worksheet UnitNo (D112)
WksRow = WksUn.Row 'These dims should be global/public
LdRow = WksRow – 4 'LdRow for ListDiners in User Form
formMealPlan.txtcRow.Value = LdRow
Load frmMealPlan
frmMealPlan.Show (vbModeless)
End If '''' WksRow, LdRow, WksUn don’t seem to immediately Public
'''' Even if I also declare them as well in formMealPlan. (See included printout)
Debug.Print vbNewLine & "<--- End " & Subroutine & "(" & WksUn & ", " & WksFlag & ")"
End Sub
'''' formMealPlan (userform) Code
Public LdRow As Integer, WksRow As Integer
Sub UserForm_Initialize()
Subroutine = "SUB: UserForm_Initialize"
Debug.Print "---> Start " & Subroutine & "()"
Debug.Print "txtcRow(" & txtcRow & ")" <<<< This is here to display my point
Exit Sub '''' I'll move this below Build_ddnUnitNo for 2nd run
Build_ddnUnitNo '''''Populates the Unit Number drop down list
End Sub
'''''''''' The Debug . Printout ''''''''''
1st Debug.Print run
---> Start SUB: Wks_B4DblClick(D112, False)
---> Start SUB: UserForm_Initialize()
txtcRow()
---> Start Sub: txtcRow_Change()
<--- End Sub: txtcRow_Change() <<< This appears Build_ddnUnitNo sub (see below)
<--- End SUB: Wks_B4DblClick(D112, False)
2nd Debug.Print run
---> Start SUB: Wks_B4DblClick(D112, False)
---> Start SUB: UserForm_Initialize()
txtcRow() <<< If this weren’t blank, I could use it immediately rather than
<<< have to force set it.
---> Start SUB: Build_ddnUnitNo()
<--- End SUB: Build_ddnUnitNo()
---> Start SUB: txtcRow_Change ()
<--- End Sub: txtcRow_Change() <<< I can’t seem to move this up.
`
Bernie Slepkov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.