我有一张工作表,其范围为A12:N112,根据更改的标准,列A是我的触发列(1或)。我的宏的第一位对该范围进行排序,所有具有1的行都位于该范围的顶部。然后,它也会打开目标表。
下面的代码的下一部分,需要为每行复制单元格B:L,并在A列中将其粘贴为1,然后将其粘贴到目标工作表中从D列开始的第一行中。然后复制并粘贴回该特定行的M列中的第一张工作表中。然后需要循环执行,直到处理完A列中所有带有1的行为止。
任何人都可以帮忙,这是我的代码,该代码可以运行,但是没有任何复制或粘贴。
Dim lr As Long lr = Sheets("Data Entry").Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step 1
If Range("AB" & r).Value = "1" Then
Rows(r).Copy.Range ("A" & lr2 + 1)
Windows("Serialisation Log.xlsx").Activate
Sheets("SNo Log").Select
Range("D" & Rows.Count).End(xlUp).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("Serialisation Log.xlsx").Activate
Sheets("SNo Log").Select
Range("A" & Rows.Count).End(xlUp).Offset(-1).Select
Selection.Copy
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
ActiveSheet.Paste
Range("A" & Rows.Count).End(xlUp).Offset(0).Select
Selection.Copy
Windows("Serialisation Generator rev 1.xlsm").Activate
Worksheets("Data Entry").Select
Range("N").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
If Range("AB" & r).Value = "0" Then
Range("I4").Select
ActiveCell.FormulaR1C1 = "Serial No. Issue complete for this OA"
End If
Range("F5").Select
Next r
任何帮助将不胜感激。
答案 0 :(得分:0)
好吧,让您走上正确的路:)
我强烈建议您在VBA编辑器中使用,然后按F8键(逐行debugging)以查看每一行的代码。.不要忘记turn on“立即”窗口和“本地”窗口。当您逐行调试代码时,他们将是您最好的朋友。
让我们假设lr为12(因为您查看的是我假设的最后一行)。
For r = 12 To 2 Step 1 'This code will never execute... it will just pass since 12 > 2.
这两个代码都将运行:
这两个语句相等时:For r = 2 To 12 Step 1
-> For r = 2 To 12
Dim lr As Long
Dim r As Long
For r = 2 To 12 'Go from row 2 to 12.
Cells(r, 1).Value = 3
Next r 'Go to next r, i.e. next step.
如果您想向后循环,我们可以这样做:
Dim lr As Long
Dim r As Long
For r = 12 To 2 Step -1 'Go from row 12 to 2, by one step backwards.
Cells(r, 1).Value = 3
Next r
可以像这样建立范围:
Range("S73:S128") -> Range(Cells(S73), Cells(S128)) ->
Range(Cells(row number, column number), Cells(row number, column number)) ->
-> Range(Cells(73, 19), Cells(128, 19))
假设lr为12。
For r = 2 To lr 'We loop from row 2 until last row 12.
If Cells(r, 28).Value = 1 Then 'If row "r" in column AB (Column 28) is equal to 1
Rows(r).Copy
Range ("A" & lr2 + 1)'I'm not sure what this part is intended to do..
对工作簿和工作表的引用:
Dim wkb As Workbook
Dim ws As Worksheet
Dim wkb2 As Workbook
Dim ws As Worksheet
Set wkb = Workbooks("Workbook1.xlsx") 'define first workbook
Set ws= wkb.Worksheets("Sheet1") 'Define worksheet
Set wkb2 = Workbooks("Workbook2.xlsx") 'Define 2nd workbook
Set ws2 = wkb2.Worksheets("Sheet2") 'Define 2nd worksheet, notice wkb2 in the beginning.
Then you can use it when you want to build your ranges and refer to different sheets...
ws2.Cells(1,2).Value = ws.Cells(1,1).Value 'Copy from worksheet1 and paste it in worksheet2.