VBA如果,并且不平等运算不起作用

时间:2019-08-07 15:05:24

标签: excel vba

我有一个数据列表,第一列是一个类别,其行类似于A,A,A,B,B,B,B,C,C等,然后其他列是每行的正数或负数(此数字在类别中有所不同)。我正在尝试定义一个函数,每个函数都具有一个类别A,B,C ...,并检查A的第一个和最后一个实例是正数还是负数,然后在一行上吐出一个结果。但是,检查不平等的最后阶段似乎不起作用。似乎忽略了And运算符,只是将And的第一个条件用作唯一条件。

Public Function TickerCurve(Tickers As Range, Ticker As String) As String

Dim c As Integer
Dim d As Integer
Dim StrA, StrB As String
Dim col As Range
c = 0

For Each col In Tickers.Rows
    d = ActiveCell.Row
    If col.Value = Ticker Then
        StrA = Cells(d, 2).Value
        StrB = Cells(d + 1, 2).Value
        If StrA = StrB Then
            c = c + 1
            d = d + 1
        Else
            If Worksheets("Sheet2").Cells(d - c, 16).Value > 0 And Worksheets("Sheet2").Cells(d, 16).Value > 0 Then
                TickerCurve = "USD above EUR"
                c = 0
            ElseIf Worksheets("Sheet2").Cells(d - c, 16).Value < 1 And Worksheets("Sheet2").Cells(d, 16).Value < 0 Then
                TickerCurve = "EUR above USD"
                c = 0
            ElseIf Worksheets("Sheet2").Cells(d - c, 16).Value < 0 And Worksheets("Sheet2").Cells(d, 16).Value > 0 Then
                TickerCurve = "Cross"
                c = 0
            ElseIf Worksheets("Sheet2").Cells(d - c, 16).Value > 0 And Worksheets("Sheet2").Cells(d, 16).Value < 0 Then
                TickerCurve = "Cross"
            End If
        End If
    End If
Next col


End Function

Data

Uses (new)

1 个答案:

答案 0 :(得分:0)

在继续操作之前,将宏分配给按钮。我希望它能对您有所帮助,但不确定您想要的应用条件是什么,但这应该可以。

尝试一下:

Sub tickercurve()

Dim dict As New Scripting.Dictionary
Dim cel As Range
Dim varKey As Variant
Dim result As Range
Dim tickers As Range
Dim tickercurve As String
Set tickers = Application.InputBox("Select the Tikers Range", Type:=8)
Set result = Application.InputBox("Select the result start Range", Type:=8)

For Each cel In tickers.Columns(1).Cells
    i = i + 1
    If Not dict.Exists(cel.Text) Then
        dict.Add cel.Text, cel

    Else
        Set rngTemp = Union(cel, dict(cel.Text))
        dict.Remove cel.Text
        dict.Add cel.Text, rngTemp
    End If
Next cel
i = 0
For Each varKey In dict.Keys

    With Range(dict.Item(varKey).Address)

            If Cells(.Cells(1, 1).row, 16).Value > 0 And Cells(.Cells(.Rows.Count, .Columns.Count).row, 16).Value > 0 Then
                tickercurve = "USD above EUR"
            ElseIf Cells(.Cells(1, 1).row, 16).Value < 1 And Cells(.Cells(.Rows.Count, .Columns.Count).row, 16).Value < 0 Then
                tickercurve = "EUR above USD"
            ElseIf Cells(.Cells(1, 1).row, 16).Value < 0 And Cells(.Cells(.Rows.Count, .Columns.Count).row, 16).Value > 0 Then
                tickercurve = "Cross"
            ElseIf Cells(.Cells(1, 1).row, 16).Value > 0 And Cells(.Cells(.Rows.Count, .Columns.Count).row, 16).Value < 0 Then
                tickercurve = "Cross"
            End If

    Cells(result.row + i, result.Column).Value = varKey
    Cells(result.row + i, result.Column + 1).Value = tickercurve
    i = i + 1

    End With

Next varKey


Set dict = Nothing

End Sub

enter image description here