Excel VBA - 拆分每一行并计算值

时间:2013-12-18 20:13:45

标签: excel vba excel-vba

我在单元格中有多行符合以下格式

12/1/2013-$590.00
10/1/2014-$602.00

基本上我要做的是让脚本读取美元符号后面的数字并取最高值并验证它是否高于当前租金,如果是,则将其更改为新的。< / p>

我尝试使用以下代码,但遇到了一些问题。 任何帮助将不胜感激

'Start Rent Update check
' First, lets check if the text in the pointed cell is divided into separate rows at all
' If this is not the case, we will display the whole text
WhereFrom = NewRent



Temporary = InStr(WhereFrom, Chr(10))
MsgBox (Temporary)
If Temporary = 0 Then
    GetTextRow = WhereFrom ' return text from pointed cell
    intPos = InStr(1, GetTextRow, "$")

        If intPos > 0 Then
            MsgBox ("Storage Variable " & StorageVariable)
            StorageVariable = CInt(Right(GetTextRow, Len(GetTextRow) - intPos))
            If StorageVariable > 100 Then
                If StorageVariable > Rent Then
                      Rent = StorageVariable
                End If

            Else
                Parking = StorageVariable
            End If

        End If
Else
    ' lets also check if the row number the user provided is not too big.

    TemporaryArray = Split(WhereFrom, Chr(10))
    Do While Not (RowNumber - 1 > UBound(TemporaryArray) Or _
    RowNumber = 0)
    ' if everything is all right the function returns (displays) the chosen row
        GetTextRow = TemporaryArray(RowNumber - 1)
         intPos = InStr(1, GetTextRow, "$")
         MsgBox (intPos)
        If intPos > 0 Then
            MsgBox (StorageVariable)
            StorageVariable = CInt(Right(GetTextRow, Len(GetTextRow) - intPos))
            If StorageVariable > 100 Then
                If StorageVariable > Rent Then
                      Rent = StorageVariable
                End If

            Else
                Parking = StorageVariable
            End If
                 RowNumber = RowNumber + 1
        End If
    Loop
    End If
'Check end of rent update

1 个答案:

答案 0 :(得分:1)

要检索单元格中的最高租金,您可以使用此

Sub Sample()
    Dim ws As Worksheet
    Dim MyAr, tmpAr
    Dim i As Long
    Dim Rent As Double

    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws.Range("A1")
        If InStr(1, .Value, "$") Then
            MyAr = Split(.Value, Chr(10))

            For i = LBound(MyAr) To UBound(MyAr)
                If InStr(1, MyAr(i), "$") Then
                    tmpAr = Split(MyAr(i), "$")

                    If Val(Trim(tmpAr(UBound(tmpAr)))) > Rent Then _
                    Rent = Val(Trim(tmpAr(UBound(tmpAr))))

                End If
            Next i
        End If
    End With

    Debug.Print Rent
End Sub

<强>截图

enter image description here