ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

¨D±Ð¦³ÃöUserForm¨Ï¥Î§¹«á¤£¦û°O¾ÐÅ骺²M°£¤èªk

¨D±Ð¦³ÃöUserForm¨Ï¥Î§¹«á¤£¦û°O¾ÐÅ骺²M°£¤èªk

¦U¦ì«e½ú¡A¤£¦n·N«ä¡A¤S­n¨Óµo°Ý¤F
°²¦p§Ú¦Û¤v§@¤F¤@­ÓUserForm1¦Ó¡A­n¶}±Òªº¥L¤èªk¤S¦³¤GºØ
¤èªk¤@¡G
Sub Test_1()

    Dim AForm As UserForm1

    Set AForm = New UserForm1
   
    With AForm
        
        .Show
   
    End With
   
    Unload AForm
    Set AForm = Nothing
   
End Sub

¤èªk¤G
Sub Test_2()

    UserForm1.Show
   
    Unload UserForm1

End Sub

·Q½Ð±Ð¦U¦ì«e½úªº¬O¡G
¤@¡B¦b¤èªk¤@¤ºUnload AForm¡A³o®Éµøµ¡·|®ø¥¢¡A¦ý¬Oºâ¬O³s¦û¥Îªº°O¾ÐÅé³£²M±¼¤F¶Ü¡HÁÙ¬O­n¦A°õ¦æSet AForm = Nothing¤~·|²M±¼°O¾ÐÅé©O¡H
¤G¡B¥t¥~§Ú­n¬O¨S¦³°õ¦æUnload AForm¡A¥u°õ¦æSet AForm = Nothing¡A¦ü¥Gµøµ¡ÁÙ¦b¡A¦ý¬O²z½×¤W¨Ó»¡AForm¦û¥Îªº°O¾ÐÅéÀ³¸Ó
³Q²M±¼¤~¹ï
¤T¡B³o¤GºØ¤èªk¡A¦³¤°»òÀu¯ÊÂI¤§¤À§O¶Ü¡H
clio

¥»©«³Ì«á¥Ñ luhpro ©ó 2016-3-26 00:21 ½s¿è

¦^´_ 1# clio
¥H¤U¬O Excel VBA ªº»¡©ú :
¤@¯ë·í¨Ï¥Î Set ¨Ó¤Þ¥Îª«¥óµ¹ÅܼƮɡA
¨Ã«D½Æ»s¤@¥÷ª«¥óµ¹ÅܼơA
¦Ó¬O«Ø¥ß¤@¥÷¹ï¸Óª«¥óªº¤Þ¥Î
¡C (µù : ³o¸Ì©Ò¿×ªº¤Þ¥Î, ³Ì²³æªº¨Ò¤l´N¬OExcel¬¡­¶Ã¯¤¤¬Y­ÓÀx¦s®æªº¦ì§}, Âǥѳo­Ó¦ì§}¥i¥H¤Þ¥Î¨ì<¨ú±o>¦¹Àx¦s®æªº¤º®e,¦r«¬...)
¶W¹L¤@­Ó¥H¤Wªºª«¥óÅܼƥi¥H¤Þ¥Î¬Û¦Pªºª«¥ó¡C
¦]¬°ÅܼƬO¤Þ¥Î¸Óª«¥ó¡A
¦Ó¤£¬O¤Þ¥Î¸Óª«¥óªº½Æ»s«~¡A
¸Óª«¥ó­Y§ïÅÜ¡A
«h©Ò¦³¤Þ¥Î¥¦ªºÅܼƤ]·|¸òµÛ§ïÅÜ¡C
µM¦Ó¡A­Y¦b Set ³¯­z¦¡¤W¨Ï¥Î New ÃöÁä¦r¡A
«h·|¯uªº«Ø¥ß¸Óª«¥óªº°õ¦æ­ÓÅé¡C (µù : ÁöµM«Ø¥ß¤F ¤@­Óª«¥ó, ¸ÓÅܼƤ´µMÁÙ¬O¤@¥÷¹ï¸Óª«¥óªº¤Þ¥Î)

©Ò¥H Set AForm = Nothing ¨ä¹ê¥u¬O²MªÅ¸ÓÅÜ¼Æ (¥¢¥h¤Þ¥Î¥\¯à),
§¹¥þ¨S¦³°Ê¨ì¸Óª«¥óªº¥»Åé.

¦Ü©ó Unload  ªº»¡©ú :
±N¤@­Óª«¥ó¦Û°O¾ÐÅ餤²¾°£¡C
ÅU¦W«ä¸q.

¨ä¹ê°£«D¦bµ{¦¡°õ¦æ¹Lµ{¤¤¤~°ÊºA«Ø¥ßª«¥ó,
§_«h·í¶}±ÒÀɮ׫á°O¾ÐÅ餺´N¤w¦s¤F¤@¥÷¸Óª«¥óªº¸ê®Æ¤F.
³Ì²³æªºÅçÃҤ覡 -
¶}±ÒExcel ¦sÀÉ­«¶}¬Ý¬Ý¨Ï¥Îªº°O¾ÐÅéªÅ¶¡¶q,
±µµÛ¥[­ÓUserForm«á¦sÀÉ­«¶}¦A¬Ý¬Ý¨Ï¥ÎªºªÅ¶¡¶q,
·|µo²{§Y¨Ï¨S¥Îµ{¦¡ Load ©Î Show ·Ó¼Ë¨Ï¥ÎªºªÅ¶¡·|¸û¦h.
·íµM­Y Load ©Î Show ¤F«áªÅ¶¡·|¥Î§ó¦h.

TOP

¦^´_ 1# clio
­Ó¤Hı±o unload ¬O¼g¦b form events(¦pclick) ¤º­nÃö³¬¡BÄÀ©ñ¦Û¤v³o­Óªí³æ¥Îªº¡C
¦Ó¤£¬O¹³§A¼g¦b show ¤U­±¡A§A³o¼Ë¼g¥i¯à·|¦³¤£¨}®ÄªG¡C

¨Ò¦p§Aªº¤èªk¤G¡A
¦pªG¦b UserForm_Initialize() ¥[¤@¦æ MsgBox "Hi"¡A§A²q¥¦·|³Q©I¥s´X¦¸?
µª®×¬O¨â¦¸!!
²Ä¤@¦¸¬Oµo¥Í¦b UserForm1.Show¡A¦]¬°«e­±¨S©ú½T¼g¥X Load (³o¬O¤¹³\ªº)¡AVBA¦Û°ÊÀ°§Aload«Ø¥ßForm¡C
µM«á¦]¬°§A«öªí³æªº x Ãö³¬®É¡AUserForm1¤w¸gÄÀ©ñ±¼¤F¡A©Ò¥H·í§A°õ¦æ¨ì Unload UserForm1 ¡AVBA¤S¦Û°ÊÀ°§A«Ø¥ßForm¡A¦AÄÀ©ñ±¼¡C

¦Ó¤èªk¤@¡A¬O§â UserForm1·íª«¥óÃþ§O¥Î
§Úı±o§a¡A set ... = new ...¡A´N¬O¨ú¥N Load §@¥Î¡AUnload AForm§Ú¬O¤£ª¾¹D¦³¨S¦³®ÄªG¡C
ª«¥óªº°O¾ÐÅ餣¥Î¤ÓªÈµ²¡A¦L¶H¤¤ VBA À³¸Ó¬O±Ä¥Î garbage collector ¾÷¨î¡A­pºâ¤Þ¥Î¦¸¼Æ (reference count)¡A·í¤Þ¥Î¦¸¼Æ¬°¹s´N·|§ä®É¾÷¦^¦¬°O¾ÐÅé¤F¡C
¬JµMAForm¬O°Ï°ìÅܼơAÂ÷¶}¨º­ÓSub ´NÄÀ©ñ¤F(¤]´N¬O¤£¤Þ¥Î¤F)¡A³o¸Ì¬Æ¦Ü¥[¤£¥[Set AForm = Nothing³£µL©Ò¿×¡C

§Úªº¸Ü¡A­n¹À«ö¶s¤º°µ§¹¤@¨Ç¨Æª½±µ unload meÃö³¬µøµ¡¡A­n¹À´N³£¤£¥[ unload¡A¤â°Ê«öXÃö³¬µøµ¡¡C
¦³¿ù½Ð«ü¥¿³á~
ªí¹F¤£²M¡BÃD·N¤£©ú½T¡B¨SªþÀɮ׮榡¡B¨S¦³°Q½×°ÝÃDªººA«×~~~~~~¥H¤W·R²ö¯à§U¡C

TOP

New ¥Î¨Ó²£¥Í¦h­Ó¹ê¨Ò

Sub QX9650()

Load UserForm1
UserForm1.Caption = "Form_1"
UserForm1.Show 0
tt = tt + 30: UserForm1.Top = tt
MsgBox "Load Form x1 -> Form count " & UserForms.Count

Load UserForm1
UserForm1.Caption = "Form_2"
UserForm1.Show 0
tt = tt + 30: UserForm1.Top = tt
MsgBox "Load Form x2  -> Form count " & UserForms.Count

Load UserForm1
UserForm1.Caption = "Form_3"
UserForm1.Show 0
tt = tt + 30: UserForm1.Top = tt
MsgBox "Load Form x3  -> Form count " & UserForms.Count

Set F1 = New UserForm1
F1.Caption = "Form_111"
F1.Show 0
tt = tt + 30: F1.Top = tt
MsgBox "New Form x1-> Form count " & UserForms.Count

Set F2 = New UserForm1
F2.Caption = "Form_222"
F2.Show 0
tt = tt + 30: F2.Top = tt
MsgBox "New Form x2 -> Form count " & UserForms.Count

Set F3 = New UserForm1
F3.Caption = "Form_333"
F3.Show 0
tt = tt + 30: F3.Top = tt
MsgBox "New Form x3 -> Form count " & UserForms.Count

End

End Sub

TOP

Set AForm = Nothing¡A¦ü¥Gµøµ¡ÁÙ¦b
¦]¬°µøµ¡ªº¥»Åé VB ÁÙ°½°½ºw«rµÛ¥L
©Ò¥H¤£·|³QÄÀ©ñ

TOP

¦^´_ 2# luhpro

·PÁ«e½úªº¸Ñ»¡¡A³o­Ó¤Ó²M·¡¤F¡AÅý§Ú§ó²M·¡ªº©ú¥Õ¡ASet ªº·N¸q¡A·PÁ±z¡C
clio

TOP

¦^´_ 3# stillfish00

·PÁ«e½úªº«ü¥¿¸ò»¡©ú¡AÅý§Ú§ó²M·¡ªº¤F¸Ñ¤F¤@¨Ç¨Æ¡A§Ú¥»¨Ó¬O¦Ò¼{«Ø¥ß¤@­ÓªÅ¥ÕªºUserForm¡AµM«á¦A¥Î¤@­ÓFunction¨Ó©I¥s¥¦¡A¦Ó©Ò¦³ªº¸ü¤J¸ê®Æ¡A¾e¨ú¦^Âиê®Æ³£¥Ñ³o­ÓFunction¨Ó³B²z¨ú±o¡A¦ÓFunction¤S¥i¥H¶Ç¦^¸ê®Æ¡A©Ò¥H§Ú´N¥i¥H±o¨ì¨Ï¥ÎªÌ¿ï¨úªº¸ê®Æ¬°¦ó¡A¦ÓFunction¤º¸ü¤Jªº¤@¨Ç°Ñ¼Æ¡A¥i¥H¨M©wListBoxªº­È¡A»P¤@¨Ç¿ï¶µ°Ñ¼Æ¡A§Ú¤]¥´ºâÅý¥L¦¨¬°±j¨î¿ï¨ú¡A©Ò¥H·|¨ú®ø✕«öÁ䪺¥\¯à¡A¨Ï¥¦¥¢®Ä¡A¦]¬°¥Ø«e§Ú§ä¤£¨ì§ó¦nªº¤èªk¨ÓÅýUserFormµ²§ô«e¡A§â¨Ï¥ÎªÌ¿ï¨úªº¸ê®Æ¶Ç¥X¥X¨Ó¡A¥Ø«e¥u¯à¥ÎPublicÅܼƤ~¯à¹F¨ì¡A¦ý¬O§Ú¦³¨Ç¤£·Q³]³o¼Ë¦hPublic ÅܼơA©Ò¥H¤~·Q§ï¥Î¦¹¤@¤è¦¡¡C
clio

TOP

¦^´_ 4# jackyq

«e½úªº³o­Ó¨Ò¤l¡A¸ÑÄÀªº«Ü²M·¡»¡¡A«e­±¤T­ÓLoadForm1³£¬O­«·sLoadForm1¡A©Ò¥H²Î­pUserForm2.Count³£¬°1
¦Ó«á¤T­ÓSet F1=New UserForm1¡A«h¬O«Ø¥ß¤F¥t¤@­ÓUserForm1¡A©Ò¥HUserForm2.Count´N·|¦A¥[1
·PÁ«e½úªº¨Ò¤l
clio

TOP

        ÀR«ä¦Û¦b : ¤ß¤¤±`¦sµ½¸Ñ¡B¥]®e¡B·P«ä¡Bª¾¨¬¡B±¤ºÖ¡C
ªð¦^¦Cªí ¤W¤@¥DÃD