如何在数据中查找部分字符串

时间:2015-11-06 12:58:50

标签: excel vba excel-vba

所以我一直致力于创建一个宏来解析大型数据集,而我正处于完成的尖端。我试图按月分析日期并忽略年份,以便我可以限定各种if语句以在另一列中打印字符串。打印字符串和其他所有内容不是问题,它只是找到日期。我不太关心今年更多只是“Jan”“Feb”“Mar”所以我可以确定它是Q1,Q2,Q3 1H,2H等等。我有的代码我是尝试这样做是在下面的legCounter = 3块。 (我把两个星号放在旁边没有按照预期做的那条线上)。

'Counts legs to narrow structure type
Do
DoEvents
    row_color_tester = row_number

    If tradeTime = Range("B" & (row_color_tester + 1)).Text Or tradeTime = Range("B" & (row_color_tester - 1)).Text Then
        legCounter = legCounter + 1

    End If

    row_number = row_number + 1

Loop Until tradeTime <> Range("B" & (row_color_tester + 1)).Text

'Analyzes individual legs to determine specific structure type
    If legCounter = 2 Then
        Range("Q" & (row_number - legCounter)) = "Spread"
        Rows(row_number - legCounter).EntireRow.Interior.ColorIndex = 4
    **ElseIf legCounter = 3 Then
        If StrComp(Range("H" & (row_number - 1)).Text, "Jan", vbTextCompare) > 0 & StrComp(Range("H" & (row_number - 2)).Text, "Feb", vbTextCompare) > 0 & StrComp(Range("H" & (row_number - 3)).Text, "Mar", vbTextCompare) > 0 Then**
            Range("Q" & (row_number - legCounter)) = "Q1"
            Rows(row_number - legCounter).EntireRow.Interior.ColorIndex = 4
        End If
    ElseIf legCounter = 4 Then
        Range("Q" & (row_number - legCounter)) = legCounter
        Rows(row_number - legCounter).EntireRow.Interior.ColorIndex = 4
    ElseIf legCounter = 5 Then
        Range("Q" & (row_number - legCounter)) = legCounter
        Rows(row_number - legCounter).EntireRow.Interior.ColorIndex = 4
    End If

  Loop Until row_number = lastRow

数据存储在H列中,格式如下:

16-Mar
16-Feb
16-Jan

2 个答案:

答案 0 :(得分:1)

这个小位将取你的范围并返回季度数字。

格式(CDate(范围(“H”&amp;(row_number - 1))。文本),“q”)

答案 1 :(得分:0)

在我看来,您希望InStr function或只需要Right函数来确定Range.Text property的月份部分。 StrCmp function比较整个字符串并返回排序位置。

ElseIf legCounter = 3 Then
    If Instr(1, Range("H" & (row_number - 1)).Text, "Jan", vbTextCompare) > 0 Or _
       Right(Range("H" & (row_number - 2)).Text, 3) = "Feb" Or _
       Range("H" & (row_number - 3)).Text Like "*Mar" Then
        Range("Q" & (row_number - legCounter)) = "Q1"
        Rows(row_number - legCounter).EntireRow.Interior.ColorIndex = 4
    End If
...

这是三种文本比较方法。对于所有四个季度,Select Case statement可能更合适。

ElseIf legCounter = 3 Then
    Select Case Right(Range("H" & (row_number - 2)).Text, 3)
      Case "Jan", "Feb", "Mar"
        Range("Q" & (row_number - legCounter)) = "Q1"
        Rows(row_number - legCounter).EntireRow.Interior.ColorIndex = 4
      Case "Apr", "May", "Jun"
        Range("Q" & (row_number - legCounter)) = "Q2"
        Rows(row_number - legCounter).EntireRow.Interior.ColorIndex = 4 '?
      Case "Jul", "Aug", "Sep"
        Range("Q" & (row_number - legCounter)) = "Q3"
        Rows(row_number - legCounter).EntireRow.Interior.ColorIndex = 4 '?
      Case "Oct", "Nov", "Dec"
        Range("Q" & (row_number - legCounter)) = "Q4"
        Rows(row_number - legCounter).EntireRow.Interior.ColorIndex = 4 '?
    End Select
...