User Form controls still executing after Unload

mikeymay

Well-known Member
Joined
Jan 17, 2006
Messages
1,421
I am using a UF that the user enters details on that need to be appended to a range in a worksheet.

Once the data has been added and pasted into a holding worksheet, the UF is closed via an Unload commande but when some of the non UF code is executing, the code for some controls are executing.

E.g.
Code:
strCategory = rngGoodsOut.Offset(0, 0)
   strPerson = rngGoodsOut.Offset(0, 1)
   dtDateOut = rngGoodsOut.Offset(0, 2)
   strItem = rngGoodsOut.Offset(0, 3)
   lngQty = rngGoodsOut.Offset(0, 4)
All this is in a module and not behind the UF and each of the variables refer to items that were addedin the UF so strPerson was added via a combobox, dtDateOut was added via a textbox.

Now when each of these lines of code execute the combobox control for selecting the person executes 'cbPerson_Change'.

I have no clue as to why this is happening and wondered if anyone else has experienced this odd behavior.
 

Some videos you may like

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,030
Office Version
2013
Platform
Windows
If I read what you are describing correctly, it appears that you initialized variables located in a public code module with values entered via a UF. Those variables do not "unload" when the UF unloads. Only the variables in the UF code module are destroyed when the form unloads. So the varibles in a public code module will hold the value until they are assigned new values or the procedure ends, whichever comes first.
 

mikeymay

Well-known Member
Joined
Jan 17, 2006
Messages
1,421
Ah!!!

So if I am needing to assign a control value to a variable (CbPersoron to strPerson), how do I do that without declaring strPerson as public?
 

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,030
Office Version
2013
Platform
Windows
Ah!!!

So if I am needing to assign a control value to a variable (CbPersoron to strPerson), how do I do that without declaring strPerson as public?
I am not sure what you are asking, but this is how it works.
Variables assigned inside a macro live and die with the macro.
Variable values can be passed from one macro to another and will live in the receiving macro until it completes at which point the varibles die.
Public variables can be used by any macro and their values are determined based on how they are declared, eg. some values of public variables might change during runtime of macros, but constants do not change during runtime of macros.
Values for variables passed from one code module to another must be passed by reference.

Values for controls on a UF can be used in variables in pulbic marcros if the UF is open during the runtime of a public macro,

Don't know how much help this will be, but it is the basic guide as I understand it.
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,101,851
Messages
5,483,306
Members
407,393
Latest member
GeorgeBrown

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top