0

「M - F」などの文字列を「M」と「F」だけに分離しようとしています。そして、これらの新しい文字列を 2 つの異なるセルに入れます。Split メソッドを試しましたが、それでもいくつかのエラーが報告され、どうすればよいかわかりません。誰でも助けることができます。

Function Aprobace(apr As String) As String
    Dim spt() As String
    Dim i As Integer
    If InStr(1, apr, " - ") < 1 Then
        Aprobace = apr
    Else
        spt = Split(apr, " - ")
        For i = 0 To Application.WorksheetFunction.CountA(spt)
        ActiveCell.Value = spt(i).Value
        ActiveCell.Offset(1, 0).Select
        Next i
    End If
End Function
4

1 に答える 1

0

Your first mistake is: ActiveCell.Value = spt(i).Value. Since spt is declared as an array at this point and not a worksheet range, it doesn't make sense for it to be using the .Value method like a Range would.

Secondly, Application.WorksheetFunction.CountA(spt) doesn't make sense to use on spt because of similar reasons. Since spt is still just in memory again and not actually a range on the worksheet, it shouldn't be utilized in worksheet functions. If you are trying to find out how many indexes that the spt is filled with after the Split method is run, this is more appropriate: UBound(spt)

See the revised function:

Function Aprobace(apr As String) As String
    Dim spt() As String
    Dim i As Integer
    Dim count As Long

    If InStr(1, apr, " - ") < 1 Then
        Aprobace = apr
    Else
        spt = Split(apr, " - ")
        count = UBound(spt)  '<----------------------
        For i = 0 To count
            ActiveCell.Value = spt(i) '<-----------------------
            ActiveCell.Offset(1, 0).Select
        Next i
    End If
End Function
于 2013-04-28T16:44:59.513 に答える