在工作表之间传输单元格值| Str Looper

预期结果

  • 如果表中的行包含Sheet1上列L中列出的任何string,则复制Sheet1中的整个行,并将该行粘贴到Sheet2上的一个重复表(在开始处将为空白)。 (不间断的,不相关的,未被遵守的等等)
  • 然后删除从表单1传输的整个行。
  • macros运行后,新的传输不应该重置Sheet2上的表,而是在预先存在的行上添加行。 这个文件将被利用几个月。

variables

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

图片

  • 图片1是错误的代码 码
  • 图片2是图片1,附有图片说明 工作表Sheet1
  • 图片3是图片2,附有图片说明 Sheet2中

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

好的,你发布的代码有太多的问题,但是我决定在这里帮助你 – 注意一些事情 – 这里没有复制和粘贴 – 我们只是传输数据。

其次,使用易于理解的variables。 lrlngLastRow不能相互区分,因此按照哪个工作表将其分类。

我们在这里一举创build一个数组 – 只需声明一个变体并将我们的值放入。 数组(从零开始),而不是一个 ,所以我们的循环从0开始。 再次,这就是所谓的最佳做法

j换了Looper 。 再次保持。 它。 简单!

编辑:我testing了这个代码在一个模拟的工作簿,它工作正常 – 应该遇到没有问题为你。

编辑2:另外,总是使用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