怎么做"做什么"有条件吗?

时间:2015-10-01 15:40:21

标签: vba excel-vba excel

我试图做一个循环,其中如果条件为真并且单元格n索引(列E)与单元格的n + 1索引相同,则将列C值复制到列中-G。

不幸的是,最后的C列值没有复制到G列,所以我只是用字符串解析它:

Sheets("Report KIT").Range("G" & n) = Sheets("Report KIT").Range("C" & n).Value
之后。有人可以帮忙吗?感谢

    Sub Macroarea1()
Dim ws As Worksheet
Dim LastRow As Long
Dim LastRow1 As Long, LastRow3 As Long
Dim i, n As Integer, x As Integer, y As Integer
Set ws = ActiveWorkbook.Sheets("Report KIT")

    If Sheets("Migrazioni").Range("F" & 7) = "si" Then
    n = Sheets("Migrazioni").Range("N" & 7).Value
    Do While _
Sheets("Report KIT").Range("E" & n) = Sheets("Report KIT").Range("E" & n + 1)
Sheets("Report KIT").Range("G" & n) = Sheets("Report KIT").Range("C" & n).Value
n = n + 1
Loop
Sheets("Report KIT").Range("G" & n) = Sheets("Report KIT").Range("C" & n).Value
End If

End Sub

enter image description here

1 个答案:

答案 0 :(得分:0)

我清理了一些循环并首先检查了E列值并修改了循环以检查以确保每行符合初始E列值以进行测试。我用你的样本数据测试了它,它产生了你想要的结果。有时这些条件循环可能很难搞清楚!

Sub Macroarea1()

    Dim wsR As Worksheet, wsN As Worksheet
    Dim LastRow As Long
    Dim LastRow1 As Long, LastRow3 As Long
    Dim i, n As Integer, x As Integer, y As Integer

    Set wsR = ActiveWorkbook.Sheets("Report KIT")
    Set wsN = ActiveWorkbook.Sheets("Migrazioni")

    If wsN.Range("F7") = "si" Then

        n = wsN.Range("N7").Value
        i = wsR.Range("E" & n).Value

        Do Until wsR.Range("E" & n) <> i
            wsR.Range("G" & n).Value = wsR.Range("C" & n).Value
            n = n + 1
        Loop 

    End If

End Sub