[英]Type mismatch error using custom class subroutine in Excel VBA
在Excel VBA中工作,我有一個類模塊,可以在其中定義類“標記”。 我的類的屬性之一是TextLine(),它是一個最多包含5個字符串的數組。 我在類模塊中定義了以下兩種方法。 在另一個(常規)模塊中,我用自定義Marker對象填充markerArr()
。 在每個數組索引處使用數據加載每個對象的屬性都可以正常工作...但是,在將數據加載到每個索引處的對象中之后,我嘗試使用markerArr(count).ProcessLines
但是會收到類型不匹配錯誤。 由於ProcessLines
是我的類模塊中的公共子項,並且markerArr(count)
包含Marker對象,所以我似乎無法理解為什么會發生此錯誤...我是否忽略了某些明顯的內容?
'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
您的CreateSerial函數采用整數作為參數,但是您嘗試傳遞字符串。 我指出了一些問題:
如果startSerNo <1並且Num_Sers <1然后'在這里,我假設您使用這些半全局變量作為變體-您在此處使用數字比較
startSerNo = InputBox(“輸入開始打印的序列號。”&Chr(10)&_“輸入1將導致-001,輸入12將導致-012,依此類推。”,“開始序列號”,“ 1“) '在這里,startSerNo從輸入框作為字符串返回
Num_Sers = InputBox(“輸入要執行的序列化的數量。”&Chr(10)&_“這將控制打印整個標記集的多少份。”,“總序列化”,“ 1”) '此處Num_Sers也成為一個字符串
萬一
strSerial = CreateSerial(startSerNo) '在這里,您要將一個字符串傳遞給CreateSerial函數。 傳遞整數,或將變體作為參數傳遞給CreateSerial
'......more code.....
私有函數CreateSerial(ByVal startNum As Integer )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.