Vba 操纵数据以查找大于的值
我需要对工作簿中的数据进行排序,该工作簿包含多个工作表,只有一个宏。到目前为止,我已成功记录了删除不必要信息所需的步骤,但当我必须指定不同的较大数量时,出现了问题 我需要宏能够执行以下操作:Vba 操纵数据以查找大于的值,vba,excel,Vba,Excel,我需要对工作簿中的数据进行排序,该工作簿包含多个工作表,只有一个宏。到目前为止,我已成功记录了删除不必要信息所需的步骤,但当我必须指定不同的较大数量时,出现了问题 我需要宏能够执行以下操作: 消息框,提示输入更大的 超过要指定的金额 突出显示大于指定值的值 当前代码: Sub Conditional_Formatting() Columns("J:J").Select ActiveWorkbook.Worksheets("GULP USD").AutoFilter.Sort.So
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
不幸的是,数据表包含各种不同的数据行,并且每张数据表需要各种大于数量的数据,因此需要有一种方法来运行此数据表,而无需指定特定的行数或特定的大于值
非常感谢您的帮助。查看您的新帖子,第一个问题(提示大于值的messagebox)的一个可用解决方案是使用
InputBox
功能
您可以创建一个字符串变量,并使用inputbox返回值填充它
i、 e
然后可以使用适当的更改函数将其传递给数字数据类型
i、 e
就您的代码而言,以下是我对它所做的:
注意:我不知道你的自动过滤器应该做什么。我只是在测试时在我的代码中添加了一些东西,这样你就可以更改IF
块的ELSE
部分来设置你的自动过滤器,如果它没有打开,你需要它
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
嗨@Jean FrançoisCorbett我已经修改了帖子,只包含了我一直坚持的代码部分,包括“大于”和“突出显示”部分。贴出的代码与你的问题有什么关系?删除所有这些,然后尝试一些真正解决你问题的方法。如果你陷入困境,做一些研究,尝试自己解决问题,然后再问一个关于你尝试的具体问题。在没有展示研究成果的情况下,要求完整解决方案的问题通常会被否决和关闭。到目前为止,您的代码依赖于在正确的单元格中提供的信息。使用绝对引用提出的任何解决方案都可能非常脆弱且需要大量维护。请参见“title=“InputBox”>InputBox,您不需要
范围。选择然后选择selection.dosomething
语句。我知道宏记录器是这样显示的,但范围(地址)
和选择
都是范围
对象-因此每个对象都有相同的方法和属性。只需调用范围(“A1”)。复制
,而不必先选择对象。今天的链接有什么进展?这里:
Dim lngGreater As Long
lngGreater = CLng(strReturn)
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