【发布时间】:2018-04-16 12:14:04
【问题描述】:
预期结果
- 如果表中的一行包含 Sheet1 的 L 列中列出的任何字符串,则从 Sheet1 复制整行并将该行粘贴到 Sheet2 上的重复表中(开头为空白)。 (不感兴趣、不相关、未决定等...)
- 然后删除从工作表 1 转移的整行。
- 宏运行后,新传输不应重置 Sheet2 上的表,而是在预先存在的行上添加行。该文件将在数月内使用。
变量
- Sheet1 被命名为 Pipeline_Input
- Sheet2 被命名为 Closed_Sheet
- Sheet1 表名为 tblData
- Sheet2 表名为 tblClosed
图片
当前结果 运行时错误“1004”: 应用程序定义或对象定义的错误
Sub closedsheet()
Application.ScreenUpdating = False
Dim Pipeline_input As Worksheet 'where is the data copied from
Dim Closed_Sheet As Worksheet 'where is the data pasted to
Dim strPhase() As String
Dim i As Integer
Dim intPhaseMax As Integer
Dim lngLstRow As Long
Dim rngCell As Range
Dim finalrow As Integer
Dim lr As Long 'row counter
Dim Looper As Integer
intPhaseMax = 6
ReDim strPhase(1 To intPhaseMax)
strPhase(1) = "LOST"
strPhase(2) = "BAD"
strPhase(3) = "UNINTERESTED"
strPhase(4) = "UNRELATED"
strPhase(5) = "UNDECIDED"
strPhase(6) = "BUDGET"
'set variables
Set Pipeline_input = Sheet1
Set Closed_Sheet = Sheet2
lr = Range("A" & Rows.Count).End(xlUp).Row
For Looper = LBound(strPhase) To UBound(strPhase)
For i = lr To 6 Step -1
Next
If Not Sheet1.Range("L9:L300" & lngLstRow).Find(strPhase(Looper), lookat:=xlWhole) Is Nothing Then
Range(Cells(i, 1), Cells(i, 20)).Copy
Sheet2.Range("A" & Rows.Count).End(3)(2).PasteSpecial xlPasteValues
Range(Cells(i, 1), Cells(i, 20)).Delete
End If
Next
Sheet2.Select
Sheet2.columns.AutoFit
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
【问题讨论】:
-
您在下一个循环中有一个不合格的
Range()和Cells()。确保将工作表名称放在这两个之前(或至少参考,即Sheet2.Range(Sheet2.Cells()...)...另外,为什么你有For行紧跟Next?那个循环有什么意义? -
@BruceWayne 如果您指的是
For i = lr To 6 Step -1,那是因为如果我删除 For 和 Next,我会收到“编译错误:预期:语句结束”。关于对 Sheet1 和 Sheet2 的引用,我不再收到您的修复错误,但代码没有做任何事情。它似乎没有检查我的字符串的列 -
@Kobetron 你的
Next放错了位置。它应该在您的End If之后。 -
@dwirony 代码更改为
For i = lr To 6 Step -1 If Not Sheet1.Range("L9:L300" & lngLstRow).Find(strPhase(Looper), lookat:=xlWhole) Is Nothing Then Sheet1.Range(Cells(i, 1), Cells(i, 20)).Copy Sheet2.Range("A" & Rows.Count).End(3)(2).PasteSpecial xlPasteValues Sheet1.Range(Cells(i, 1), Cells(i, 20)).Delete End If Next Sheet2.Select Sheet2.columns.AutoFit Application.CutCopyMode = False Application.ScreenUpdating = True在突出显示 Sub closedsheet() 时收到新错误为“编译错误:For without Next”
标签: excel string vba find looper