在VBA中编写公式会导致类型不匹配13

时间:2017-09-20 12:28:27

标签: excel vba

覆盖问题:是否可以在VBA中编写将值保存为变量的公式,而不将它们放在工作表的单元格中?

我收到以“My_Date = Left(替换”)开头的行的错误...我认为这也是“My_Take = Right(替换”行)的问题。

该函数应该从字符串中取出数字:

Public Function SplitText(pWorkStr As String, pIsNumber As Boolean) As String

Dim xLen As Long
Dim xStr As String
xLen = VBA.Len(pWorkStr)
For i = 1 To xLen
    xStr = VBA.Mid(pWorkStr, i, 1)
    If ((VBA.IsNumeric(xStr) And pIsNumber) Or (Not (VBA.IsNumeric(xStr)) And Not (pIsNumber))) Then
        SplitText = SplitText + xStr
    End If
Next
End Function

这会将数字格式化为日期,并且不带任何字母:

Sub Fill_Date_and_Take()

    Dim NAMEoFILE As String

    Dim My_Date As String
    Dim My_Take As String

    NAMEoFILE = "I love dogs 09-20-17 Take Number 2.xlsx"

    My_Date = Left(Replace(Replace(Replace(SplitText(NAMEoFILE, True), 2, 0, "-"), 5, 0, "-"), 8, 0, "_"), 7)
        MsgBox (My_Date)

    My_Take = Right(Replace(Replace(Replace(SplitText(NAMEoFILE, True), 2, 0, "-"), 5, 0, "-"), 8, 0, "_"), 1)
        MsgBox (My_Take)

End Sub

1 个答案:

答案 0 :(得分:0)

我对Stack Overflow有点新意,所以我不确定如何设置GSerg对答案的评论,但我所要做的就是改变每一个"替换" to" WorksheetFunction.Replace&#34 ;!

My_Date = Left(WorksheetFunction.Replace(WorksheetFunction.Replace(WorksheetFunction.Replace(SplitText(NAMEoFILE, True), 2, 0, "-"), 5, 0, "-"), 8, 0, "_"), 7)

My_Take = Right(WorksheetFunction.Replace(WorksheetFunction.Replace(WorksheetFunction.Replace(SplitText(NAMEoFILE, True), 2, 0, "-"), 5, 0, "-"), 8, 0, "_"), 1)

谢谢GSerg !!