分割函数中的VBA类型不匹配

时间:2016-11-28 11:03:17

标签: excel vba excel-vba

我编写了一个代码,我需要从这样的字符串中获取数字:“3/1”。我需要将这2个数字作为整数存储在2个不同的变量中。我已经在2个类中编写了这段代码: 这个函数是我在类(BigOne)中的split函数

Public Function SplitValues(pInput As String, pdelim As String) As String()
'Declaration of variables
Dim strSplit() As String
Dim countDelim As Integer

'Initialization of variables

countDelim = countCharacter(pInput, pdelim)
If countDelim > 0 Then
    ReDim strSplit(countDelim)
    strSplit = Split(pInput, pdelim)
    SplitValues = strSplit
End If
End Function

在主类中,我有一个调用此函数的函数,它将数字拆分以获取我想要的值。但是我收到“类型不匹配错误”我无法检测到此类型不匹配的原因。

Public Function get_MaxChars(pInput As String) As Integer
'declaration of variables
    Dim gen As cBigOne
    Dim values As String

   'Main code
    pInput = CStr(pInput)
    Debug.Print (pInput)
    values = gen.SplitValues(pInput, "/")
    get_MaxChars = CInt(values(0))

End Function

所以,我无法理解为什么它不能正常工作,我收到类型不匹配错误。因为,我相信无论我在哪里传递相同的类型。

3 个答案:

答案 0 :(得分:4)

SplitValues返回一个String数组,并尝试将其分配给String。请尝试将values调暗为String()

如果您没有创建班级实例,则在致电SplitValues时仍然会遇到问题,只是说gen将是一个。在Dim gen As cBigOne之后,您应该Set gen As New cBigOne

答案 1 :(得分:2)

我遇到了同样的问题。然后我发现你必须将数组声明为VARIANT

这是我的代码,我将每个到达Outlook的邮件的附件保存到指定的文件夹

        Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
           Dim objOL As Outlook.Application

            Dim arr As Variant
            Dim i As Integer
            Dim ns As Outlook.NameSpace
            Dim itm As MailItem
            Dim m As Outlook.MailItem
            Dim j As Long
        Dim lngCount As Long
        Dim strFile As String
        Dim strFolderpath As String
        Dim strDeletedFiles As String
        Dim fs As FileSystemObject
        Dim mldat As Date
        Dim strsndr As String

            'On Error Resume Next
            Set ns = Application.Session
            arr = Split(EntryIDCollection, ",")



        '*******************************************************************************************

        ' Set the Attachment folder.
        strFolderpath = "z:\dropbox (AAA-DZ)\25_Attach\"

        ' Check each selected item for attachments. If attachments exist,
        ' save them to the strFolderPath folder and strip them from the item.
        For ij = 0 To UBound(arr)
        Set itm = ns.GetItemFromID(arr(ij))
                If itm.Class = olMail Then
                                With itm

                                ' This code only strips attachments from mail items.
                                ' If objMsg.class=olMail Then
                                ' Get the Attachments collection of the item.
                                Set objAttachments = .Attachments
                                lngCount = objAttachments.Count
                                strDeletedFiles = ""

                                If lngCount > 0 Then

                                ' We need to use a count down loop for removing items
                                ' from a collection. Otherwise, the loop counter gets
                                ' confused and only every other item is removed.
                                Set fs = New FileSystemObject

                                For i = lngCount To 1 Step -1

                                    ' Save attachment before deleting from item.
                                    ' Get the file name.


                                    strFile = Right("0000" + Trim(Str$(Year(.SentOn))), 4) + "_" + Right("00" + Trim(Str$(Month(.SentOn))), 2) + "_" + Right("00" + Trim(Str$(Day(.SentOn))), 2) + "_" + Right("00" + Trim(Str$(Hour(.SentOn))), 2) + "_" + Right("00" + Trim(Str$(Minute(.SentOn))), 2) + "_" + Right("00" + Trim(Str$(Second(.SentOn))), 2) + "_" + .SenderEmailAddress + "_" + .Parent + "_" + objAttachments.Item(i).FileName

                                    ' Combine with the path to the Temp folder.
                                    strFile = strFolderpath & strFile

                                    ' Save the attachment as a file.
                                          If Left(objAttachments.Item(i).FileName, 5) <> "image" Then
                                          objAttachments.Item(i).SaveAsFile strFile

                                            ' Delete the attachment.
                                            objAttachments.Item(i).Delete

                                            'write the save as path to a string to add to the message
                                            'check for html and use html tags in link
                                            If .BodyFormat <> olFormatHTML Then
                                                strDeletedFiles = strDeletedFiles & vbCrLf & "<file://" & strFile & ">"
                                                Else
                                                strDeletedFiles = strDeletedFiles & "<br>" & "<a href='file://" & _
                                                strFile & "'>" & strFile & "</a>"
                                            End If
                                        Else
                                        objAttachments.Item(i).Delete

                                        End If


                                Next i

                                      ' Adds the filename string to the message body and save it
                                      ' Check for HTML body
                                      If Len(strDeletedFiles) > 5 Then
                                              If .BodyFormat <> olFormatHTML Then
                                                  .Body = vbCrLf & "The file(s) were saved to " & strDeletedFiles & vbCrLf & .Body
                                              Else
                                                  .HTMLBody = "<p>" & "The file(s) were saved to " & strDeletedFiles & "</p>" & .HTMLBody
                                              End If

                                          .Save
                                      End If
                                  Else
                                  objAttachments.Item(1).Delete

                                  End If


                        End With

                End If
        Next ij

    ExitSub:

        Set objAttachments = Nothing
        Set objSelection.Item(ij) = Nothing
        Set objSelection = Nothing
        Set objOL = Nothing


        '********************************************************************************************


            Set ns = Nothing
            Set itm = Nothing
            Set m = Nothing
        End Sub

答案 2 :(得分:0)

“类型不匹配”错误13当我们按照以下数据类型进行拆分时,我们可能会收到此错误,

** Public Sub Array_Split()

此data_ Dim X()As Variant _type将更改为字符串,这意味着如果我们输入错误的数据类型,我们将获得类型不匹配错误< / strong>那个狗屎

Dim X()As Variant

Dim VALU As Variant

VALU =“Raj,Kumar”__我们也可以选择像范围(“A2”)或细胞(1,1)这样的细胞参考

X = VBA.split(VALU,“,”)

MsgBox X(0)&amp; vbNewLine&amp; X(1)

End Sub