使用Excel VBA

时间:2015-12-31 13:48:35

标签: excel vba class excel-vba methods

在Excel VBA中工作,我有一个类模块,我在其中定义了我的课程&#39; Marker&#39;。我的类的一个属性是TextLine(),它是一个最多可包含5个字符串的数组。我在我的类模块中定义了以下两种方法。在另一个(常规)模块中,我使用自定义标记对象填充markerArr()。使用每个数组索引处的数据加载每个对象的属性工作正常...但是,在将数据加载到每个索引处的对象后,我尝试使用markerArr(count).ProcessLines但是收到类型不匹配错误。由于ProcessLines是我的类模块中的公共子,并且markerArr(count)包含标记对象,我似乎无法理解为什么会发生此错误...我是否忽略了一些明显的东西?< / p>

'Serial number replacement processing function
Public Sub ProcessLines()

    Dim strSerial As String
    Dim toggle As Boolean
    toggle = False

    Dim i As Integer
    For i = 0 To 4
        If Trim(m_TxtLines(i)) <> "" Then
            'Add linefeed char to non-empty text lines
            m_TxtLines(i) = m_TxtLines(i) & Chr(10)

            'Detect if it is a serialized line
            If InStr(1, m_TxtLines(i), "XXXXXX-YYY") > 0 Then
                m_Serial(i) = True
                toggle = True
            End If
        End If
    Next

    'When at least one line on the marker is serialized, create and replace serial text
    If toggle = True Then
        'Only prompt for input once
        If startSerNo < 1 And Num_Sers < 1 Then
            startSerNo = InputBox("Enter the serial number to start printing at." & Chr(10) & _
                "Entering 1 will result in -001, entering 12 will result in -012, etc.", "Starting Serial #", "1")

            Num_Sers = InputBox("Enter the amount of serializations to perform." & Chr(10) & _
                "This will control how many copies of the entire marker set are printed.", "Total Serializations", "1")
        End If

        strSerial = CreateSerial(startSerNo)

        Dim j As Integer
        For j = 0 To 4
            If m_Serial(j) Then
                m_TxtLines(j) = Replace(m_TxtLines(j), "XXXXXX-YYY", strSerial)
            End If
        Next
    End If

End Sub

'Creates the string to replace XXXXXX-YYY by concatenating the SFC# with the starting serial number
Private Function CreateSerial(ByVal startNum As Integer)
    Dim temp
    temp = SFC_Num

    Select Case Len(CStr(startNum))
        Case 1
            temp = temp & "-00" & startNum
        Case 2
            temp = temp & "-0" & startNum
        Case 3
            temp = temp & "-" & startNum
        Case Else
            temp = temp & "-001"
    End Select

    CreateSerial = temp
End Function

1 个答案:

答案 0 :(得分:0)

您的CreateSerial函数将整数作为参数,但您尝试传递字符串。我已经指出了一些问题:

如果startSerNo&lt; 1和Num_Sers&lt; 1然后 &#39;我假设您将这些半全局变量作为变体 - 您在这里使用数字比较

startSerNo = InputBox(&#34;输入序列号以开始打印。&#34;&amp; Chr(10)&amp; _                 &#34;输入1将导致-001,输入12将导致-012等。&#34;,&#34;开始序列#&#34;,&#34; 1&#34;) &#39;这里startSerNo作为输入框中的字符串返回

Num_Sers = InputBox(&#34;输入要执行的序列化数量。&#34;&amp; Chr(10)&amp; _                 &#34;这将控制整个标记集的打印份数。&#34;,&#34;总序列化&#34;,&#34; 1&#34;) &# 39;这里Num_Sers也变成了一个字符串

结束如果

strSerial = CreateSerial(startSerNo) &#39;在这里,您将String传递给CreateSerial函数。传递一个整数,或允许变量作为CreateSerial的参数

'......more code.....

私人功能CreateSerial(ByVal startNum为整数