【问题标题】:Transferring Cell Values Between Worksheets | Str Looper在工作表之间传输单元格值 | Str Looper
【发布时间】:2018-04-16 12:14:04
【问题描述】:

预期结果

  • 如果表中的一行包含 Sheet1 的 L 列中列出的任何字符串,则从 Sheet1 复制整行并将该行粘贴到 Sheet2 上的重复表中(开头为空白)。 (不感兴趣、不相关、未决定等...)
  • 然后删除从工作表 1 转移的整行。
  • 宏运行后,新传输不应重置 Sheet2 上的表,而是在预先存在的行上添加行。该文件将在数月内使用。

变量

  • Sheet1 被命名为 Pipeline_Input
  • Sheet2 被命名为 Closed_Sheet
  • Sheet1 表名为 tblData
  • Sheet2 表名为 tblClosed

图片

  • 图 1 是有错误的代码
  • 图 2 是表 1,带有一些图片说明
  • 图 3 是表 2,带有一些图片说明

当前结果 运行时错误“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


【解决方案1】:

好的,您发布的代码存在大量问题,但我决定在这里为您提供帮助 - 请注意一些事项 - 这里没有复制和粘贴 - 我们只是在传输数据。

其次,使用易于理解的变量。 lr 和 lngLastRow 无法相互区分,因此请按照您从哪个工作表中获取该值对它们进行分类。

我们在这里一举创建了一个数组 - 只需声明一个变体并将我们的值放入其中。数组(通常)从零开始,而不是一,所以我们的循环从 0 开始:)。同样,这就是所谓的最佳实践...

我将Looper 换成了j。再次,保持。它。简单!

编辑:我在模拟工作簿上测试了这段代码,它运行良好 - 对你来说也不应该遇到任何问题。

EDIT2:另外,请始终使用Option Explicit!

Option Explicit
Sub closedsheet()
Application.ScreenUpdating = False

Dim Pipeline_Input As Worksheet 'source sheet
Dim Closed_Sheet As Worksheet 'destination sheet

Dim i As Long, j As Long, CSlastrow As Long, PIlastrow As Long

Dim strPhase As Variant

'Here we create our array
strPhase = Array("LOST", "BAD", "UNINTERESTED", "UNRELATED", "UNDECIDED", "BUDGET")

'Assign worksheets
Set Pipeline_Input = ActiveWorkbook.Worksheets("Pipeline_Input")
Set Closed_Sheet = ActiveWorkbook.Worksheets("Closed_Sheet")

PIlastrow = Pipeline_Input.Range("A" & Rows.Count).End(xlUp).Row

For j = 0 To UBound(strPhase)
    For i = PIlastrow To 6 Step -1
        If Pipeline_Input.Range("L" & i).Value = strPhase(j) Then

            'Refresh lastrow value
            CSlastrow = Closed_Sheet.Range("A" & Rows.Count).End(xlUp).Row

            'Transfer data
            Closed_Sheet.Range("A" & CSlastrow + 1 & ":S" & CSlastrow + 1).Value = _
            Pipeline_Input.Range("A" & i & ":S" & i).Value

            'Delete the line
            Pipeline_Input.Range("A" & i & ":S" & i).EntireRow.Delete

        End If
    Next i
Next j

Closed_Sheet.Select
Closed_Sheet.Columns.AutoFit
Application.ScreenUpdating = True
End Sub

【讨论】:

  • 数组并不总是START AT ZERO 在这种情况下是的,但是直接从一个范围内分配值,不,它从 1 开始。它也是一个设置,如果你可以从 1 开始你的数组你想要的。
  • @ScottCraner 是的,你是对的。我只是觉得他使用 ReDim 并选择 1-6 很愚蠢,而我认为最佳做法是 ReDim 0 到 5。
  • @dwirony 难以置信。我有另一个工作表更改在我的工作表中处于活动状态,这会导致问题,但我会先用它重新制作它,看看我是否能让两者共存。在稍微摆弄之后,我会给出一个解决的标记。我真的很感激!
  • @dwirony 可以确认提供的代码在运行时会传输数据,但不会将其添加到表中。它将数据推到底部,但最终需要在 tblClosed 内部。将其添加到表格中的任何简单更改?此外,您认为这也可以分配给按钮吗?让我知道我是否应该创建一个单独的问题,因为这是技术上的回答。
  • @Kobetron “在 tblClosed 内部”是什么意思?不知道那是什么-
猜你喜欢
  • 2015-09-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-16
  • 2020-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多