从2张不同的纸张复制数据并应用于另一张纸张

时间:2015-06-30 10:27:43

标签: excel vba excel-vba

我希望根据第三张纸中的单元格值从2张不同的纸张中复制数据,并在通过vba应用vlookup后将这些值放在第3张纸中...

我使用以下代码执行此操作:

For TableRowCntr = 0 To i
        Range("A" & 9 + TableRowCntr + n).Select
        ActiveCell.Value = "Fiber No." & TableRowCntr + 1
        Selection.Font.Size = 8
        Range("A" & 11 + TableRowCntr + n).Select
        ActiveCell.Value = "Individual Splice Loss"
        Selection.Font.Size = 8
        With Selection.Font
            .ColorIndex = 4
            .TintAndShade = 0
        End With
        FldrString = Null
        FIBERREPORTNAME = Null
        fiberreportname1 = Null
        Set appExcel = New Application
        Set appExcel1 = New Application
        'Set openWorkBook = ""
        'Set openWorkBook1 = ""
        For TableCOLCntr1 = 0 To j
            iTblCntr = Choose(TableCOLCntr1 + 1, "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC")
            iAlphabet = iTblCntr & 9 + TableRowCntr + n
            Range(iAlphabet).Select
            FIBERREPORTNAME = Choose(TableRowCntr + 1, "A001", "A002", "A003", "A004", "A005", "A006", "A007", "A008", "A009", "A010", "A011", "A012", "A013", "A014", "A015", "A016", "A017", "A018", "A019", "A020", "A021", "A022", "A023", "A024", "A025", "A026", "A027", "A028", "A029", "A030", "A031", "A032", "A033", "A034", "A035", "A036", "A037", "A038", "A039", "A040", "A041", "A042", "A043", "A044", "A045", "A046", "A047", "A048", "A049", "A050", "A051", "A052", "A053", "A054", "A055", "A056")


            ' open workbook
            Set appExcel = New Application
            openWorkBookName = "C:\Users\Shikhar\Desktop\VTL Rewa\Ferozepur\Templates\Fiber Reports\Ferozepur to Jalallabad\A to B\" & SheetName & "\" & FIBERREPORTNAME
            Set openWorkBook = appExcel.Workbooks.Open("C:\Users\Shikhar\Desktop\VTL Rewa\Ferozepur\Templates\Fiber Reports\Ferozepur to Jalallabad\A to B\" & SheetName & "\" & FIBERREPORTNAME)

            'C:\Users\Shikhar\Desktop\VTL Rewa\Ferozepur\Templates\Fiber Reports\Ferozepur to Jalallabad\A to B\



            ActiveCell.Value = "=vlookup(" & iTblCntr & "6," & FIBERREPORTNAME & ".xls!C17:E" & i + 17 & ",3,TRUE)"
            Selection.Font.Size = 8

            openWorkBook.Close
            appExcel.Quit
            Set appExcel1 = New Application
            fiberreportname1 = Choose(TableRowCntr + 1, "A001", "A002", "A003", "A004", "A005", "A006", "A007", "A008", "A009", "A010", "A011", "A012", "A013", "A014", "A015", "A016", "A017", "A018", "A019", "A020", "A021", "A022", "A023", "A024", "A025", "A026", "A027", "A028", "A029", "A030", "A031", "A032", "A033", "A034", "A035", "A036", "A037", "A038", "A039", "A040", "A041", "A042", "A043", "A044", "A045", "A046", "A047", "A048", "A049", "A050", "A051", "A052", "A053", "A054", "A055", "A056")
            openWorkBookName1 = "C:\Users\Shikhar\Desktop\VTL Rewa\Ferozepur\Templates\Fiber Reports\Ferozepur to Jalallabad\B to A\" & SheetName & "\" & fiberreportname1
            Set openWorkBook1 = appExcel1.Workbooks.Open("C:\Users\Shikhar\Desktop\VTL Rewa\Ferozepur\Templates\Fiber Reports\Ferozepur to Jalallabad\B to A\" & SheetName & "\" & fiberreportname1)

            iAlphabet = iTblCntr & 10 + TableRowCntr + n
            Range(iAlphabet).Select


            ActiveCell.Value = "=vlookup(" & iTblCntr & "6," & fiberreportname1 & ".xls!C17:E" & i + 17 & ",3,TRUE)"
            'ActiveCell.Value = "test 1"
            Selection.Font.Size = 8
            openWorkBook1.Close
            appExcel1.Quit
            iAlphabet = iTblCntr & 11 + TableRowCntr + n
            Range(iAlphabet).Select
            Selection.Font.Size = 8
            With Selection.Font
                .ColorIndex = 4
                .TintAndShade = 0
            End With
            ActiveCell.Value = "=sum(" & iTblCntr & 9 + TableRowCntr + n & ": " & iTblCntr & 10 + TableRowCntr + n & ")/2"
        Next
        n = n + 2
    Next

我面临的问题是,一旦代码被执行,我在第9行和第10行的单元格中得到相同的公式....而目标是从第9行的数据中获取数据和数据来自第10行的练习册2。

请帮助。 如果您在理解上述代码方面需要帮助,请告诉我。

0 个答案:

没有答案