PDA

View Full Version : help with user forms



next
05-09-2012, 06:44 AM
I made a user form, it works well except for one little thing. When I run my code, the form is displayed, I make my selection and click on the button to process data, yet when I click the button the form doesn't disappear by itself, I need to press the exit button manually for it to go away and finish the code. How do I automate thsi process?
Form code:
Private Sub btn_cancel_Click()
Unload Me
End Sub

Private Sub btn_create_Click()
Dim today As Variant
Dim ach As Worksheet, visa As Worksheet, amex As Worksheet
Dim ict As Worksheet, other As Worksheet, sheet As Worksheet

ActiveSheet.Name = "Deposit"

If chk_ach = True Then
Set ach = ActiveWorkbook.Sheets.Add
ach.Name = "ACH Deposits"
ach.Move After:=ActiveWorkbook.Worksheets(ActiveWorkbook.Worksheets.Count)
End If
If chk_visa = True Then
Set visa = ActiveWorkbook.Sheets.Add
visa.Name = "Visa"
visa.Move After:=ActiveWorkbook.Worksheets(ActiveWorkbook.Worksheets.Count)
End If
If chk_amex = True Then
Set amex = ActiveWorkbook.Sheets.Add
amex.Name = "Amex"
amex.Move After:=ActiveWorkbook.Worksheets(ActiveWorkbook.Worksheets.Count)
End If
If chk_ict = True Then
Set ict = ActiveWorkbook.Sheets.Add
ict.Name = "ICTs"
ict.Move After:=ActiveWorkbook.Worksheets(ActiveWorkbook.Worksheets.Count)
End If
If chk_other = True Then
Set other = ActiveWorkbook.Sheets.Add
other.Name = "Other"
other.Move After:=ActiveWorkbook.Worksheets(ActiveWorkbook.Worksheets.Count)
End If
End Sub
Private Sub UserForm_Initialize()
chk_ach = True
End Sub

macro
Sub Format_PayHist()
Dim today As Variant, last_cell As Double, sheet As Worksheet
Dim total_cell As Double
today = Format(Now, "dddd")
last_cell = GetLastCell()

Columns("G:G").Delete shift:=xlLeft
payments_form.Show
For Each sheet In ActiveWorkbook.Sheets
sheet.Range("A1") = "Date"
If today = "Monday" Then
sheet.Range("B1") = Format(DateAdd("d", -3, Now), _
"mm/dd/yy")
Else
sheet.Range("B1") = Format(DateAdd("d", -1, Now), _
"mm/dd/yy")
End If
sheet.Range("C1") = "Cleanway A Division of Cintas"
sheet.Range("A2") = "Acc#"
sheet.Range("B2") = "Loc#"
sheet.Range("C2") = "Inv#"
sheet.Range("D2") = "Amount"
sheet.Range("E2") = "Date"
sheet.Range("F2") = "Chk/Ref"
sheet.Range("G2") = "DiscTaken"
sheet.Range("H2") = "Allowance"
sheet.Range("I2") = "WO#"
sheet.Range("J2") = "ServiceDate"

sheet.Columns("A:K").ColumnWidth = 12
sheet.Columns("D:D").Style = "Currency"
sheet.Columns("G:H").Style = "Currency"
If sheet.Name = "Deposit" Then
total_cell = last_cell + 1
Else
total_cell = 10
End If

With sheet.Range("D" & total_cell)
.FormulaR1C1 = "=SUM(R1C:R[-1]C)"
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeTop).Weight = xlThin
.Borders(xlEdgeBottom).LineStyle = xlDouble
.Borders(xlEdgeBottom).Weight = xlThick
.Font.Bold = True
End With

If sheet.Name = "Deposit" Then
total_cell = last_cell + 1
Else
total_cell = 10
End If
With sheet.Range("G" & total_cell & ":H" & total_cell)
.FormulaR1C1 = "=SUM(R1C:R[-1]C)"
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeTop).Weight = xlThin
.Borders(xlEdgeBottom).LineStyle = xlDouble
.Borders(xlEdgeBottom).Weight = xlThick
.Font.Bold = True
End With

Application.PrintCommunication = False
With sheet.PageSetup
.LeftHeader = "&[Tab]"
.PrintTitleRows = "$1:$2"
.PrintGridlines = True
.PrintQuality = 600
.CenterHorizontally = True
.Orientation = xlPortrait
.FitToPagesWide = 1
.FitToPagesTall = 0
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = True
End With
Application.PrintCommunication = True
Next sheet
'Format(DateAdd("d", -1, Now), "mm-dd-yy")
ActiveWorkbook.SaveAs "xxxxxxxxxxxxxxxxx.xls (file://\\Cwidomain\Accounting\LEVEL2\Deposits)", xlExcel8
End Sub

Tinbendr
05-09-2012, 07:17 AM
Does the Cancel button work? If so, use the Unload Me command, say, at the end of the btn_create_Click event to close the form.