Chào Anh Chi và các bạn, Nhờ giúp Code VBA . Mình cần thực hiện lấy dự liệu sumif ở nhiều sheets ( Các sheet co mẫu giống nhau) trong một File Excel khác. Mình đang thực hiện Record Marcro thì code quá dài VBA không chấp nhận. Nhờ Anh Chị hướng dẫn code khác . Hoặc hướng dẫn mình rút gọn bớt code sau . Minh thực hiện sumif gần 70 sheets nên recode rất nhiều dòng. Cám ơn diễn đàn
Option Explicit |
Sub Get_Data_From_File_006_Update1() |
Dim FileToOpen As Variant |
Dim OpenBook As Workbook |
Application.ScreenUpdating = False |
FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.xls*),*xls*") |
If FileToOpen <> False Then |
Set OpenBook = Application.Workbooks.Open(FileToOpen) |
ThisWorkbook.Activate |
Sheets("Report_007).Select |
Range("G13").Select |
ActiveCell.FormulaR1C1 = _ |
=SUMIF('[BC_Ngay_K6-1.xlsm]1_HERSCHEL'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]1_HERSCHEL'!C5) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("G14").Select |
ActiveCell.FormulaR1C1 = _ |
=SUMIF('[BC_Ngay_K6-1.xlsm]1_HERSCHEL'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]1_HERSCHEL'!C6) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("G15").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]1_HERSCHEL'!C2,R[-14]C[-3],'[BC_Ngay_K6-1.xlsm]1_HERSCHEL'!C7),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("G32").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]1_HERSCHEL'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]1_HERSCHEL'!C8),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("G48").Select |
ActiveCell.FormulaR1C1 = _ |
=SUMIF('[BC_Ngay_K6-1.xlsm]1_HERSCHEL'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]1_HERSCHEL'!C9) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("G49").Select |
ActiveCell.FormulaR1C1 = _ |
=SUMIF('[BC_Ngay_K6-1.xlsm]1_HERSCHEL'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]1_HERSCHEL'!C10) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("G55").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]1_HERSCHEL'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]1_HERSCHEL'!C11),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("G56").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]1_HERSCHEL'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]1_HERSCHEL'!C12),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("G57").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]1_HERSCHEL'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]1_HERSCHEL'!C13),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("G58").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]1_HERSCHEL'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]1_HERSCHEL'!C14),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("J13").Select |
ActiveCell.FormulaR1C1 = _ |
=SUMIF('[BC_Ngay_K6-1.xlsm]2_HERSCHEL_OB_PnP'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]2_HERSCHEL_OB_PnP'!C5) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("J14").Select |
ActiveCell.FormulaR1C1 = _ |
=SUMIF('[BC_Ngay_K6-1.xlsm]2_HERSCHEL_OB_PnP'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]2_HERSCHEL_OB_PnP'!C6) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("J15").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]2_HERSCHEL_OB_PnP'!C2,R[-14]C[-3],'[BC_Ngay_K6-1.xlsm]2_HERSCHEL_OB_PnP'!C7),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("J32").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]2_HERSCHEL_OB_PnP'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]2_HERSCHEL_OB_PnP'!C8),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("J48").Select |
ActiveCell.FormulaR1C1 = _ |
=SUMIF('[BC_Ngay_K6-1.xlsm]2_HERSCHEL_OB_PnP'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]2_HERSCHEL_OB_PnP'!C9) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("J49").Select |
ActiveCell.FormulaR1C1 = _ |
=SUMIF('[BC_Ngay_K6-1.xlsm]2_HERSCHEL_OB_PnP'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]2_HERSCHEL_OB_PnP'!C10) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("J55").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]2_HERSCHEL_OB_PnP'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]2_HERSCHEL_OB_PnP'!C11),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("J56").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]2_HERSCHEL_OB_PnP'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]2_HERSCHEL_OB_PnP'!C12),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("J57").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]2_HERSCHEL_OB_PnP'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]2_HERSCHEL_OB_PnP'!C13),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("J58").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]2_HERSCHEL_OB_PnP'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]2_HERSCHEL_OB_PnP'!C14),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("M13").Select |
ActiveCell.FormulaR1C1 = _ |
=SUMIF('[BC_Ngay_K6-1.xlsm]3_EI_KNQ_CRAYOLA'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]3_EI_KNQ_CRAYOLA'!C5) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("M14").Select |
ActiveCell.FormulaR1C1 = _ |
=SUMIF('[BC_Ngay_K6-1.xlsm]3_EI_KNQ_CRAYOLA'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]3_EI_KNQ_CRAYOLA'!C6) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("M15").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]3_EI_KNQ_CRAYOLA'!C2,R[-14]C[-3],'[BC_Ngay_K6-1.xlsm]3_EI_KNQ_CRAYOLA'!C7),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("M32").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]3_EI_KNQ_CRAYOLA'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]3_EI_KNQ_CRAYOLA'!C8),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("M48").Select |
ActiveCell.FormulaR1C1 = _ |
=SUMIF('[BC_Ngay_K6-1.xlsm]3_EI_KNQ_CRAYOLA'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]3_EI_KNQ_CRAYOLA'!C9) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("M49").Select |
ActiveCell.FormulaR1C1 = _ |
=SUMIF('[BC_Ngay_K6-1.xlsm]3_EI_KNQ_CRAYOLA'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]3_EI_KNQ_CRAYOLA'!C10) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("M55").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]3_EI_KNQ_CRAYOLA'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]3_EI_KNQ_CRAYOLA'!C11),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("M56").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]3_EI_KNQ_CRAYOLA'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]3_EI_KNQ_CRAYOLA'!C12),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("M57").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]3_EI_KNQ_CRAYOLA'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]3_EI_KNQ_CRAYOLA'!C13),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("M58").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]3_EI_KNQ_CRAYOLA'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]3_EI_KNQ_CRAYOLA'!C14),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("P13").Select |
ActiveCell.FormulaR1C1 = _ |
=SUMIF('[BC_Ngay_K6-1.xlsm]4_SCI_EI'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]4_SCI_EI'!C5) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("P14").Select |
ActiveCell.FormulaR1C1 = _ |
=SUMIF('[BC_Ngay_K6-1.xlsm]4_SCI_EI'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]4_SCI_EI'!C6) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("P15").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]4_SCI_EI'!C2,R[-14]C[-3],'[BC_Ngay_K6-1.xlsm]4_SCI_EI'!C7),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("P32").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]4_SCI_EI'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]4_SCI_EI'!C8),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("P48").Select |
ActiveCell.FormulaR1C1 = _ |
=SUMIF('[BC_Ngay_K6-1.xlsm]4_SCI_EI'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]4_SCI_EI'!C9) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("P49").Select |
ActiveCell.FormulaR1C1 = _ |
=SUMIF('[BC_Ngay_K6-1.xlsm]4_SCI_EI'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]4_SCI_EI'!C10) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("P55").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]4_SCI_EI'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]4_SCI_EI'!C11),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("P56").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]4_SCI_EI'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]4_SCI_EI'!C12),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("P57").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]4_SCI_EI'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]4_SCI_EI'!C13),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("P58").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]4_SCI_EI'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]4_SCI_EI'!C14),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("S13").Select |
ActiveCell.FormulaR1C1 = _ |
=SUMIF('[BC_Ngay_K6-1.xlsm]5_BOAD&MORE'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]5_BOAD&MORE'!C5) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("S14").Select |
ActiveCell.FormulaR1C1 = _ |
=SUMIF('[BC_Ngay_K6-1.xlsm]5_BOAD&MORE'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]5_BOAD&MORE'!C6) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("S15").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]5_BOAD&MORE'!C2,R[-14]C[-3],'[BC_Ngay_K6-1.xlsm]5_BOAD&MORE'!C7),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("S32").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]5_BOAD&MORE'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]5_BOAD&MORE'!C8),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("S48").Select |
ActiveCell.FormulaR1C1 = _ |
=SUMIF('[BC_Ngay_K6-1.xlsm]5_BOAD&MORE'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]5_BOAD&MORE'!C9) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("S49").Select |
ActiveCell.FormulaR1C1 = _ |
=SUMIF('[BC_Ngay_K6-1.xlsm]5_BOAD&MORE'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]5_BOAD&MORE'!C10) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("S55").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]5_BOAD&MORE'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]5_BOAD&MORE'!C11),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("S56").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]5_BOAD&MORE'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]5_BOAD&MORE'!C12),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
Range("S57").Select |
ActiveCell.FormulaR1C1 = _ |
=IFERROR(AVERAGEIF('[BC_Ngay_K6-1.xlsm]5_BOAD&MORE'!C2,R1C4,'[BC_Ngay_K6-1.xlsm]5_BOAD&MORE'!C13),0) |
ActiveCell.Copy |
Selection.PasteSpecial xlPasteValues |
OpenBook.Close False |
End If |
Application.ScreenUpdating = True |
MsgBox "Done Update !" |
Range("A5").Select |
End Sub |