簡體   English   中英

如果然后直到最后一行

[英]if then till last row

我希望如果 e 列中的單元格不為空而 i 列中的單元格為空,則在 i 列中寫入 unregister 或在 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

您的代碼無法正常工作的原因是您無法獲得.range .valueRange("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

輸出

在此處輸入圖片說明

循環遍歷行

  • 您試圖"E2:E & LastRow"檢查兩個范圍"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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM