Quando estamos trabalhando com várias planilhas, não raramente centenas delas, e precisamos elaborar uma análise, um relatório, importá-las para uma base de dados, etc...Tudo isso seria mais fácil se ao invés de termos centenas de arquivos, tivéssemos acesso a somente uma planilha contendo os dados de todas as demais. Sim, meus caros, nos pouparia muito tempo. E como sempre nos vem a pergunta: Como?
Segue:
Sub CopyFromWorksheets()Dim wrk As WorkbookDim sht As WorksheetDim trg As WorksheetDim rng As RangeDim colCount As Long'Dim sheetDelimiter As String' Creates excel app objectSet objExcel = CreateObject("Excel.Application")' Makes the excel invisibleobjExcel.Visible = False' Supress all display alertsobjExcel.DisplayAlerts = False' Gets the complete path of the active excel sheetstrExcelFilePath = ActiveWorkbook.FullName' Opens the excel fileSet objWorkbook = objExcel.Workbooks.Open(Trim(strExcelFilePath))
Set objWorkSheet = objWorkbook.Worksheets("Merge")objWorkSheet.Activate' Gets the count of columnSet objRange = objWorkbook.Worksheets("Merge")numRowsCount = objRange.Evaluate("COUNTA(A1:A100)")Worksheets("Merge").Activate'sheetDelimiter = "######"Set wrk = ActiveWorkbook 'Working in active workbookFor Each sht In wrk.WorksheetsIf sht.Name = "Consolidated Backlog" ThenMsgBox "There is a worksheet called as 'Consolidated Backlog'." & vbCrLf & _"Please remove or rename this worksheet since 'Consolidated Backlog' would be" & _"the name of the result worksheet of this process.", vbOKOnly + vbExclamation, "Error"Exit SubEnd IfNext shtApplication.ScreenUpdating = FalseSet trg = wrk.Worksheets.Add(After:=wrk.Worksheets(wrk.Worksheets.Count))trg.Name = "Consolidated Backlog"'Get column headers from the first worksheet'Column count firstSet sht = wrk.Worksheets(1)colCount = 30For cntLoop = 1 To numRowsCountstrSheetName = Trim(UCase(objExcel.Cells(cntLoop, 1).Value))If Trim(strSheetName) = "" ThenExit ForEnd IfIf Trim(strSheetName) = "SHEET NAMES" ThenGoTo ContinueEnd IfFor Each sht In wrk.Worksheets'If worksheet in loop is the last one, stop execution (it is Master worksheet)If sht.Index = wrk.Worksheets.Count Then Exit ForIf strSheetName = UCase(sht.Name) Then'Delimits the copied sheets with a string in a new rowWith trg.Cells(1, 1).Resize(1, colCount).Value = sht.Cells(1, 1).Resize(1, colCount).Value'Set font as bold.Font.Bold = TrueEnd Withtrg.Cells(Rows.Count, 1).End(xlUp).Offset(1).Resize(1, 1).Value = sheetDelimiterSet rng = sht.Range(sht.Cells(2, 1), sht.Cells(Rows.Count, 1).End(xlUp).Resize(, colCount))rng.Copy trg.Cells(Rows.Count, 1).End(xlUp).Offset(1)'Set objRange = sht.Range("A1").EntireColumn'objRange.Insert (xlShiftToRight)'sht.Range("A1") = sht.NameEnd IfNext shtContinue:NextobjExcel.QuitSet objWorkbook = NothingSet objExcel = NothingSet sht = NothingSet objWorkSheet = NothingSet objRange = NothingSet trg = NothingSet rng = NothingApplication.ScreenUpdating = True'create WMI object instanceSet objWMI = GetObject("winmgmts:")If Not IsNull(objWMI) Then'create object collection of Win32 processesSet objProcList = objWMI.InstancesOf("win32_process")For Each objProc In objProcList 'iterate through enumeratedIf UCase(objProc.Name) = UCase(procName) ThenobjProc.Terminate (0)End IfNextEnd IfSet objProcList = NothingSet objWMI = NothingEnd Sub
Reference:
Aditya Kalra
Inspiration:
André Luiz Bernardes
Tags: VBA, Tips, dummy, dummies, row, last, cell, célula, dirty area, detect, detectar
Nenhum comentário:
Postar um comentário