繁体   English   中英

Excel VBA-拆分每行并计算值

[英]Excel VBA - Split Each Line and Calulate Value

我在单元格中有多行遵循以下格式

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

基本上,我想做的是让脚本读取美元符号后的数字,并取最高的数字,并确认其高于当前租金,如果是,则将其更改为新的租金。

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

'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

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

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

截图

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM