View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
davegb davegb is offline
external usenet poster
 
Posts: 573
Default running program from userform


Nigel wrote:
Your event code must be on the userform, so when a control is clicked or
changed etc. the private sub will act on these events
from these you can call functions or other subs in external modules that
carry out the steps required.

Private Sub btnCancel_Click
Call mySub ' in another module
myValue = myFunction ' in another module
End Sub

If you wish to act on the control on the userform, then that code could be
placed in the userform or the name of the userform passed to the sub routine
as an object parameter, this method allows code to be reusable for different
forms, something like....

Dim myForm as Object
Private Sub btnCancel_Click
Set myForm = Me.Name
Call mySub (myForm)
End Sub


--
Cheers
Nigel



Thanks for your reply, Nigel. Unfortunately, you didn't answer either
of my questions. Can anyone answer my questions?


"davegb" wrote in message
oups.com...
Some time ago, in a series of threads, I learned that if a program is
always run in conjunction with a userform, the code should all be in
the userform, not in a module. I was too far along with the previous
program to re-write it all to be in the userform.

Now I'm writing another program that will only be run with the userform
(uf1021Mid), so I'm trying to write the code entirely in the userform.
So far, I have 2 problems. One is, how do I initiate a program in the
userform? Normally, when I initiate a program, I call it's name from
the macro menu or from a tool to which it's been assigned. Since the
code in the userform already has names like "Private Sub
btnCancel_Click()", I don't think I can call the program from those
names. So how do I access the macro? Maybe I misunderstood the previous
posts and there always has to be at least the Sub name and a userform
call in a module?

Second problem in testing to see if the refedit in the userform is
returning a range. I'm using the following code, the test code being
from Walkenbach's book:

Private Sub OKButton_Click()


Dim wbExtr As Workbook

Dim wsRef As Worksheet 'wks where Top 10 list is stored
Dim wsExtFrom As Worksheet 'Wks where data is extracted from
Dim wsTop As Worksheet 'wks where new table goes

Dim oWS As Object

Dim rCopy As Range
Dim rCell As Range 'each cell in rRef
Dim rRef As Range 'Range on wsRef where current CtyLst is
Dim rExtrFromStrt As Range
Dim rExtrFrom As Range 'range in Src sheet Where cty names are
Dim rRefRow As Range
Dim rExtrFromEnd As Range
'Dim rColHdr As Range
Dim rHdrStrt As Range
Dim rHdrEnd As Range

Dim s1stCtyName As String
Dim sUCrCell As String
Dim sCtyName As String
Dim sHeader As String

Dim lExtrFromCol As Long 'CtyCol in Src sht
Dim lExtrToCol As Long
Dim lTopRow As Long
Dim lCopyRow As Long
Dim lBOS10Row As Long
Dim lBOS21Row As Long
Dim lStrDif As Long
Dim lMid3Row As Long
Dim lBOS3Row As Long
Dim lRefRow As Long
Dim lExtrFromStrt As Long
Dim lColHdrCount As Long
Dim lLastCol As Long
Dim lLastRow As Long


Dim bHdr As Boolean

Set wsRef = Workbooks("Mark Top 10.xls").Worksheets("CtyLst")
Set wsExtFrom = ActiveSheet
Set wbExtr = ActiveWorkbook
lTopRow = 2
lBOS10Row = 14
lBOS21Row = 25
lMid3Row = 14
lBOS3Row = 27

'Test is Mark Top 10 workbook is active
If ThisWorkbook.Name = wbExtr.Name Then
MsgBox "You have selected the workbook that contains the macro." &
_
Chr(13) & "Please click Ok and select the correct workbook and " &
_
Chr(13) & "worksheet and restart the macro.", vbOKOnly
Exit Sub

End If



'TEST FOR SHEET NAMED "Top"
For Each oWS In wbExtr.Sheets
If oWS.Name = "Top" Then
If MsgBox("A worksheet named Top already exists in this
workbook." _
& Chr(13) & "Please remove or rename it and run the macro
again.", _
vbOKOnly) = vbOK Then Exit Sub
End If
Next
On Error Resume Next
'Set rColHdr = ActiveSheet.Range(reDataStrt.Text)
Set uf1021Mid.rColHdr = Range(reDataStrt.Text)

If Error < 0 Then
MsgBox "Invalid range selection, please select the starting range
again."
On Error GoTo 0
Exit Sub
End If
uf1021Mid.Hide
End Sub

No matter what I enter in the refedit control, I get the error message.
Any ideas?

Thanks as always.