为什么这个IF / ELSE不一致?

时间:2018-10-09 21:02:49

标签: excel vba excel-vba

我需要重新格式化C列中包含字符串“ No Action”的行

我使用这个:

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)中找到字符串“ No Action”,然后执行格式化?

其余代码:

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 :(得分:0)

接下来的事情,下面的代码将使用“ No Action”值自动过滤C列,而不是使用For循环,然后它将使从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

答案 1 :(得分:0)

我将像下面那样使用.findnext:

def remove_stop_words(lines):
  stop_words = ['am', ':']
  results = []
  for text in lines:
    tmp = text.split(' ')
    for x in range(0, len(tmp)):
      for st_w in stop_words:
        if st_w in tmp[x]:
          tmp[x] = ''
    results.append(" ".join(tmp))
  return results