【发布时间】:2021-06-27 10:11:11
【问题描述】:
我创建了下面的代码,其工作方式类似于 IF Col"B" 任何单元格 <> "" 和 Col"L" 任何单元格 = "Leop" 然后将下面的行添加到活动单元格。
我的意思是我想要实现的是在 B 列中包含任何值的特定行之后插入单行,并且如果同一行中的 L 列包含 value =“Leop”。然后在该行之后添加行。
但出现错误。 Compile Error: Invalid use of property xlDown
您的帮助将不胜感激。
从这里:
到这里:
Sub firstcondition()
Dim ws As Worksheet
Dim LRow As Long
Dim rng As Range
Dim rng2 As Range
Dim i As Long
Dim p As Long
Dim dat As Variant
Dim datt As Variant
Dim IRow As Long
Set ws = Thisworkbooks.Sheets("Sheet2")
With ws
LRow = .Range("A" & .Rows.Count).End(xlUp).Row
Set rng = .Range("B2:B" & LRow)
Set rng2 = .Range("L2:L" & LRow)
dat = rng
datt = rng2
IRow = Selection.Row
For i = LBound(dat, 1) To UBound(dat, 1)
For p = LBound(datt, 1) To UBound(datt, 1)
If dat(i, 1) <> "" And datt(p, 1) = "Leop" Then
Rows(IRow + 1).Select
Selection.Insert Shift: xlDown
End If
End Sub
它会像公式:
IF(AND(B2<>"",L2="Leop"),"InsertRowBelow to Row 2 If condition is met","")
并将其拖到最后一行。
【问题讨论】: