Excel VBA:如何从用户获取输入并使用VBA突出显示范围内的正数和负数?

huangapple go评论79阅读模式
英文:

Excel VBA: How to get a input from a user and highlight its positive and negative number from the range using VBA?

问题

如果我点击一个按钮,它会显示一个输入框,如果用户在输入框中写入5,那么所有+5和-5的单元格应该在该列中被突出显示。

如果有人可以帮助我编写这个VBA代码,我将不胜感激。提前谢谢。

英文:

If I click a button it will show a inputbox and If the user writes 5 in inputbox then all the +5 and -5 should be highlighted from the column.

If anyone can help me with the VBA code of this it will be grateful. Thank you in advance.

答案1

得分: 1

很高兴知道,如果你能证明自己尝试过一些方法,那么你增加了被帮助的机会!一段代码,即使它不能返回你需要的结果,一些类似的解决方案链接,但不能解决问题等。

我正在做一个例外,希望你已经学会了这个方面,下次会做好该做的事情。

请尝试下面的代码,并在测试后发送一些反馈:

Sub testHighlightInpNumbersInCol()

   Dim sh As Worksheet, lastR As Long, rngCol As Range, mtchCell As Range, searchNo As String
   Const colNo As Long = 1 '要搜索数字的列号
   
   Set sh = ActiveSheet
   lastR = sh.Cells(sh.Rows.Count, colNo).End(xlUp).Row '搜索列上的最后一行
   Set rngCol = sh.Range(sh.Cells(1, colNo), sh.Cells(lastR, colNo)) '设置包含数据的范围
   rngCol.Interior.Color = xlNone '删除以前的背景颜色
   
   searchNo = InputBox("请输入要突出显示的数字值", "搜索数字") '返回要搜索的数字
   
   If searchNo = "" Then MsgBox "您没有输入任何内容...": Exit Sub '如果没有输入任何内容...
   If Not IsNumeric(searchNo) Then MsgBox "输入值必须为数字...": Exit Sub '如果输入了字母数字
   
   Set mtchCell = rngCol.Find(CDbl(searchNo), rngCol.Cells(1), xlValues, xlWhole, xlByRows) '查找具有正数值的单元格
   If mtchCell Is Nothing Then MsgBox searchNo & " 未找到...": Exit Sub '如果未找到,退出
   mtchCell.Interior.Color = vbYellow '如果找到,将其内部颜色设置为黄色
   
   Set mtchCell = rngCol.Find(-CDbl(searchNo), rngCol.Cells(1), xlValues, xlWhole, xlByRows) '查找负数值
   If mtchCell Is Nothing Then MsgBox "未找到 " & searchNo & " 的负数值...": Exit Sub
   mtchCell.Interior.Color = vbYellow
   
End Sub

上述代码假设在相应的列中只存在一个数字(正数和负数)。该代码可以适应搜索所有出现的情况,但是也很重要的是要明确解释你拥有的东西以及你试图实现什么...

英文:

It is good to know that you increase your chances to be helped if you prove that tried something by your own! A piece of code, even if it does not return what you need, some links to solution looking similar, but not solving the issue etc.

I am making an exception, hoping that you learned this aspect and next time will do what is to be done.

Please, try the next code and send some feedback after testing it:

Sub testHighlightInpNumbersInCol()

   Dim sh As Worksheet, lastR As Long, rngCol As Range, mtchCell As Range, searchNo As String
   Const colNo As Long = 1 'column number where the number to be searched
   
   Set sh = ActiveSheet
   lastR = sh.cells(sh.rows.count, colNo).End(xlUp).row 'last row on the searched column
   Set rngCol = sh.Range(sh.cells(1, colNo), sh.cells(lastR, colNo)) 'set the range keeping data
   rngCol.Interior.Color = xlNone                                    'remove any previous background colors
   
   searchNo = InputBox("Please, enter the numeric value to be highlighted", "Search for numbers") 'return the number to be searched
   
   If searchNo = "" Then MsgBox "You did not write anything...": Exit Sub   'in case of nothing input...
   If Not IsNumeric(searchNo) Then MsgBox "The input value MUST be numeric...": Exit Sub 'in case of alpha numeric entrance
   
   Set mtchCell = rngCol.Find(CDbl(searchNo), rngCol.cells(1), xlValues, xlWhole, xlByRows) 'find the cell with the positive value
   If mtchCell Is Nothing Then MsgBox searchNo & " could not be found...": Exit Sub 'if not found, exit
   mtchCell.Interior.Color = vbYellow                                                                                    'if found, color its interior in yellow
   
   Set mtchCell = rngCol.Find(-CDbl(searchNo), rngCol.cells(1), xlValues, xlWhole, xlByRows) 'find the negative value
   If mtchCell Is Nothing Then MsgBox "No negative value for " & searchNo & " could not be found...": Exit Sub
   mtchCell.Interior.Color = vbYellow
   
End Sub

The above code assumes that a single occurrence of the number (positive and negative) exists on the respective column. The code can be adapted to search for all occurrences, but it is also good to learn that we here are not mind reader and you need to clearly explain what you have and what you try accomplishing...

huangapple
  • 本文由 发表于 2023年6月12日 18:00:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定