在VBA中给定一个input框值,编写一个程序,将字体更改为斜体,使其在给定值以下的范围内

我对这个问题很困惑。 上面提到的问题要求你写一个程序,给出一个input框,你可以input一个截止水平。 一旦给出了截止水平,那么低于该截止水平的表格中的所有值都需要被斜体化。 我卡住的地方是让代码select单元格,而不是整个范围。 有什么build议么?

分配

Sales.xlsx文件包含给定月份中不同地区的公司的销售数据。 写一个斜体的macros,并将字体颜色更改为蓝色的月份区域8的销售量低于用户在InputBox中提供的截断级别。 例如,如果用户在input框中input10000,那么macros应search区域8中的销售小于10000的月份。然后,macros应将单元格E9,F9,I9,J9,K9和L9变为斜体与蓝色的字体

我的企图

Sub Homework7Problem2() ' Keyboard Shortcut: Ctrl+b Dim Cutoff As String Cutoff = InputBox("Enter the cutoff level you would like to use", "CutoffLevel") If Range("B2:M41").Cells < CutoffLevel Then Range("B2:M41").Cells.Font.Italic = True With Selection.Font .Color = -4165632 .TintAndShade = 0 End With End If End Sub 

基本上你需要遍历单元格,并确定哪些符合条件(在这种情况下,单元格小于截止)。 一旦你确定了这些单元格,你可以将它们格式化,只要你喜欢。 我更喜欢将它们放入一个范围variables中,然后将所有已识别的单元格一次全部格式化,这比将每个已识别的单元格格式化得快得多。 当你学习更多的VBA时,你会发现这被认为是很好的做法。

这是注释的代码:

 Sub Homework7Problem2() 'Declare Variables Dim rngSales As Range 'Range to store the Sales cells Dim SalesCell As Range 'Loop variable for rngSales Dim rngBelowTarget As Range 'Range to store cells that are below the target/cutoff Dim dTarget As Double 'Variable used to store the target/cutoff entered by the user 'Define what cells to check Set rngSales = Range("B2:M41") 'Reset formatting so that only cells below the new target will be formatted With rngSales .Font.Italic = False .Font.Color = vbBlack End With 'Use Application.InputBox and set the Type parameter to 1 to force a number dTarget = Application.InputBox("Enter the cutoff level you would like to use:", "Cutoff Level", Type:=1) If dTarget = 0 Then Exit Sub 'User pressed cancel 'Verify if there will be any matching cells If WorksheetFunction.CountIf(rngSales, "<" & dTarget) = 0 Then 'If no matching cells, return error and exit the subroutine MsgBox "No months found to have sales less than [" & dTarget & "]", , "No Matches" Exit Sub End If 'Loop though each cell in the range For Each SalesCell In rngSales.Cells If SalesCell.Value < dTarget Then 'If a matching cell is found, add it to the rngBelowTarget variable If rngBelowTarget Is Nothing Then 'This is the first cell found Set rngBelowTarget = SalesCell Else 'This is for subsequent cells found, add them to the variable Set rngBelowTarget = Union(rngBelowTarget, SalesCell) End If End If Next SalesCell 'Format cells that meet the condition With rngBelowTarget .Font.Italic = True .Font.Color = vbBlue End With End Sub 

有几种方法可以做到这一点,将整个范围与截断值进行比较,因为您所做的工作不会奏效,但是您可以遍历整个范围,并将格式应用于满足条件的任何单元格:

 Sub Homework7Problem2() ' Keyboard Shortcut: Ctrl+b Dim Cutoff As String Dim c As Range Cutoff = InputBox("Enter the cutoff level you would like to use", "CutoffLevel") For Each c In Range("B2:M41") If c < CLng(Cutoff) Then c.Font.Italic = True c.Font.Color = -4165632 c.Font.TintAndShade = 0 End If Next End Sub 

或者,您可以使用macros来将某些条件格式应用于该范围:

 Sub Homework7Problem2() ' Keyboard Shortcut: Ctrl+b Dim Cutoff As String Cutoff = InputBox("Enter the cutoff level you would like to use", "CutoffLevel") With Range("B2:M41") ' First delete earlier conditional formatting, to avoid conflicts .FormatConditions.Delete ' Add new conditional formatting .FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="=" & Cutoff With .FormatConditions(1).Font .Italic = True .Color = -4165632 .TintAndShade = 0 End With End With End Sub 

我认为从Excel中的条件格式对话框中应用条件格式会简单多了,但是由于问题需要VBA,所以我想你可能会用到这个。