函数在用户形式子例程中未适当定义



如果这是一个愚蠢的问题,我深表歉意,但我一直找不到我当前问题的答案。我正在尝试创建一个用户形式,该用户形式基于Combobox中的输入搜索电子表格数据,用于用户名,开始日期和结束日期。

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

我一直在遇到错误("编译错误:未定义的函数或函数"),以尝试使用该函数为适当的日期定义列。我感谢任何投入。谢谢。

'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

编译错误:sub或函数未定义

每当我看到此错误时,我通常会在函数/sub和呼叫之间寻找语法错误。在您的情况下,您在两个之间的地址有所不同。

    Public Function fnColumnToLetter_CellAdressReplace(ByVal ColumnNumber As Integer)
    pharmcol1 = fnColumnToLetter_CellAddressReplace(STARTcol) + rowno
    pharmcol2 = fnColumnToLetter_CellAddressReplace(ENDcol) + rowno

希望这会有所帮助。

最新更新