Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在给定输入框值的VBA中,编写一个程序,将低于给定值的单元格的字体更改为斜体_Vba_Excel - Fatal编程技术网

在给定输入框值的VBA中,编写一个程序,将低于给定值的单元格的字体更改为斜体

在给定输入框值的VBA中,编写一个程序,将低于给定值的单元格的字体更改为斜体,vba,excel,Vba,Excel,我对这个问题感到相当困惑。上述问题要求您编写一个程序,提供一个输入框,您可以在其中输入截止电平。一旦给出了截止水平,该截止水平以下的工作表范围内的所有值都需要斜体。我陷入困境的地方是让代码选择单元格,而不是整个范围。有什么建议吗 分配 文件Sales.xlsx包含公司在给定月份的不同地区的销售数据。编写一个宏,在区域8的销售额低于用户在输入框中提供的截止值的月份,将字体颜色改为蓝色。例如,如果用户在输入框中输入10000,则宏应搜索区域8的销售额低于10000的月份。然后,宏应使用蓝色字体将单元

我对这个问题感到相当困惑。上述问题要求您编写一个程序,提供一个输入框,您可以在其中输入截止电平。一旦给出了截止水平,该截止水平以下的工作表范围内的所有值都需要斜体。我陷入困境的地方是让代码选择单元格,而不是整个范围。有什么建议吗

分配 文件Sales.xlsx包含公司在给定月份的不同地区的销售数据。编写一个宏,在区域8的销售额低于用户在输入框中提供的截止值的月份,将字体颜色改为蓝色。例如,如果用户在输入框中输入10000,则宏应搜索区域8的销售额低于10000的月份。然后,宏应使用蓝色字体将单元格E9、F9、I9、J9、K9和L9变成斜体

我的尝试
子作业7问题2()
'键盘快捷键:Ctrl+b
像字符串一样变暗
切断=输入框(“输入要使用的切断电平”,“切断电平”)
如果范围(“B2:M41”)。单元格<截止电平,则
范围(“B2:M41”).Cells.Font.Italic=True
使用Selection.Font
.Color=-4165632
.TintAndShade=0
以
如果结束
端接头

有几种方法可以做到这一点,将整个范围与截止值进行比较,因为这样做不起作用,但您可以在整个范围内循环,并将格式应用于满足条件的任何单元格:

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

我认为只应用Excel中“条件格式”对话框中的条件格式要简单得多,但由于问题需要VBA,我想您可能会从中得到一些好处。

基本上,您需要在单元格上循环,并确定哪些单元格符合条件(在这种情况下,小于截止值的单元格)。一旦识别出这些单元格,您可以按自己的喜好对其进行格式化。我更喜欢将它们放入一个范围变量中,然后在最后一次对所有已识别的单元格进行格式化,这比在找到每个已识别的单元格时对其进行单独格式化要快得多。当您了解更多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
子作业7问题2()
'声明变量
将rngSales设置为“范围”以存储销售单元格
Dim SalesCell作为rngSales的“范围”循环变量
将rngBelowTarget设置为“范围”,以存储低于目标/截止值的单元格
Dim dTarget As Double'变量,用于存储用户输入的目标/截止值
'定义要检查的单元格
设置rngSales=范围(“B2:M41”)
'重置格式,以便仅格式化新目标下的单元格
与rngSales
.Font.Italic=False
.Font.Color=vbBlack
以
'使用Application.InputBox并将类型参数设置为1以强制输入一个数字
dTarget=Application.InputBox(“输入要使用的截止级别:”,“截止级别”,类型:=1)
如果dTarget=0,则退出Sub“用户按取消”
'验证是否存在任何匹配的单元格

如果工作表function.CountIf(rngSales,"先给我们一些代码怎么样?很抱歉,以前从未上过这个网站,这是一位朋友向我推荐的。这是原始问题。Sales.xlsx文件包含一家公司在给定月份的不同地区的销售数据。请编写一个宏,在地区8的销售月份将字体颜色改为蓝色s大于用户在输入框中提供的截止级别。例如,如果用户在输入框中输入10000,则宏应搜索区域8中销售额小于10000的月份。然后,宏应将单元格E9、F9、I9、J9、K9和L9转换为蓝色字体的斜体。Sub Homework 7Problem2()'键盘快捷键:Ctrl+b Dim CUPTION As String CUPTION=InputBox(“输入您要使用的截止级别”,“截止级别”)如果是范围(“B2:M41”)。单元格<截止级别,然后是范围(“B2:M41”).Cells.Font.Italic=选择为True。Font.Color=-4165632.TintAndShade=0结尾为End If End sub为什么这需要是VBA?您可以指定一个特定的单元格作为输入框,然后使用条件格式来实现这一点,没有问题,但是您应该先看看规则在StackOverflow上,它们很有用。此外,您应该编辑您的问题,在其中添加代码,并确保使用[code]标记:)我不再收到错误消息,但没有突出显示单元格!知道可能是什么问题吗?也非常感谢您的帮助!我为自己的无知道歉,除了自学的HTML和Wikipedia标记语言之外,我对任何代码都不太精通。可能是因为
截止
变量是一个字符串d B2:M41范围内的值是数字,因此由于数据类型不匹配,比较失败。@Crimsontide57是的,我得到了inputbox的标题,variablename搞乱了,现在应该可以修复了。我还确保我尝试将输入转换为
Long
。但是,如果谁输入数据,你仍然会遇到麻烦除了数字之外,还有别的东西。
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
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