Sub,Function或Property错误

时间:2013-04-25 20:48:55

标签: excel excel-vba vba

我正在尝试从Excel文档中加载,排序和显示名称。我一直得到预期的子,函数或属性错误?

Sub load()   'this program purpose is to load , sort and display names from an excel document'
    Dim names(1 To 5) As String
    Dim passNum As Integer, i As Integer, temp As String
    Dim num_names As Long, shift As String

num_names = 4  ' LOADS NAMES FROM EXCEL FILE'
For i = 1 To num_names
    names(i) = Cells(i, 1)
Next i
Call shift
Call display
End Sub

Sub sort_1(passNum As Integer, num_names As Long, i As Integer, temp As String)' SORTS NAMES'
   For passNum = 1 To (num_names - 1)
    For i = 1 To (num_names - passNum)
       If names(i) > names(i + 1) Then
            temp = names(i)
            names(i) = names(i + 1)
            names(i + 1) = temp
       End If
    Next i
Next passNum
End Sub

Sub display(i As Integer, num_names As Long)'DISPLAYS NAME BACK ON EXCEL FILE In COLUMN 2'
   For i = 1 To num_names
    Cells(i, 2) = names(i)
   Next i

End Sub

2 个答案:

答案 0 :(得分:2)

您收到该错误,因为找不到Shift程序

Call shift

此外,您需要传递Sub display的参数,否则您将收到“Argument Not Optional”错误。

例如

Call display(1, 1)
'OR Simply
display 1,1 

答案 1 :(得分:0)

你需要列出所有名字吗?

试试这个......

Sub ListNames()
Dim N As Name

For Each N In ThisWorkbook.Names
    Debug.Print N.RefersTo
Next N

End Sub