VBA功能无法正确调用

时间:2018-02-21 12:58:55

标签: excel excel-vba excel-2016 vba

我有以下函数找到工作表中的最后一行,并尝试在我的Sub Submit_data()中调用它。子似乎没有识别它,因此消息框返回0.但是,如果我将完全相同的代码放在测试子中,其中没有其他任何东西,它就可以工作。任何人都知道为什么会这样?注意:我的提交数据子更长,所以我刚刚包含了发生错误的第一部分。

Private Function lasrow() As Long
Worksheets("Data - Complete").Select
lasrow = Cells.Find(What:="*", _
After:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
End Function

Sub Submit_data()

Dim lRow As Long
Dim lCol As Long
Dim colName As Long
Dim resetrange As Range
Dim indicator As String
Dim Dire(1 To 6, 1 To 2) As String
Dim i As Integer
Dim lasrow As Long, lasCol As Long
Dim lro As Long


Dire(1, 1) = "B2": Dire(1, 2) = "D2"
Dire(2, 1) = "B3": Dire(2, 2) = "D3"
Dire(3, 1) = "B4": Dire(3, 2) = "D4"
Dire(4, 1) = "G7": Dire(4, 2) = "I7"
Dire(5, 1) = "G11": Dire(5, 2) = "I11"
Dire(6, 1) = "G13": Dire(6, 2) = "I13"

Application.ScreenUpdating = False

If IsEmpty(Range("I15")) = False Then
    lro = lasrow
    Worksheets("User").Select
    Range("I15").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Data - Complete").Select
    Cells(lro + 1, 5).Select 'problem with selecting column
    ActiveSheet.Paste
    Worksheets("User").Select
    Range("I15").Select
    Selection.ClearContents
MsgBox ("lRow is " & lro)

End If

1 个答案:

答案 0 :(得分:0)

总结一下:

要解决此问题,您需要删除与您的函数同名的变量声明。

为了使它更简单,您可以删除该功能并让您的子像这样:

Sub Submit_data()

    Dim lRow As Long
    Dim lCol As Long
    Dim colName As Long
    Dim resetrange As Range
    Dim indicator As String
    Dim Dire(1 To 6, 1 To 2) As String
    Dim i As Integer
    Dim lasCol As Long
    Dim lro As Long


    Dire(1, 1) = "B2": Dire(1, 2) = "D2"
    Dire(2, 1) = "B3": Dire(2, 2) = "D3"
    Dire(3, 1) = "B4": Dire(3, 2) = "D4"
    Dire(4, 1) = "G7": Dire(4, 2) = "I7"
    Dire(5, 1) = "G11": Dire(5, 2) = "I11"
    Dire(6, 1) = "G13": Dire(6, 2) = "I13"

    Application.ScreenUpdating = False

    If IsEmpty(Range("I15")) = False Then
        'Change this so find the last row without the function
        'The "1" is telling it to look in column "A"
        'The ActiveSheet.Rows.Count is using the maximum number of possible rows as the row which is 1048576
        'So it is starting in A1048576 and going up till it finds the first non-empty row and returning that row number
        lro = ActiveSheet.Cells(ActiveSheet.Rows.count, 1).End(xlUp).row
        Worksheets("User").Select
        Range("I15").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Data - Complete").Select
        Cells(lro + 1, 5).Select 'problem with selecting column
        ActiveSheet.Paste
        Worksheets("User").Select
        Range("I15").Select
        Selection.ClearContents
    MsgBox ("lRow is " & lro)

    End If

并且您仍然不需要使用此处显示的内容来调整lasrow变量。