适用于i IF的VBA复制单元

时间:2014-11-18 15:16:12

标签: excel vba excel-vba

我正在努力学习VBA,但仍然非常粗糙

我正在尝试创建一个宏,该宏将为分配给另一个工作簿中的员工的任务编译报告。我没有使用所有行。

问题在于宏正在返回所有任务,而不仅仅是分配给指定员工的任务。

我认为我的问题正在发生,因为我同时使用For和If语句。

感谢您的帮助。

Option Explicit
Sub Import_Open_Jobs()
Dim DestinWB As Workbook: Set DestinWB = ThisWorkbook
Dim SourceWB As Workbook: Set SourceWB = Workbooks.Open(Filename:="C:\Users\...\Open Tasks    Report.xlsx")
Dim ci As Integer
DestinWB.Activate
    Sheets("Open Tasks").Range("A3").Select
        Do Until ActiveCell.Value = ""
            Intersect(Range("A:P"), ActiveCell.EntireRow).Clear
        ActiveCell.Offset(1, 0).Select
        Loop

SourceWB.Activate
SourceWB.Sheets("Task_List").Range("A2").Select
Do Until ActiveCell.Value = ""

'F1 contains the name of the employee that the report is created for.
    If Cells(ActiveCell.Row, 5).Value = DestinWB.Sheets("Open Tasks").Range("F1") Then 


        For ci = 3 To 10000
            DestinWB.Sheets("Open Tasks").Cells(ci, 1).Value = ActiveCell.Value
            If ActiveCell.Value = "" Then Exit For
            DestinWB.Sheets("Open Tasks").Cells(ci, 2).Value = Cells(ActiveCell.Row, 2)
            DestinWB.Sheets("Open Tasks").Cells(ci, 3).Value = Cells(ActiveCell.Row, 8)
            DestinWB.Sheets("Open Tasks").Cells(ci, 4).Value = Cells(ActiveCell.Row, 9)
            DestinWB.Sheets("Open Tasks").Cells(ci, 5).Value = Cells(ActiveCell.Row, 15)
            DestinWB.Sheets("Open Tasks").Cells(ci, 6).Value = Cells(ActiveCell.Row, 22)
            DestinWB.Sheets("Open Tasks").Cells(ci, 7).Value = Cells(ActiveCell.Row, 23)
            DestinWB.Sheets("Open Tasks").Cells(ci, 8).Value = Cells(ActiveCell.Row, 24)
            DestinWB.Sheets("Open Tasks").Cells(ci, 9).FormulaR1C1 = "=IF(RC[-2]="""",""NA"",IF(RC[-1]="""",""NA"",NETWORKDAYS(RC[-2],RC[-1])))"
            DestinWB.Sheets("Open Tasks").Cells(ci, 10).FormulaR1C1 = "=IF(RC[-3]="""",""NA"",IF(RC[-2]="""",""NA"",NETWORKDAYS(RC[-3],R1C4,)))"
            DestinWB.Sheets("Open Tasks").Cells(ci, 11).Value = Cells(ActiveCell.Row, 27)
            DestinWB.Sheets("Open Tasks").Cells(ci, 12).Value = Cells(ActiveCell.Row, 16)
            DestinWB.Sheets("Open Tasks").Cells(ci, 13).Value = Cells(ActiveCell.Row, 21)
            DestinWB.Sheets("Open Tasks").Cells(ci, 14).Value = Cells(ActiveCell.Row, 18)
            DestinWB.Sheets("Open Tasks").Cells(ci, 15).Value = Cells(ActiveCell.Row, 19)
            DestinWB.Sheets("Open Tasks").Cells(ci, 16).Value = Cells(ActiveCell.Row, 20)
            ActiveCell.Offset(1, 0).Select
        Next ci
    Else
            ActiveCell.Offset(1, 0).Select
    End If

Loop
SourceWB.Close
DestinWB.Activate
Range("D1").Value = Format(Now(), "MM-DD-YYYY")
Columns("F:H").Select
    Selection.NumberFormat = "m/d/yyyy"

End Sub

1 个答案:

答案 0 :(得分:0)

我把IF放在For循环中,而不是在外面。

如果我理解正确,你想要浏览所有单元格,如果条件匹配,请将其移除。

如果第一个单元格的值匹配,您当前正在做什么,请引入下一个10000