Dave_george
New Member
- Joined
- Jul 20, 2023
- Messages
- 32
- Office Version
- 2021
- 2016
- 2013
- Platform
- Windows
The code helps me to merge files. Excel auto changes the formatting of dates after merging the files. I have to use text to columns to format the dates again. Is there a workaround to keep the original format? I am using excel 2016. The dates changes from DD/MM/YYYY to MM/DD/YYYY after the files get merged. Mostly the dates are in column AY. TIA
VBA Code:
Private Sub CommandButton2_Click()
Dim SummarySheet As Worksheet
Dim NewWbk As Workbook
Dim folderPath As String
Dim SelectedFiles() As Variant
Dim NRow As Long
Dim filename As String
Dim NFile As Long
Dim WorkBk As Workbook
Dim SourceRange As Range
Dim DestRange As Range
Dim LastColumn As Long
Set NewWbk = Workbooks.Add
Set SummarySheet = NewWbk.Sheets(1)
On Error GoTo Koniec
SelectedFiles = Application.GetOpenFilename(filefilter:="Excel Workbooks (*.xls; *.xlsm; *.csv; *.xlsx),*.xls;*.xslm;*.csv;*.xlsx", MultiSelect:=True)
NRow = 1
' Loop through the list of returned file names
For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)
' Set FileName to be the current workbook file name to open.
filename = SelectedFiles(NFile)
Set WorkBk = Workbooks.Open(filename)
FindRange = WorkBk.Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Row
With WorkBk.Worksheets(1).UsedRange
LastColumn = .Columns(.Columns.Count).Column
End With
Set SourceRange = WorkBk.Worksheets(1).Range(Cells(1, 1), Cells(FindRange, LastColumn))
' Set the destination range to start at column B and be the same size as the source range.
Set DestRange = SummarySheet.Range("A" & NRow)
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, SourceRange.Columns.Count)
SourceRange.Copy DestRange
NRow = NRow + DestRange.Rows.Count
WorkBk.Close savechanges:=False
Next NFile
SummarySheet.Columns.AutoFit
Dim CellA1 As String, CellA2 As String
CellA1 = SummarySheet.Range("A1").Value
CellA2 = SummarySheet.Range("A2").Value
last = Cells(Rows.Count, "A").End(xlUp).Row
For i = last To 2 Step -1
If (Cells(i, "A").Value) = CellA1 Or (Cells(i, "A").Value) = CellA2 Then
Cells(i, "A").EntireRow.Delete
End If
Next i
MsgBox "Done !!!", vbInformation
Exit Sub
Koniec:
NewWbk.Close savechanges:=False
MsgBox "No File Specified.", vbExclamation, "ERROR"
End Sub