[英]if then till last row
I want that if cell in column e is not blank but cell in column i is blank then write unregister in column i or else write what ever written in column i.我希望如果 e 列中的单元格不为空而 i 列中的单元格为空,则在 i 列中写入 unregister 或在 i 列中写入曾经写过的内容。
Please help - I have used below code:请帮忙 - 我使用了以下代码:
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
The reason your code was not working is because you can't get .value
of a .range
( Range("e2:e" & lastrow).Value <> ""
).您的代码无法正常工作的原因是您无法获得.range
.value
( Range("e2:e" & lastrow).Value <> ""
)。 Instead, use a for loop
to iterate through each cells value individually.相反,使用for loop
单独迭代每个单元格值。
I have commented each line of the code below to help you understand what is happening.我已经注释了下面的每一行代码,以帮助您了解正在发生的事情。
To make this work, change SO.xlsm
to the name of your workbook and 63649177
to the name of your worksheet.为了使这项工作,改变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
Output输出
"E2:E & LastRow"
and "I2:I & LastRow"
in one go, but you cannot do that.您试图"E2:E & LastRow"
检查两个范围"E2:E & LastRow"
和"I2:I & LastRow"
,但您不能这样做。 You have to loop through the rows of the ranges and check each cell ie "E2", "E3", "E4" ... "E" & LastRow
and "I2", "I3", "I4" ... "I" & LastRow
.您必须遍历范围的行并检查每个单元格,即"E2", "E3", "E4" ... "E" & LastRow
和"I2", "I3", "I4" ... "I" & LastRow
。 For this task a For Next loop can used.对于此任务,可以使用For Next循环。Cells
.第三个代码显示了如何使用列号和Cells
来完成。rng1
, rng2
) and use Cells
with one parameter.第四个代码显示了如何定义列范围( rng1
、 rng2
)并使用带有一个参数的Cells
。tgtCol
).它还被修改为能够更改结果列 ( tgtCol
)。Range
might seem easier, but you have to learn Cells
, too, eg because you cannot loop through columns using Range
, you have to use column numbers with Cells
. Range
可能看起来更容易,但您也必须学习Cells
,例如因为您不能使用Range
遍历列,您必须将列号与Cells
一起使用。The Code编码
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.