操纵数据以查找大于值

时间:2014-11-20 11:49:39

标签: excel vba excel-vba

我需要在包含几张只有一个宏的工作簿中对数据进行排序,到目前为止,我已经设法记录了删除不必要信​​息所需的步骤但是当我必须指定不同的更大时我遇到了问题量。

我需要宏才能执行以下操作:

  1. 消息框提示更大 比指定金额
  2. 突出显示大于指定的值
  3. 当前代码:

    Sub Conditional_Formatting()
        Columns("J:J").Select
        ActiveWorkbook.Worksheets("GULP USD").AutoFilter.Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("GULP USD").AutoFilter.Sort.SortFields.Add Key:= _
            Range("J1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("GULP USD").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
            Formula1:="=250000"
        Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        With Selection.FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent6
            .TintAndShade = 0
        End With
        Selection.FormatConditions(1).StopIfTrue = False
    End Sub
    

    不幸的是,数据表包含各种不同的数据行,并且每张表需要多于大于数量的数据,因此需要有一种方法来运行它而不指定特定数量的行或特定的大于值。

    非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

查看您的新帖子,第一个问题(提示输入大于值的消息框)的一个可用解决方案是使用InputBox函数。

您可以创建一个字符串变量,并使用inputbox的返回值填充它。

即。

Dim strReturn           As String
strReturn = InputBox(Title:="Greater than what??", Prompt:="Enter a numeric value and I'll highlight everything greater than it for you.", Default:="42")

然后,您可以使用适当的更改函数将其传递给数字数据类型。

即。

Dim lngGreater          As Long
lngGreater = CLng(strReturn)

就你的代码而言,这是我用它做的: 注意:我不知道您的自动过滤器应该做什么。我刚刚在测试时在代码中添加了一些内容,因此您可以更改ELSE块的IF部分,以便在需要时自动设置自动过滤器(如果未启用)。

Option Explicit

Sub Conditional_Formatting()

Dim rngColumns      As Excel.Range
Dim wshGULP         As Excel.Worksheet
Dim strGreater      As String
Dim lngReturn       As Long

'First things first, lets ask our user for a greater than value
    strGreater = InputBox(Title:="Greater than what??", prompt:="Enter a numeric value and I'll highlight everything greater than it for you.", Default:="42")

'I'm assuming you want numeric values, but if not we can change this next part. I did just as an example for you.
'Basically, we are going to check if the user entered _
 a numeric value, and if not - we're going to ask them to retry or quit.

        If Not IsNumeric(strGreater) Then

        'Did you know the messagebox function returns something as well?  It returns a numeric value that indicates what button the user pressed.
            lngReturn = MsgBox(prompt:="I don't know what to do with non-numeric numbers. Please enter a numeric value, or press cancel to quit.", Title:="What was that??", Buttons:=vbOKCancel)

        'Now, we can recursively call this routine to try again, or we can quit based on whatever the user's choice was.
                If Not lngReturn = vbCancel Then Conditional_Formatting
            Exit Sub
        End If

'If execution gets this far, we know we have a numeric value for our greater than condition and we can move on.

'It's easier to set an object once, especially if you want to change your code later, so lets just set the worksheet here
    Set wshGULP = ActiveWorkbook.Worksheets("GULP USD")


    'Another timesaver is to use a "With" statement.  Basically, you tell the VBA compiler that eveyrthing you want to do applies _
     to the object that is the target of the "With" statement.  You stop this "With" block with the "End With" statement
        With wshGULP

        'Instead of using Columns("J:J").Select, you can just assing that range to a Range object
            Set rngColumns = .Range("J:J")

            'Have to check if there IS an autofilter before we can clear it
                If Not .AutoFilter Is Nothing Then
                    .AutoFilter.Sort.SortFields.Clear
                Else
                    rngColumns.AutoFilter
                End If


        'Notice I changed the J1 absolute reference in the next line to be the first cell of our column range object - again, makes it easeier if you need to change things later
            .AutoFilter.Sort.SortFields.Add Key:=Range(rngColumns.Cells(1, 1)), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

            'You can nest With statements like so:
            'You probably noticed, but I like to use TAB to indent my code so it's easier to see the stucture visually...
                With .AutoFilter.Sort
                    .Header = xlYes
                    .MatchCase = False
                    .Orientation = xlTopToBottom
                    .SortMethod = xlPinYin
                    .Apply
                End With

        'Now we're back into the original "With" block - remember, it stays in effect until you reach the "End With" statement for that block
        End With

    'I assume you want to select cells before calling this procedure, and have any in the selection highlighted, so I left this pretty much as you had it. _
     Just showing the same nested "With" blocks concept again for another example
        With Selection
            .FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="=" & strGreater    'See - this is where we set the greater than value you entered at the beginning
            .FormatConditions(.FormatConditions.Count).SetFirstPriority
                With .FormatConditions(1)
                        With .Interior
                            .PatternColorIndex = xlAutomatic
                            .ThemeColor = xlThemeColorAccent6
                            .TintAndShade = 0
                        End With
                    .StopIfTrue = False
                End With
        End With

End Sub