卸载我的用户表格VBA

我做了一个应该把总收入转换为净收入的function。

在这种情况下,总收入将在小区B1中提供。

该function的工作原理如下:

启动函数并在函数中selectinput,所以在这种情况下,它的B1:= GrossNet(B1)

之后应该popup一个用户表单询问用户付费频率。 (例如每月,每周四次,每周或每天)。在select之后,用户表单应该卸载它不…

如果我改变function分工作…但作为一个function时,它不,当我作为函数使用它不会卸载用户的forms。

该函数的代码如下:

Public Function GrossNet(InputGross As Variant) Dim GrossPay As Variant Dim pay As String SelectPayFreq.Show pay = Sheets("xx").Range("AT50").Value Range("AT50").Cells.ClearContents GrossPay = InputGross 'Checks whether the provided input is numerical. Dim CountGPay As Integer For CountGPay = 1 To Len(GrossPay) If Not IsNumeric(Mid(GrossPay, CountGPay, 1)) Then MsgBox ("The provided gross pay is not numeric!") Exit Function End If Next 'Converts the pay basis to a monthly basis. eg weekly -> monthly, daily -> monthly etc. If pay = "Daily" Then GrossPay = GrossPay * 5 * 52 / 12 ElseIf pay = "Weekly" Then GrossPay = GrossPay * 52 / 12 ElseIf pay = "4Weeks" Then GrossPay = 52 * GrossPay / 48 Else End If 'Converts the provided gross pay to net pay with the use of the inverse of the lineair regression used @sub netgross. 'R^2 = 0.9995 so the modal fits the data almost perfectly. Dim NetPay As Double If GrossPay <= 1633 Then NetPay = (GrossPay + 61.23) / 1.20326 ElseIf 4180 >= GrossPay And GrossPay > 1633 Then NetPay = (GrossPay + 895.36) / 1.7958 ElseIf 4800 >= GrossPay And GrossPay > 4180 Then NetPay = (GrossPay + 1278.6) / 1.9325 ElseIf 8250 >= GrossPay And GrossPay > 4800 Then NetPay = (GrossPay + 2338.7) / 2.2694 Else NetPay = (GrossPay + 1454.2) / 2.0833 End If GrossNet = NetPay End Function 

第4行中的命令:SelectPayFreq.Show使用以下function加载下列用户窗体: 在这里input图像说明

用户表单后面的代码是:

 Private Sub DailyBut_Click() Sheets("xx").Range("AT50").Value = "Daily" Unload Me End Sub Private Sub FWeekBut_Click() Sheets("xx").Range("AT50").Value = "4Weeks" Unload Me End Sub Private Sub MonthlyBut_Click() Sheets("xx").Range("AT50").Value = "Monthly" Unload Me End Sub Private Sub WeeklyBut_Click() Sheets("xx").Range("AT50").Value = "Weekly" Unload Me End Sub 

有没有人可以帮我一下吗?

顺便说一句,我分配AT50的单元格中的值的原因是将variables从用户窗体转移到该函数。 可能有一种方法直接获得价值,而不使用单元AT50?

您需要在代码中更改两件事情:

  • Frequency应该从用户表单直接传递给函数(而不是写在工作表中),

  • 用户表单应该由主函数卸载而不是卸载自己。

这里是你的代码修改如上所述:

[用户表格代码]

 Option Explicit Public Frequency As String Private Sub DailyBut_Click() Frequency = "Daily" Call Me.Hide End Sub Private Sub FWeekBut_Click() Frequency = "4Weeks" Call Me.Hide End Sub Private Sub MonthlyBut_Click() Frequency = "Monthly" Call Me.Hide End Sub Private Sub WeeklyBut_Click() Frequency = "Weekly" Call Me.Hide End Sub 

[function代码]

 Option Explicit Public Function GrossNet(InputGross As Variant) Dim GrossPay As Variant Dim pay As String Dim freq As String With SelectPayFreq .Show freq = .Frequency End With Unload SelectPayFreq pay = Sheets("xx").Range("AT50").Value Range("AT50").Cells.ClearContents GrossPay = InputGross 'Checks whether the provided input is numerical. Dim CountGPay As Integer For CountGPay = 1 To Len(GrossPay) If Not IsNumeric(Mid(GrossPay, CountGPay, 1)) Then MsgBox ("The provided gross pay is not numeric!") Exit Function End If Next 'Converts the pay basis to a monthly basis. eg weekly -> monthly, daily -> monthly etc. If pay = "Daily" Then GrossPay = GrossPay * 5 * 52 / 12 ElseIf pay = "Weekly" Then GrossPay = GrossPay * 52 / 12 ElseIf pay = "4Weeks" Then GrossPay = 52 * GrossPay / 48 Else End If 'Converts the provided gross pay to net pay with the use of the inverse of the lineair regression used @sub netgross. 'R^2 = 0.9995 so the modal fits the data almost perfectly. Dim NetPay As Double If GrossPay <= 1633 Then NetPay = (GrossPay + 61.23) / 1.20326 ElseIf 4180 >= GrossPay And GrossPay > 1633 Then NetPay = (GrossPay + 895.36) / 1.7958 ElseIf 4800 >= GrossPay And GrossPay > 4180 Then NetPay = (GrossPay + 1278.6) / 1.9325 ElseIf 8250 >= GrossPay And GrossPay > 4800 Then NetPay = (GrossPay + 2338.7) / 2.2694 Else NetPay = (GrossPay + 1454.2) / 2.0833 End If GrossNet = NetPay End Function 

GD Maurice,

请注意, Me.Hide会隐藏你的表单

但…

  1. 您可以直接将payvariables保存到您的函数中,而无需将其写入表单。

创build一个你复制你的“GrossNet”function的模块。

在新的代码模块的顶部声明如下:

Public pay as string

这将使payvariables,所有代码可访问,如果你把它叫做Module1.pay(或者你给模块的任何名字)。 然后,您可以直接将button_click链接到payvariables,而无需将其写入表单。

**编辑**你将不得不删除

尽pipe在你的函数的第2行Dim pay As String ,以避免重新分配(并因此清除)已经声明的payvariables。

 Private Sub WeeklyBut_Click() Module1.pay = "Weekly" Me.Hide 'this hides the form Unload Me 'this unloads it from memory End Sub 
  1. IsNumeric(<value>)是一个variables的函数调用,用来检查variables是否为数值。 即

     If Not IsNumeric(Mid(GrossPay, CountGPay, 1)) Then MsgBox ("The provided gross pay is not numeric!") Exit Function End If 

足以检查它是否是数值,不需要For..to..loop

尝试实施上面的一些技巧,这应该会帮助你一点点?

如果您还有其他问题,请告诉我们

干杯