临时线显示类型不匹配错误

问题描述 投票:0回答:1
Sub assign_sr_no()

For Each sh In ThisWorkbook.Sheets
    If sh.Name = "master" Then GoTo gogo
    If sh.ProtectContents = False Then GoTo gogo

    Dim srno As String
    Dim temp As Integer
    temp = CInt(Right(Sheets("master").Range("A65536").End(xlUp).Text, 4))
    srno = "VE17" & "_" & Format(temp + 1, "0000")
    sh.Unprotect "VALUE"
    sh.Cells.Validation.Delete
    sh.Range("L1").Value = srno
    sh.Hyperlinks.Add Anchor:=sh.Range("A1"), SubAddress:="master!A1", Address:="", TextToDisplay:="Faridabad Plant"
    Call copy_from_ve(srno)

gogo:
    Next
End Sub

这段代码用于为工作表分配序列号,然后有另一个代码将数据从工作表复制到主工作表。临时线显示

错误13即类型不匹配错误。

excel vba
1个回答
1
投票

整个excel表是空的我有这25列[...]

您不能使用Integer函数将空字符串转换为CInt。此代码重现错误:

Debug.Print CInt("")

你可以使用Val函数给自己一个Double,你可以转换为Integer如果你绝对肯定你永远不会需要一个大于32,767的值。否则,请使用Long

temp = Val(Right(Sheets("master").Range("A65536").End(xlUp).Text, 4))

你应该真的打破那个语句,如果它在每次迭代时都是常量的话,就把它拉出循环体。

此外,Excel> 2003在工作表上有超过65K可能的行;考虑使用.Range("A" & [ActiveSheet.]Rows.Count)(或将Sheets("master")拉入自己的With块变量,或者更好,使用表格的CodeName

With MasterSheet ' assuming (Name) property was set to "MasterSheet"
    Dim lastValue As String
    lastValue = .Range("A" & .Rows.Count).End(xlUp).Text

    Dim temp As Long
    temp = Val(Right(lastValue, 4)

    Dim sh As Worksheet
    For Each sh In ThisWorkbook.Worksheets
        If sh.Name <> .Name Then ' no need for GoTo jumping here
            '...
        End If
    Next

End With
© www.soinside.com 2019 - 2024. All rights reserved.