类型不匹配仅在在线文件

时间:2017-08-30 10:45:56

标签: excel excel-vba vba

在下面的代码中,我根据表中的多个条目多次运行代码。在这种情况下,它是4.如果我运行代码,它会在某个循环上第一次出现错误13类型不匹配。因此当a = 1时它会给出一个。但是当我下次运行一个时,它不会在a = 1上给出错误,但它会在a = 2上。所以我必须运行代码4次,然后它实际上给出了我想要的结果。

当我放入一些MsgBoxes来检查问题所在的位置时,它位于TopicCount计算的某处,使用以下公式TopicCount = [TopicAmount].Cells(a, 1)

这些文档位于SharePoint上,当我下载并在本地运行它们时,它们不会给出任何错误。所以我认为它与路径有关,但我无法找到问题。

如果缺少任何信息或您有其他问题,请不要犹豫。

非常感谢任何兴趣和帮助,非常感谢你。

EDIT1 :为了让它更奇怪,我按照Greedo的提示,使用F9和F8逐步完成代码。当我在TopicCount中慢慢点击F8时,它在整个代码中都能很好地工作。当我按住F8而不是点击它时,我得到了类型不匹配错误。

干杯, 巴特


Sub PullValue()

Dim path As String, file As String, sheet As String, sheet2 As String, TableName As String
Dim LastRow As Long, SectionCount As Long, StatusCount As Long, TopicCount As Long
Dim i As Integer, j As Integer, a As Integer


Application.ScreenUpdating = False
Application.AutoCorrect.AutoFillFormulasInLists = False


Worksheets("Consolidation").Cells.ClearContents

With Worksheets("Hidden").ListObjects("TopicAmount")

    If Not .DataBodyRange Is Nothing Then
            .DataBodyRange.Delete
    End If
    .ListRows.Add AlwaysInsert:=True

End With

With Worksheets("Sections").ListObjects("Sections")

    SectionCount = .DataBodyRange.Rows.Count - _
    Application.CountBlank(.DataBodyRange)

End With

With Worksheets("Settings").ListObjects("Status")

    StatusCount = .DataBodyRange.Rows.Count - _
    Application.CountBlank(.DataBodyRange)

End With

For a = 1 To SectionCount

    With Worksheets("Consolidation")

        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    End With

    If LastRow <> 1 Then

        LastRow = LastRow + 2

    End If

    Cells(LastRow, "A").Value = [Sections].Cells(a, 1)

    path = Application.ThisWorkbook.path + "/"
    file = Worksheets("Consolidation").Cells(LastRow, 1)
    sheet = "Overview"
    sheet2 = "Hidden"

    Worksheets("Hidden").ListObjects("TopicAmount").ListRows.Add AlwaysInsert:=True

    [TopicAmount].Cells(a, 1) = "='" & path & "[" & file & ".xlsm]" & _
            sheet2 & "'!" & Cells(1, 4).Address & ""

    TopicCount = [TopicAmount].Cells(a, 1)

    For i = LastRow + 1 To LastRow + TopicCount + 1

        For j = 1 To StatusCount + 1

            Cells(i, j).Formula = "='" & path & "[" & file & ".xlsm]" & _
            sheet & "'!" & Cells(i - LastRow, j).Address & ""

        Next j

    Next i

    Worksheets("Consolidation").ListObjects.Add(xlSrcRange, Range(Cells(LastRow + 1, 1), Cells(LastRow + TopicCount + 1, _
    StatusCount + 1)), , xlYes).Name = [Sections].Cells(a, [Sections[Sections]].Column)

    TableName = [Sections].Cells(a, 1)
    Worksheets("Consolidation").ListObjects(TableName).TableStyle = "Testing Progress"
    Cells(LastRow + 1, 1).AutoFilter

Next a


Application.ScreenUpdating = True
Application.AutoCorrect.AutoFillFormulasInLists = True

End Sub

1 个答案:

答案 0 :(得分:0)

从你得到的评论中,问题是与参考文献有关。 它不会足够快地更新。

所以这是一个解决方案:

您可以使用Application.Wait (Now + TimeValue("0:00:01"))等待一秒钟,并让Excel时间更新参考。

如果你不想做一点Preatier,你可以像这样使用IsError()函数。

If IsError(TopicCount ) Then'When #Ref! Wait for 1 Second    
    Application.Wait (Now + TimeValue("0:00:01"))
    DoEvents
End If

或者您可以在循环中执行相同操作,如果值设置为Corectly,则每秒后检查一次。

旧答案

我认为问题是,在这个单元格中有一个Number Formated as String。所以你会得到一个类型不匹配,因为你不能将一个字符串插入一个长变量。

您可以尝试将字符串转换为Long,如下所示:

TopicCount = CLng([TopicAmount].Cells(a, 1))

其次我会像John Coleman所说的那样对Deklare这些VAriables说:

 Dim path As String, file As String, sheet As String, sheet2 As String, TableName As String
    Dim LastRow As Long, SectionCount As Long, StatusCount As Long, TopicCount As Long
    Dim i As Integer, j As Integer, a As Integer
    Dim Rng1 As Range

怎么一回事,因为: Dim yxz, abc as Integer相当于:

Dim yxz
Dim abc as Integer

,这相当于:

Dim yxz as Variant
Dim abc as Date