操纵数据来查找大于价值

我需要使用一个macros对包含多个工作表的工作簿中的数据进行sorting,到目前为止,我已经设法logging了我需要删除不必要信​​息的步骤,但是当我必须指定不同的更大数量时遇到问题。

我需要macros才能做到以下几点:

  1. 提示input大于指定值的消息框
  2. 突出显示大于指定值的值

当前代码:

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 

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

任何帮助将不胜感激。

看看你的新post,第一个问题的一个可用解决scheme(messagebox提示大于值)将是使用InputBox函数。

您可以创build一个stringvariables,并使用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") 

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

 Dim lngGreater As Long lngGreater = CLng(strReturn) 

至于你的代码,这是我做了什么:注意:我不知道你的自动filter应该做什么。 testing时,我在代码中添加了一些东西,所以你可以改变IF块的ELSE部分来设置你的自动filter,如果你不需要的话。

 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