Почему это IF / ELSE не соответствует? - PullRequest
0 голосов
/ 10 октября 2018

Мне нужно переформатировать строки, в которых есть строка «Без действия» в столбце C

Я использую это:

Dim ActionRow as Long


For ActionRow = 2 To 50
    If .Cells(ActionRow, 3).Value = "No Action" Then
        .Range("A" & ActionRow & ":AB" & ActionRow).Font.Italic = True
        .Range("A" & ActionRow & ":AB" & ActionRow).Font.Color = 8421504
    End If
Next ActionRow

Вместо того, чтобы просто нацеливать строки с «Без действия»в столбце C это предназначается для каждой строки, или отслеживает это непоследовательно.Как я могу сказать, чтобы только найти строку «Без действия» в столбце C (3), а затем выполнить форматирование?

Остальная часть кода:

Option Explicit

    Sub Main()
      Dim Wb As Workbook
      Dim Data, Last, BU7, Lvl7
      Dim sourcerow As Long, sourcecol As Long, destrow As Long, destcol As Long
      Dim rngDest As Range
      Dim ActionRow As Long


      'Refer to the template
      Set Wb = Workbooks("Book1.xlsx")
      'Refer to the destination cell
      Set rngDest = Wb.Sheets("Sheet1").Range("A2")
      'Read in all data
      With ThisWorkbook.Sheets("Data_")
        Data = .Range("Ab2", .Range("A" & Rows.Count).End(xlUp))
      End With
      Wb.Activate
      Application.ScreenUpdating = False

      'Process the data
      For sourcerow = 1 To UBound(Data, 1)
        'Manager changes?
        If Data(sourcerow, 15) <> Last Then
          'Skip the first
          If sourcerow > 1 Then

            'Scroll into the view
            rngDest.Select
            'Save a copy
            Wb.SaveCopyAs ThisWorkbook.Path & Application.PathSeparator & _
              ValidFileName("10.08.18" & " - " & BU7 & " - " & Lvl7 & " - " & Last & ".xlsx")
          End If
          'Clear the employees

          ActiveSheet.Range("A2:AB" & ActiveSheet.Columns.Count + 1).ClearContents
          'Remember this manager

          Last = Data(sourcerow, 15)
          BU7 = Data(sourcerow, 18)
          Lvl7 = Data(sourcerow, 25)

          'Start the next round
          destcol = 0
        End If
        'Write the employee data into the template
            destrow = 0
            For sourcecol = 1 To UBound(Data, 2)
                If sourcecol = 1 Then
                rngDest.Offset(destcol, destrow) = CStr(Format(Data(sourcerow, sourcecol), "000000000"))
                    Else
                    rngDest.Offset(destcol, destrow) = Data(sourcerow, sourcecol)
                End If

                    destrow = destrow + 1
            Next

        'Next column
        destcol = destcol + 1

        Next
                With Wb.Worksheets("Sheet1")

                    For ActionRow = 2 To 50
                            If .Cells(ActionRow, 3).Value = "No Action" Then
                                .Range("A" & ActionRow & ":AB" & ActionRow).Font.Italic = True
                                .Range("A" & ActionRow & ":AB" & ActionRow).Font.Color = 8421504
                            End If
                    Next ActionRow

                .Columns("A:ab").Sort key1:=Range("c2"), _
                order1:=xlAscending, Header:=xlYes

        .Columns("A:A").NumberFormat = "000000000"


      End With

    End Sub

Ответы [ 2 ]

0 голосов
/ 10 октября 2018

Я буду работать с .findnext так:

With sheets(youractionsheet).Range(“A2:AB” & sheets(youractionsheet).cells(rows.count,3).end(xlup).row)
 Set myaction = .cells.find(what:=“No Action”)

If not myaction is nothing then 
 Do until myaction is nothing
 myaction.font.italic = true
 myaction.font.color = 8421504

 Set myaction = .findnext(myaction)

 Loop
End if

End with 
0 голосов
/ 10 октября 2018

Как насчет следующего, вместо использования цикла For, приведенный ниже код будет автоматически фильтровать столбец C со значением «No Action», затем он будет выделять курсивом и окрашивать каждую видимую строку от столбца A до AB:

With WB.Worksheets("Sheet1")
    .Cells.AutoFilter 'add AutoFilter
    .Range("$A$1:$AB$50").AutoFilter Field:=3, Criteria1:="No Action"
    .Range("$A$2:$AB$50").SpecialCells(xlCellTypeVisible).Font.Italic = True
    .Range("$A$2:$AB$50").SpecialCells(xlCellTypeVisible).Font.Color = 8421504
    .Cells.AutoFilter 'remove AutoFilter
End With

ОБНОВЛЕНИЕ:

Чтобы проверить значение «Нет действия» в столбце C перед использованием автофильтра, вы можете сделать что-то вроде следующего:

With WB.Worksheets("Sheet1")
    Set FoundNoAction = .Range("C:C").Find(What:="No Action", Lookat:=xlWhole)
    If Not FoundNoAction Is Nothing Then
        .Cells.AutoFilter 'add AutoFilter
        .Range("$A$1:$AB$50").AutoFilter Field:=3, Criteria1:="No Action"
        .Range("$A$2:$AB$50").SpecialCells(xlCellTypeVisible).Font.Italic = True
        .Range("$A$2:$AB$50").SpecialCells(xlCellTypeVisible).Font.Color = 8421504
        .Cells.AutoFilter 'remove AutoFilter
    End If
End With
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...