samedi 9 janvier 2021

VBA comparing user input and cell value, but not evaluating as intended

New to VBA an programming in general.

I wrote the following code to go through a list and retrieve the desired cells to a new workbook. I've used this code before, and everything works fine, but now I'm trying to evaluate whether the cell value is above or below a user input number (rafA), but VBA seems to not be evaluating correctly.

When it gets to the second if statement in the loop, it seems to evaluate as false, even when the cell value is greater than the input (rafA) value. So, run like this, I'm always getting the "Input value is greater" MsgBox, even when it's not the case.

Both rafA and testValue data types are set as Variant, and month is an integer value that gets me to the column I want, and then use Offset to get the respective cell value.

Any help??

rowCount = Cells(Rows.Count, 1).End(xlUp).Row
rowCount = rowCount - 1

range("A2").Select

For rowStart = 2 To rowCount
    If Not IsEmpty(ActiveCell.Offset(0, month + 2)) Then
        testValue = ActiveCell.Offset(0, month + 2).value
        MsgBox (rafA)
        MsgBox (testValue)
        If testValue > rafA Then
            'range(Cells(rowStart, month), Cells(rowStart, month + 8)).Copy_ 
                Destination:=inputwb.Sheets(1).range("I" & targetRow)
            MsgBox ("Cell Value is greater")
            'range(Cells(rowStart, 1), Cells(rowStart, 8)).Copy_ 
                Destination:=inputwb.Sheets(1).range("A" & targetRow)
            'targetRow = targetRow + 1
        Else
            MsgBox ("Input value is greater")
        End If
    End If

    ActiveCell.Offset(1, 0).Select
Next rowStart

Aucun commentaire:

Enregistrer un commentaire