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:
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