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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40842732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:37:28  来源:igfitidea点击:

VBA type mismatch in split function

excelvbaexcel-vba

提问by TMikonos

I have written a code where I need to get the number from a string like this: "3/1". I need to store those 2 numbers as integer in 2 different variables. I have written this code in 2 classes: This function is my split function in the class (BigOne)

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

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

In the main class I have a function calling to this function that splits the number to get the values that I want. However I am getting a "Type Mismatch error" I am not able to detect the reason of this type mismatch.

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

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

So, I am not able to see why it is not working correctly and I am getting the type mismatch error. Because, I believe that everywhere I am passing the same type.

所以,我不明白为什么它不能正常工作,并且我收到了类型不匹配错误。因为,我相信在任何地方我都在传递相同的类型。

回答by bobajob

SplitValuesreturns a String array, and you are trying to assign it to a String. Try dimming valuesas String()instead.

SplitValues返回一个字符串数组,而您正试图将其分配给一个字符串。尝试调光values作为String()代替。

You'll still have an issue when calling SplitValuesas you haven't created an instance of your class, just said that genwill be one. After Dim gen As cBigOne, you should have Set gen As New cBigOne

您在调用时仍然会遇到问题,SplitValues因为您还没有创建类的实例,只是说gen将是一个实例。之后Dim gen As cBigOne,你应该有Set gen As New cBigOne

回答by user2485790

I had the same problem. Then I found that you have to declare the array as VARIANT

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

This is my code, where I save an attachment for every mail that arrives in Outlook to a specified folder

这是我的代码,我将到达 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)_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

回答by Dhiraj Mishra

"Type Mismatch" Error 13 When we split as per below data type we can get this error,

“类型不匹配”错误 13 当我们按照以下数据类型拆分时,我们会得到这个错误,

**Public Sub Array_Split()

**公共子数组_Split()

This data_Dim X() As Variant_type would be change to string that mean if we are entring wrong data type we will get Type Mismatch Errorthat shit

此 data_ Dim X() As Variant_type 将更改为字符串,这意味着如果我们输入错误的数据类型,我们将得到该狗屎的类型不匹配错误

Dim X() As Variant

Dim X() 作为变体

Dim VALU As Variant

Dim VALU 作为变体

VALU = "Raj,Kumar"__ also we can select cell refrence like range("A2") or cells(1,1)

VALU = "Raj,Kumar"__ 我们也可以选择单元格引用,如 range("A2") 或单元格 (1,1)

X = VBA.split(VALU, ",")

X = VBA.split(VALU, ",")

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

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

End Sub

结束子