gpt4 book ai didi

vba - 如何制作条件 IF 语句以调用重新格式化模块

转载 作者:行者123 更新时间:2023-12-04 22:31:05 25 4
gpt4 key购买 nike

我有一个脚本,它根据我的原始数据中的值收集一个数组,但是该数组中的某些行需要是灰色字体和斜体。

我该怎么做:

Dim strAction As String

Dim ActionRow As Long

Dim colAction As Long
colAction = 3

For ActionRow = 2 to ThisWorkbook.Worksheets("Sheet1").UsedRange.Rows.Count

if ThisWorkbook.Worksheets("Sheet1").Cells(ActionRow, 3).Value = "No Action" Then

Call Row_Reformat

End If

Next

释义,在任何当前的新工作表上(实际工作表名称可能是工作表 54,所以我需要它来仅引用当前工作表)我在上,如果 C 列中的一行具有值 =“无操作”然后调用一个模块我用来将整行重新格式化为斜体和灰色字体

编辑:

我尝试了以下方法,当我输入时它可以工作:
2 to 10' To Wb.Worksheet("Sheet1").UsedRange.Rows.Count

但这不起作用:
For ActionRow = 2 To Wb.Worksheet("Sheet1").UsedRange.Rows.Count
If .Cells(ActionRow, 3).Value = "No Action" Then
.Range("A" & ActionRow & ":AB" & ActionRow).Font.Italic = True
.Range("A" & ActionRow & ":AB" & ActionRow).Font.Color = 10921638
End If
Next ActionRow

最佳答案

如果这些是您正在采取的唯一行动,我的方法是在没有第二个 sub 的情况下立即格式化行。我的偏好是仅在 Action 范围大时将参数传递给其他潜艇。这就是我将如何在一个潜艇中执行此操作。

选项1

Sub Tester()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim i As Long

For i = 2 To ws.Range("C" & ws.Rows.Count).End(xlUp).Row
If ws.Range("C" & i) = "No Action" Then
ws.Range("C" & i).EntireRow.Font.Italic = True
ws.Range("C" & i).EntireRow.Interior.Color = "INSERT COLER NUMBER HERE"
Else
'Do what you want if the value is NOT "No Action"
End If
Next i

End Sub

选项 2

如果您的偏好是将您的范围传递给一个新的子程序,该子程序专门用于处理显示 No Action 的范围的操作语句,你需要实现这样的东西:
Sub LoopSub()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim i As Long

For i = 2 To ws.Range("C" & ws.Rows.Count).End(xlUp).Row
If ws.Range("C" & i) = "No Action" Then
No_Action ws.Range("C" & i)
Else
'Do what you want if the value is NOT "No Action"
End If
Next i

End Sub
Sub No_Action(Target As Range)

With Target
.EntireRow.Font.Italic = True
.EntireRow.Interior.Color = "INSERT COLOR NUMBER HERE"
End With

End Sub

关于vba - 如何制作条件 IF 语句以调用重新格式化模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52650449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com