如果然后直到最后一行

问题描述

我希望如果e列中的单元格不为空,但i列中的单元格为空,则在i列中注销或不写入i列中的内容

请帮助-我使用了以下代码

Sub Simple_If()

Dim lastrow As Long
 
lastrow = Cells(Rows.Count,"F").End(xlUp).Row

    If Range("e2:e" & lastrow).Value <> "" And Range("i2:i" & lastrow).Value = "" Then
        Range("i2:i" & lastrow).Value = "unregister"
    End If
       
End Sub

解决方法

您的代码无法正常运行的原因是,您无法获得.value.range)中的Range("e2:e" & lastrow).Value <> ""。而是使用for loop分别遍历每个单元格的值。

我已注释了以下代码的每一行,以帮助您了解正在发生的事情。

要执行此操作,请将SO.xlsm更改为工作簿的名称,将63649177更改为工作表的名称。

Sub Simple_If()
    Dim WB As Workbook                                                                  ' workbook      - full name of the file containing data.
    Dim WS As Worksheet                                                                 ' worksheet     - worksheet within  workbook containing data.
    Dim lRow As Long                                                                    ' last row      - find last row containing data
    Dim i As Long                                                                       ' iteration     - used for loop
        
    Set WB = Workbooks("SO.xlsm")                                                       ' set the name of the  workbook here
    Set WS = WB.Worksheets("63649177")                                                  ' set the name of the  worksheet here
    lRow = WS.Cells(WS.Rows.count,"E").End(xlUp).Row                                   ' find the last row of E in the WS object,not user defined.
    Set Rng = WS.Range("E2:E" & lRow)                                                   ' set the initial range
    
    For i = 2 To lRow                                                                   ' from line 2 to the last row,repeat this loop
        If WS.Range("E" & i).Value <> "" And WS.Range("I" & i).Value = "" Then          ' if E contains data and I does not then
            WS.Range("I" & i).Value = "unregister"                                      ' fill cell with "unregister"
        End If                                                                          ' end if
    Next                                                                                ' cycle through next iteration of loop
End Sub

输出

enter image description here

,

通过行循环

  • 您试图一次性检查两个范围"E2:E & LastRow""I2:I & LastRow"的值,但是您不能这样做。您必须遍历范围的行并检查每个单元格,即"E2","E3","E4" ... "E" & LastRow"I2","I3","I4" ... "I" & LastRow。对于此任务,可以使用For Next循环。
  • 第一个代码显示了如何使用Range完成它。
  • 第二个代码显示了如何使用带有Cells列字符串(字母)。
  • 第三个代码显示了如何使用带有Cells列号
  • 第四个代码显示了如何定义列范围(rng1rng2)以及如何将Cells与一个参数一起使用。
  • 第5个代码显示了如何定义常量来存储所谓的“魔术”字符,并在以后快速访问(更改)它们。还对其进行了修改,以能够更改结果列(tgtCol)。
  • Range似乎更容易,但是您也必须学习Cells,例如因为您不能使用Range遍历列,所以必须将列号与Cells一起使用。
  • 仔细研究前三个代码,您将很快了解它们之间的差异。

代码

Option Explicit

Sub fillSimpleRangeVersion()
    
    ' Calculate the last non-blank cell in column "F".
    Dim LastRow As Long
    LastRow = Range("F" & Rows.Count).End(xlUp).Row
    
    Dim i As Long
    ' Loop through the rows from 2 to LastRow.
    For i = 2 To LastRow ' i will change: "2,3,4 ... LastRow"
        ' Check that current cell in column "E" is not blank and
        ' that current cell in column "I" is blank:
        ' If not E2 blank and I2 blank then,' If not E3 blank and I3 blank then ...
        ' If not E & LastRow blank and I & LastRow blank then.
        If Not IsEmpty(Range("E" & i)) And IsEmpty(Range("I" & i)) Then
            ' If true,write "unregister" to current cell in column "I".
            Range("I" & i).Value = "unregister"
        ' The Else statement is not needed,because you only write when
        ' the condition is true.
        Else
            ' If not true,do nothing.
        End If
    Next i

End Sub

Sub fillSimpleCellsStringsVersion() ' Column Strings E,F,I
    
    Dim LastRow As Long
    LastRow = Cells(Rows.Count,"F").End(xlUp).Row
    
    Dim i As Long
    For i = 2 To LastRow
        If Not IsEmpty(Cells(i,"E")) And IsEmpty(Cells(i,"I")) Then
            Cells(i,"I").Value = "unregister"
        End If
    Next i

End Sub

Sub fillSimpleCellsNumbersVersion() ' Column Numbers 5,6,9
    
    Dim LastRow As Long
    LastRow = Cells(Rows.Count,6).End(xlUp).Row
    
    Dim i As Long
    For i = 2 To LastRow
        If Not IsEmpty(Cells(i,5)) And IsEmpty(Cells(i,9)) Then
            Cells(i,9).Value = "unregister"
        End If
    Next i

End Sub

Sub fillSimpleCellsVersionWithRanges()

    Dim LastRow As Long
    LastRow = Cells(Rows.Count,"F").End(xlUp).Row
    
    Dim rng1 As Range
    Set rng1 = Range("E2:E" & LastRow)
    Dim rng2 As Range
    Set rng2 = Range("I2:I" & LastRow)
    
    Dim i As Long
    For i = 1 To rng1.Rows.Count
        If rng1.Cells(i).Value <> "" And rng2.Cells(i).Value = "" Then
            rng2.Cells(i).Value = "unregister"
        End If
    Next i
            
End Sub


Sub fillSimpleCellsExpanded()
    
    Const FirstRow As Long = 2          ' First Row
    Const LastRowCol As Variant = "F"   ' The column to Calculate Last Row
    Const Col1 As Variant = "E"         ' Column 1
    Const Col2 As Variant = "I"         ' Column 2
    Const tgtCol As Variant = "I"       ' Target Column,the Column to Write to
    ' You want to write to the same column "CritCol2 = tgtCol",but if you
    ' want to write to another column,you can easily change "tgtCol".
    Const Criteria As String = "unregister"  ' Write Criteria
    
    Dim LastRow As Long
    LastRow = Cells(Rows.Count,LastRowCol).End(xlUp).Row
    
    Dim i As Long
    For i = FirstRow To LastRow
        If Not IsEmpty(Cells(i,Col1)) And IsEmpty(Cells(i,Col2)) Then
            Cells(i,tgtCol).Value = Criteria
        Else
            ' The following line is only needed if "CritCol2" is different
            ' than "tgtCol".
            Cells(i,tgtCol).Value = Cells(i,Col2).Value
        End If
    Next i

End Sub