【发布时间】:2018-09-03 14:06:56
【问题描述】:
今天刚开始学习 VBA,试图让我在新工作中的生活更轻松。我实际上是在尝试查找列 E 具有字母“a”的每个实例,并将其粘贴到新创建的名为“Aton”的工作表中,然后删除带有“a”s 的原始行。
我尝试修改此处找到的解决方案:VBA: Copy and paste entire row based on if then statement / loop and push to 3 new sheets
当我将上述解决方案更改为“If wsSrc.Cells(i, "E").Value = "a" Then" 时,我遇到了问题。
Sub Macro3()
'Need "Dim"
'Recommend "Long" rather than "Integer" for referring to rows and columns
'i As Integer
Dim i As Long
'Declare "Number"
Dim Number As Long
'Declare a variable to refer to the sheet you are going to copy from
Dim wsSrc As Worksheet
Set wsSrc = ActiveSheet
'Declare a variable to refer to the sheet you are going to copy to
Dim wsDest As Worksheet
'Declare three other worksheet variables for the three potential destinations
Dim wsEqualA As Worksheet
'Create the three sheets - do this once rather than in the loop
Set wsEqualA = Worksheets.Add(After:=Worksheets(Worksheets.Count))
'Assign the worksheet names
wsEqualA.Name = "Aton"
'Determine last row in source sheet
Number = wsSrc.Cells(wsSrc.Rows.Count, "C").End(xlUp).Row
For i = 1 To Number
'Determine which destination sheet to use
If wsSrc.Cells(i, "E").Value = "a" Then
Set wsDest = wsEqualA
Else
End If
'Copy the current row from the source sheet to the next available row on the
'destination sheet
With wsDest
wsSrc.Rows(i).Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
End With
'Delete row if column E has an a
If wsSrc.Cells(i, "E").Value = "a" Then
Selection.EntireRow.Delete
Else
End If
Next i
End Sub
【问题讨论】:
-
您共享的代码与您要执行的操作的描述不匹配。