Excel 如果一直到最后一排
我希望,若e列中的单元格不是空的,但I列中的单元格是空的,那个么在I列中写unregister,或者写I列中曾经写过的内容 请帮助-我使用了以下代码:Excel 如果一直到最后一排,excel,vba,arguments,Excel,Vba,Arguments,我希望,若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
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
Sub-Simple_If()
最后一排一样长
lastrow=单元格(Rows.Count,“F”).End(xlUp).Row
如果范围(“e2:e”和lastrow.Value”)和范围(“i2:i”和lastrow.Value=”“,则
范围(“i2:i”和lastrow)。Value=“注销”
如果结束
端接头
代码不起作用的原因是无法获取范围的.value
(范围(“e2:e”和lastrow).value”“
)。相反,使用for循环
逐个迭代每个单元格值
我已经对下面的每一行代码进行了注释,以帮助您理解正在发生的事情
若要执行此操作,请将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
Sub-Simple_If()
Dim WB As工作簿工作簿-包含数据的文件的全名。
Dim WS-As工作表的工作表-工作簿中包含数据的工作表。
Dim lRow As Long“最后一行-查找包含数据的最后一行
Dim i As Long“迭代-用于循环
Set WB=Workbooks(“SO.xlsm”)'在此处设置工作簿的名称
Set WS=WB.Worksheets(“63649177”)'在此处设置工作表的名称
lRow=WS.Cells(WS.Rows.count,“E”).End(xlUp).Row'查找WS对象中E的最后一行,而不是用户定义的。
设置Rng=WS.Range(“E2:E”&lRow)”设置初始范围
对于从第2行到最后一行的i=2到lRow’,重复此循环
如果WS.Range(“E”&i.Value”)和WS.Range(“i”&i.Value=”“,则“如果E包含数据而我不包含数据,则”
WS.Range(“I”和I.Value=“unregister”用“unregister”填充单元格
如果结束“如果结束”
“下一个”循环通过循环的下一个迭代
端接头
输出
在行中循环
- 您试图一次检查两个范围的值
“E2:E&LastRow”
和“I2:I&LastRow”
,但您不能这样做。您必须循环遍历范围行并检查每个单元格,即“E2”、“E3”、“E4”。。。“E”&LastRow
和“I2”、“I3”、“I4”。。。“I”和LastRow
。对于此任务,可以使用循环
单元格
rng1
,rng2
),并使用带有一个参数的单元格
tgtCol
)范围
似乎更容易,但您也必须学习单元格
,例如,因为您无法使用范围
循环列,所以必须将列编号与单元格
一起使用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
选项显式
子fillSimpleRangeVersion()
'计算“F”列中最后一个非空白单元格。
最后一排一样长
LastRow=范围(“F”和Rows.Count).End(xlUp).Row
我想我会坚持多久
'循环浏览从2到最后一行的行。
对于i=2到最后一行,我将更改:“2,3,4…最后一行”
'检查列“E”中的当前单元格是否为空,以及
'列“I”中的当前单元格为空:
'如果不是E2空白和I2空白,
'如果不是E3 blank和I3 blank,则。。。
'如果不是E&LastRow blank和I&LastRow blank,则为。
如果不是IsEmpty(范围(“E”)和IsEmpty(范围(“i”)和i)),则
'如果为true,则将“注销”写入“I”列中的当前单元格。
范围(“I”和I).Value=“注销”
'不需要Else语句,因为您只在
"情况属实。
其他的
“如果不是真的,什么也不做。
如果结束
接下来我
端接头
Sub-fillSimpleCellsStringsVersion()'列字符串E、F、I
最后一排一样长
LastRow=单元格(Rows.Count,“F”).End(xlUp).Row
我想我会坚持多久
对于i=2到最后一行
如果不是IsEmpty(单元格(i,“E”)和IsEmpty(单元格(i,“i”)),那么
单元格(i,“i”).Value=“取消注册”
如果结束
接下来我
端接头
子FillSimpleCellsMembersVersion()'列号5、6、9
最后一排一样长
LastRow=单元格(Rows.Count,6).End(xlUp).Row
我想我会坚持多久
对于i=2到最后一行
如果不是等空(单元格(i,5))和等空(单元格(i,9)),则
单元格(i,9)。Value=“取消注册”
如果结束
接下来我
端接头
子填充SimpleCellsVersionWithRanges()
最后一排一样长
LastRow=单元格(Rows.Count,“F”).End(xlUp).Row
变暗rng1 As范围
设置rng1=范围(“E2:E”和最后一行)
变暗rng2 As范围
设置rng2=Ran