我正在处理一个宏,它可以帮助我在一些条件下将数据从一个工作表复制到另一个工作表。我尝试了以下代码;它工作,但我有一个无限循环,我无法在代码中正确的条件(我只需要复制下拉列表显示(完整)的行,实际上它填充了3个选项(完成/取消/正在进行中)
Sub copier()
Dim ws1 As Worksheet, ws2 As Worksheet, src As Range, dest As Range, i As Integer
Set ws1 = Worksheets("Workload - Charge de travail")
Set ws2 = Worksheets("Sheet1")
For i = 2 To ws1.UsedRange.Rows.Count
Set src = ws1.Range("A2" & i & ":AG10" & i)
Set dest = ws2.Range("A2" & i & ":AG10" & i)
If Source.Cells(1, 4).Value = "complete" Then
src.Copy Destination:=dest
dest.Value = dest.Value
Next i
End If
End Sub
答案 0 :(得分:0)
这里可能会对您的代码进行一些更改。
Sub copier()
Dim ws1 As Worksheet, ws2 As Worksheet, src As Range, dest As Range, i As Integer
Set ws1 = Worksheets("Workload - Charge de travail")
Set ws2 = Worksheets("Sheet1")
For i = 2 To ws1.Range("A1").SpecialCells(xlLastCell).Row
' this is more reliable than the method you used (which relies ont eh workbook being saved)
' but there must be no gaps in column A.
' Your code would not work. Think about whay happens as i increases!
' ws1.Range("A2" & i & ":AG10" & i)
Set src = ws1.Range("A" & i & ":AG" & i+10 )
Set dest = ws2.Range("A" & i & ":AG" & i+10 )
If Source.Cells(1, 4).Value = "complete" Then
' what is source?
' What is this trying to do! I think you mean i+1 not 1.
' I think you mean src not source. I'm not psychic.
src.Copy Destination:=dest
dest.Value = dest.Value
End If
Next i
End Sub