首页 > 解决方案 > 在excel中用特定颜色填充偶数行

问题描述

我需要在我的工作表中有交替行是特定颜色 RGB(142、169、219)。我使用了 =MOD(ROW(),2)=0 但我的代码在运行时的方式是 .clear 它会删除这些行中的颜色。我试图在.clear 之后和ActiveWorkbook.Save 之前重新应用它的代码。此宏仅在月份更改为下个月时删除,它设置为在打开 excel 文件时运行和检查。我不想在 G1 的电子表格中使用 mthNum 来检查当前月份,但我对 VBA 还是很陌生,想不出其他方法。宏

Sub MakeNewSheet()
Dim mthNum As Integer
Dim name As String
todaydate = Format(Date, "MM/DD/YYYY")
mthNum = Month(todaydate)
    name = MonthName(mthNum)
    yearNum = Year(todaydate)
    pdfname = "Jobs_" & name & yearNum
If Range("G1").Value = "" Then Range("G1") = mthNum
    If Range("G1") <> mthNum Then
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:="C:\Users\******\Documents\*******\Desktop\" & pdfname
    ThisWorkbook.Sheets("Sheet1").Range("A2:F3000").Clear
    ThisWorkbook.Sheets("Sheet1").Range("G1").Value = mthNum
    ActiveWorkbook.Save
End If
End Sub

工作表

Dim mthNum As Integer
todaydate = Format(Date, "MM/DD/YYYY")
mthNum = Month(todaydate)

If Selection.Count = 1 Then
            If Not Intersect(Target, Range("G1")) Is Nothing Then
            MsgBox ("This Number Cannot Be Changed!")
            Target.Offset(1, -6).Activate
        End If
        End If
        Dim nPut As String
        If ThisWorkbook.Sheets("Sheet1").Range(Cells(Selection.Row, 4).Address) <> "" Then GoTo 16

        If Selection.Count = 1 Then
            If Not Intersect(Target, Range("D:D")) Is Nothing Then
            nPut = InputBox("Please Scan Part Number")
            If nPut <> "" Then
            Target.Value = nPut
            Target.Offset(0, 1).Activate
            End If
            End If
            End If
  Dim nPut2 As String
16            If ThisWorkbook.Sheets("Sheet1").Range(Cells(Selection.Row, 5).Address) <> "" Then Exit Sub
                 If ThisWorkbook.Sheets("Sheet1").Range(Cells(Selection.Row, 4).Address) = "" Then Exit Sub
                    If Selection.Count = 1 Then
                        If Not Intersect(Target, Range("E:E")) Is Nothing Then

            nPut2 = InputBox("Please Scan Badge")

                    If nPut2 <> "" Then
                    Target.Value = nPut2
            If ThisWorkbook.Sheets("Sheet1").Range(Cells(Selection.Row, 6).Address) = "" Then ThisWorkbook.Sheets("Sheet1").Range(Cells(Selection.Row, 6).Address) = todaydate

            Target.Offset(1, -4).Activate

                End If
            Else
            Exit Sub
            End If
        End If

  End Sub

标签: excelvbaoffice365

解决方案


推荐阅读