Vba 使用条件将数据从一个工作表复制到另一个工作表
我正在处理一个宏,它可以帮助我在一定条件下将Excel中的数据从一个工作表复制到另一个工作表。我尝试了以下代码;它可以工作,但是我得到了一个无限循环,我无法在代码中正确地创建条件(我必须只复制下拉列表显示的行(完成),实际上它填充了3个选项(完成/取消/正在进行)Vba 使用条件将数据从一个工作表复制到另一个工作表,vba,excel,Vba,Excel,我正在处理一个宏,它可以帮助我在一定条件下将Excel中的数据从一个工作表复制到另一个工作表。我尝试了以下代码;它可以工作,但是我得到了一个无限循环,我无法在代码中正确地创建条件(我必须只复制下拉列表显示的行(完成),实际上它填充了3个选项(完成/取消/正在进行) 下面是对代码的一些更改,可能会有所帮助 Sub copier() Dim ws1 As Worksheet, ws2 As Worksheet, src As Range, dest As Range, i As Intege
下面是对代码的一些更改,可能会有所帮助
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
我认为你的帖子中有一个额外的“End if”…同时,你的if语句中也有你的“Next I”。如果错误地发布了额外的End if,我可以将我的conditionsource放在我的下拉列表中,因此我编写的代码只有在你选择“complete”时才会复制在您的下拉列表中。如果我们想为您提供更多帮助,您需要使用更多的单词并提供更多的信息。@Riadh。很抱歉,他不是一个代码编写服务,我发现您的解释很难理解。我将无法为您提供更多已经提供的帮助。我希望我对您有所帮助。
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