Preciso dividir uma planilha com 10.000 linhas em vários arquivos Excel com base nas datas da coluna D. O arquivo possui 9 colunas (A:I). As datas na coluna D estão atualmente formatadas como DD/MM/AAAA. Eu só quero dividir o arquivo em MMM/AAAA para que haja menos arquivos. Aqui está o meu código até agora, ele divide a planilha em vários arquivos, mas quando abro os arquivos, apenas o cabeçalho foi copiado, não os dados do mês e ano relevantes.
Como posso copiar e colar todos os dados relevantes para MMM/AAAA na coluna D e como posso salvar todos os arquivos divididos em uma pasta específica como esta? C:\Geral\Londres\Clientes
Sub SplitData()
'
' SplitData Macro
Dim objWorksheet As Excel.Worksheet
Dim nLastRow, nRow, nNextRow As Integer
Dim strColumnValue As String
Dim objDictionary As Object
Dim varColumnValues As Variant
Dim varColumnValue As Variant
Dim objExcelWorkbook As Excel.Workbook
Dim objSheet As Excel.Worksheet
Dim aCol As String
aCol = "D"
On Error GoTo err1
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
.EnableEvents = False
End With
Set objWorksheet = ActiveSheet
nLastRow = objWorksheet.Range("A" & objWorksheet.Rows.Count).End(xlUp).Row
Set objDictionary = CreateObject("Scripting.Dictionary")
For nRow = 1 To nLastRow
strColumnValue = Format(objWorksheet.Range(aCol & nRow).Value, "Report_mmm_yyyy")
If objDictionary.Exists(strColumnValue) = False Then
objDictionary.Add strColumnValue, 1
End If
Next
varColumnValues = objDictionary.Keys
For i = LBound(varColumnValues) To UBound(varColumnValues)
varColumnValue = varColumnValues(i)
Set objExcelWorkbook = Excel.Application.Workbooks.Add
Set objSheet = objExcelWorkbook.Sheets(1)
objSheet.Name = objWorksheet.Name
objWorksheet.Rows(1).EntireRow.Copy
objSheet.Activate
objSheet.Range("A1").Select
objSheet.Paste
For nRow = 1 To nLastRow
If CStr(objWorksheet.Range(aCol & nRow).Value) = CStr(varColumnValue) Then
objWorksheet.Rows(nRow).EntireRow.Copy
nNextRow = objSheet.Range("A" & objWorksheet.Rows.Count).End(xlUp).Row + 1
objSheet.Range("A" & nNextRow).Select
objSheet.Paste
objSheet.Columns("A:I").AutoFit
End If
Next
objExcelWorkbook.SaveAs (CStr(varColumnValue))
Next
err1:
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
.EnableEvents = True
End With
End Sub
Responder1
O principal foi substituir
If CStr(objWorksheet.Range(aCol & nRow).Value) = CStr(varColumnValue) Then
com
If Format(objWorksheet.Range(aCol & nRow).Value, "Report_mmm_yyyy") = varColumnValue Then
Mas fiz mais alterações para copiar e colar um pouco mais rápido. Como observação lateral, colar não é uma opção rápida, você poderia fazer algo parecido TargetCell.Value = SourceCell.Value
(neste caso, apenas os valores são transferidos).
Sub SplitData()
'
' SplitData Macro
Dim objWorksheet As Excel.Worksheet
Dim nLastRow As Integer, nRow As Integer, nNextRow As Integer
Dim strColumnValue As String
Dim objDictionary As Object
Dim varColumnValues As Variant
Dim varColumnValue As Variant
Dim objExcelWorkbook As Excel.Workbook
Dim objSheet As Excel.Worksheet
Dim i As Long
Dim aCol As String
aCol = "D"
On Error GoTo err1
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
.EnableEvents = False
End With
Set objWorksheet = ActiveSheet
nLastRow = objWorksheet.Range("A" & objWorksheet.Rows.Count).End(xlUp).Row
Set objDictionary = CreateObject("Scripting.Dictionary")
For nRow = 2 To nLastRow
strColumnValue = Format(objWorksheet.Range(aCol & nRow).Value, "Report_mmm_yyyy")
If objDictionary.Exists(strColumnValue) = False Then
objDictionary.Add strColumnValue, 1
End If
Next
varColumnValues = objDictionary.Keys
For i = LBound(varColumnValues) To UBound(varColumnValues)
varColumnValue = varColumnValues(i)
Set objExcelWorkbook = Excel.Application.Workbooks.Add
Set objSheet = objExcelWorkbook.Sheets(1)
objSheet.Name = objWorksheet.Name
objSheet.Rows(1).Value = objWorksheet.Rows(1).Value
objWorksheet.Rows(1).Copy objSheet.Rows(1)
nNextRow = 2
For nRow = 1 To nLastRow
If Format(objWorksheet.Range(aCol & nRow).Value, "Report_mmm_yyyy") = varColumnValue Then
objWorksheet.Rows(nRow).Copy objSheet.Rows(nNextRow)
nNextRow = nNextRow + 1
End If
Next
objSheet.Columns("A:I").AutoFit
' Closing the workbook with changes saved
objExcelWorkbook.Close True, CStr(varColumnValue)
Next
err1:
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
.EnableEvents = True
End With
End Sub
Responder2
Sub SplitData() ' ' Macro SplitData
Dim objWorksheet As Excel.Worksheet Dim nLastRow As Integer, nRow As Integer, nNextRow As Integer Dim strColumnValue As String Dim objDictionary As Object Dim varColumnValues As Variant Dim varColumnValue As Variant Dim objExcelWorkbook As Excel.Workbook Dim objSheet As Excel.Worksheet Dim i As Long Dim FPath como string FPath = Application.ActiveWorkbook.Path
Dim aCol As String aCol = "A"
Em caso de erro, vá para err1
Com aplicativo .ScreenUpdating = False .Calculation = xlCalculationManual .EnableEvents = False Terminar com
Definir objWorksheet = ActiveSheet nLastRow = objWorksheet.Range("A" & objWorksheet.Rows.Count).End(xlUp).Row
Definir objDictionary = CreateObject("Scripting.Dictionary")
Para nRow = 2 Para nLastRow
strColumnValue = Format(objWorksheet.Range(aCol & nRow).Value, "mmm_yyyy")
If objDictionary.Exists(strColumnValue) = False Then
objDictionary.Add strColumnValue, 1
End If
Próximo
varColumnValues = objDictionary.Keys
Para i = LBound(varColumnValues) Para UBound(varColumnValues) varColumnValue = varColumnValues(i)
Set objExcelWorkbook = Excel.Application.Workbooks.Add
Set objSheet = objExcelWorkbook.Sheets(1)
objSheet.Name = objWorksheet.Name
objSheet.Rows(1).Value = objWorksheet.Rows(1).Value
objWorksheet.Rows(1).Copy objSheet.Rows(1)
nNextRow = 2
For nRow = 1 To nLastRow
If Format(objWorksheet.Range(aCol & nRow).Value, "mmm_yyyy") = varColumnValue Then
objWorksheet.Rows(nRow).Copy objSheet.Rows(nNextRow)
nNextRow = nNextRow + 1
End If
Next
objSheet.Columns("A:K").AutoFit
CheckDir (FPath & "\" & varColumnValue)
objExcelWorkbook.SaveAs FPath & "\" & varColumnValue & "\" & "Report.xlsx"
' Closing the workbook with changes saved
objExcelWorkbook.Close True, CStr(varColumnValue)
Próximo
err1: Com Aplicativo .ScreenUpdating = True .Calculation = xlCalculationAutomatic .EnableEvents = True Finalizar com
Finalizar sub
Função CheckDir (caminho como string)
If Dir(Path, vbDirectory) = "" Then
MkDir (Path)
End If
Função final