我想做什么:
从Salesforce导出excel doc。我试图根据条件删除一些行,然后根据一组不同的条件添加行。 作为VBA的新手,我浏览了论坛并搜索了一些示例代码,但无法让它们完全正常工作。
添加行:
C列=数量。需要根据C列中的值添加下面的行。如果quantity = 3 - 我想在下面添加2个空行。如果数量= 4 - 下面有3个空白行......
尝试:
Sub BlankLine()
Dim Col As Variant
Dim BlankRows As Long
Dim LastRow As Long
Dim R As Long
Dim StartRow As Long
Col = "C"
StartRow = 15
BlankRows = 1
LastRow = Cells(Rows.Count, Col).End(xlUp).Row
Application.ScreenUpdating = False
With ActiveSheet
For R = LastRow To StartRow + 1 Step -1
If .Cells(R, Col) > 1 Then
.Cells(R + 1, Col).EntireRow.Insert Shift:=xlDown
End If
Next R
End With
Application.ScreenUpdating = True
End Sub
这样可行,但只在下面添加一行 - 我需要根据列C ='quantity'的值添加行。
删除行:
A列=产品名称。如果任何产品名称包含MAINT或APP,我需要删除该行。尝试:
Sub test()
With ActiveSheet
.AutoFilterMode = False
With Range("A1", Range("A" & Rows.Count).End(xlUp))
.AutoFilter 1, "*Maint*"
On Error Resume Next
.Offset(1).SpecialCells(12).EntireRow.Delete
End With
.AutoFilterMode = False
End With
End Sub
这也有效,但一旦完成就会删除顶行,所以我丢失了一些信息。
非常感谢任何有关如何处理此问题的建议!
答案 0 :(得分:0)
If .Cells(R, Col) > 1 Then
numRows = val(.Cells(R, Col)) - 1
For i = 1 to numRows
.Cells(R + 1, Col).EntireRow.Insert Shift:=xlDown
Next i
End If
答案 1 :(得分:0)
至于插入行
For R = LastRow To StartRow + 1 Step -1
With .Cells(R, Col)
If .Value > 1 Then .Offset(1).Resize(.Value).EntireRow.Insert
End With
Next R
用于过滤和删除
Sub test()
With ActiveSheet
.AutoFilterMode = False
With .Range("A1", .Range("A" & .Rows.Count).End(xlUp))
.AutoFilter Field:=1, Criteria1:=Array("*Maint*", "*APP*"), Operator:=xlFilterValues
If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then .Offset(1).Resize(.Rows.Count -1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
.AutoFilterMode = False
End With
End Sub