函数未在userform子例程中正确定义

时间:2017-09-22 17:26:56

标签: excel vba excel-vba userform

如果这是一个愚蠢的问题我很抱歉,但我一直无法找到当前问题的答案。我正在尝试创建一个userform,它根据组合框中的输入搜索电子表格数据,用于用户名,开始日期和结束日期。

有一个用户列,所有其他列对应于日期。数据是每日工作负载生产率数字(例如,87个小部件)。目标是能够输入输入并将原始数据电子表格中的数据复制到结果电子表格,在该日期范围内显示一个用户的信息。

我一直收到错误("编译错误:Sub或Function not defined")关于我尝试使用该函数定义适当日期的列。我很欣赏任何意见。感谢。

'Function changes column number to column name
Public Function fnColumnToLetter_CellAdressReplace(ByVal ColumnNumber As Integer)
fnColumnToLetter_CellAdressReplace = Replace(Replace(Cells(1, ColumnNumber).Address, "1", ""), "$", "")
End Function

Private Sub SearchButton1_Click()
'Dim variables
Dim sheet As Worksheet
Dim name1 As String
Dim date1 As Date
Dim date2 As Date
Dim STARTcol As String
Dim ENDcol As String
Dim pharmcol1 As String
Dim pharmcol2 As String
Set sheet = ActiveWorkbook.Sheets("Data")
Set Results = ActiveWorkbook.Sheets("Results")
'Variables for entries input into userform
name1 = userform1.NameBox1.Value
date1 = userform1.DateBox1.Value
date2 = userform1.DateBox2.Value

'Define row based on pharmacist name
rowno = Application.WorksheetFunction.Match(name1, Range("A:A"), 0)
pharmrow = "a" + rowno

'Find first column from start date
STARTcol = Application.WorksheetFunction.Match(date1, Range("A1:AZZ1"), 0)
'Find last column from end date
ENDcol = Application.WorksheetFunction.Match(date2, Range("A1:AZZ1"), 0)

'Call function to replace column number to column name
pharmcol1 = fnColumnToLetter_CellAddressReplace(STARTcol) + rowno
pharmcol2 = fnColumnToLetter_CellAddressReplace(ENDcol) + rowno

'Copy table array to RESULTS worksheet
sheet.Range("pharmcol1:pharmcol2").Copy Destination = Results.Range("A1")

End Sub

1 个答案:

答案 0 :(得分:0)

编译错误:Sub或Function not defined

每当我看到这个错误时,我通常会在函数/ sub和调用之间寻找语法错误。在你的情况下,你在两者之间拼写了不同的地址。

    Public Function fnColumnToLetter_CellAdressReplace(ByVal ColumnNumber As Integer)

    pharmcol1 = fnColumnToLetter_CellAddressReplace(STARTcol) + rowno
    pharmcol2 = fnColumnToLetter_CellAddressReplace(ENDcol) + rowno

希望这有帮助。