Userform doesn't want to start the function

jackjack

New Member
Joined
Aug 4, 2021
Messages
5
Office Version
  1. 365
Platform
  1. Windows
so, I have a code that will divide the dataset into multiple sheet based on the user input (SplitRow). Since I also need to take in the brand name, I decided to use it in a userform format. I somehow decided that the code that will split the dataset should be a public function in that userform, which will be called by a module. but somehow the userform does not want to start the function. If I put the code in the OkBtn it will run just fine, but I need to know why the function will not start since I am curious. When I used f*, it just simply stop when I click the Ok button. the function is in Public Function ShowDialog.
below is the code for the userform:
VBA Code:
Private Sub CancelBtn_Click()
    Unload Me
    MsgBox "Thank You."
    End
End Sub

Private Sub OkBtn_Click()
   
    Cancel = False
End Sub
Public Function ShowDialog()

Me.Show

Dim WorkRng As Range
Dim xRow As Range
Dim SplitRow As Integer
Dim xWs As Worksheet
Dim n As Integer
Dim i As Integer
Dim LastRow As Integer
Dim WS As Excel.Worksheet
Dim SaveToDirectory As String
Dim RngFromUF As String
Dim lRow As Long
Dim fCell As String
Dim lCell As String
Dim yaya As String

    
If Not Cancel Then

If UFRng.Enabled = True Then
    RngFromUF = UFRng
    Else
fCell = "A2"
        lRow = Cells.Find(What:="*", _
                    After:=Range("A1"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Row
        lCell = "w" & lRow
        RngFromUF = "" & fCell & ":" & lCell
        End If
        
Set WorkRng = Range("" & RngFromUF)
brand = UFBrand
SplitRow = UFSplitRow

Set xWs = WorkRng.Parent
Set xRow = WorkRng.Rows(1)

wb_name = ActiveSheet.Name

'Copy and setup new sheet

For i = 1 To WorkRng.Rows.Count Step SplitRow
    resizeCount = SplitRow
    If (WorkRng.Rows.Count - xRow.Row + 1) < SplitRow Then resizeCount = _
    WorkRng.Rows.Count - xRow.Row + 2
    xRow.Resize(resizeCount).Copy
    Application.Worksheets.Add After:=Application.Worksheets(Application.Worksheets.Count)
    ActiveSheet.Range("A1:W1").Value = Sheet1.Range("A1:W1").Value
    Application.ActiveSheet.Range("A2").PasteSpecial
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    n = i + LastRow - 1
    ActiveSheet.Name = "" & brand & " " & i & "-" & n
    Set xRow = xRow.Offset(SplitRow)
Next

End If
ShowDialog = Not Cancel
Unload Me

End Function
Private Sub UFSelRng_Click()
If UFSelRng.Value = True Then

UFRng.Enabled = True
UFRng.Visible = True
Else
UFRng.Enabled = False
UFRng.Visible = False
    
End If

End Sub
Private Sub UserForm_Initialize()
    UFRng.Enabled = False
    UFRng.Visible = False
    
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
You really shouldn't show a form from code inside that form since, unless the form is modeless, the routine that shows the form will stop once the form is shown and then not continue until the form is unloading.
 
Upvote 0
So I guess it is better to just put the whole function to Ok Button so that it run when the button is pressed?
 
Upvote 0
Logically it makes more sense to me that the code would run when you press OK.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top