The below code is working I just need a review as there are somethings that I think I might have done wrong. The code will edit the raw data of an excel CSV comma delimited file moving all text using textToColumn by deliminator ";" as all except heather are in column A, formatting into a table and hiding unnecessary columns removing the extra spaces from first 2 rows and saving to the hdd in a specific folder.
Main Sub
Option Explicit
Public WS As Worksheet
Sub editRawData()
'
' edit Macro
'
' Keyboard Shortcut: Ctrl+Shift+I
'
Dim myResult As VbMsgBoxResult
myResult = MsgBox("Are you sure you want to edit this worksheet?", vbQuestion + vbOKCancel + vbDefaultButton1, "Edit worksheet?")
If myResult = vbCancel Then Exit Sub
On Error GoTo ErrorHandler
Application.ScreenUpdating = False
Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp)).Offset(1, 0).Select
ActiveWindow.FreezePanes = True
Selection.TextToColumns Destination:=ActiveCell, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:=";", TrailingMinusNumbers:=True
Call removeHeatherSpace
changeProperties WS
Call SaveAsToFolderPath
Range(Cells(1, 23), Cells(Rows.Count, 23).End(xlUp)).Offset(1, 0).Select
Application.ScreenUpdating = True
Exit Sub
ErrorHandler:
Application.ScreenUpdating = True
Debug.Print Err.Description
MsgBox "Sorry, an error occured." & vbCrLf & Err.Number & " " & Err.Description, vbCritical, "Error!"
End Sub
Edit Properties Sub
Private Sub changeProperties(WS As Worksheet)
Dim tableRng As Range
Dim columnsToHide As Range
Dim heatherFormat As Range
Set heatherFormat = Range(Cells(1, 1), Cells(1, Columns.Count).End(xlToLeft)).Columns
Set tableRng = Range("A2").CurrentRegion
Set WS = ActiveSheet
With tableRng.Borders
.LineStyle = xlContinuous
.Weight = xlThin
End With
With heatherFormat.Font
.Name = "Arial"
.FontStyle = "Bold"
End With
heatherFormat.Interior.ColorIndex = 17
tableRng.Cells.EntireColumn.AutoFit
With WS
Set columnsToHide = Application.Union(.Columns("A:F"), _
.Columns("H:O"), _
.Columns("Q"), _
.Columns("S:V"), _
.Columns("Z:AE"), _
.Columns("AI:BD"))
columnsToHide.EntireColumn.Hidden = True
End With
With WS.PageSetup
.PrintArea = ""
.PrintTitleRows = ""
.PrintTitleColumns = ""
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.25)
.RightMargin = Application.InchesToPoints(0.25)
.TopMargin = Application.InchesToPoints(0.75)
.BottomMargin = Application.InchesToPoints(0.75)
.HeaderMargin = Application.InchesToPoints(0.3)
.FooterMargin = Application.InchesToPoints(0.3)
.Orientation = xlLandscape
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
End With
End Sub
Remove space Sub
Sub removeHeatherSpace()
Dim rngRemoveSpace As Range
Dim CellChecker As Range
Set rngRemoveSpace = Range(Cells(1, 1), Cells(2, Columns.Count).End(xlToLeft)).Columns
rngRemoveSpace.Replace What:=Chr(160), Replacement:=Chr(32), LookAt:=xlPart, SearchOrder:=xlByColumns, MatchCase:=False
For Each CellChecker In rngRemoveSpace.Cells
CellChecker.Value = Application.Trim(CellChecker.Value)
CellChecker.Value = Application.Clean(CellChecker.Value)
Next CellChecker
Set rngRemoveSpace = Nothing
End Sub
SaveAs to specific folder
Sub SaveAsToFolderPath()
Dim MyFileName As String
Dim folderPath As String
Dim dateFormat As String
folderPath = "C:\Users\A\Desktop\M work\DFMS\"
dateFormat = Format(Now, "dd.mm.yyyy HH-mm-ss AMPM")
MyFileName = Range("G2").Value
If Not ActiveWorkbook.Saved Then
ActiveWorkbook.SaveAs Filename:=folderPath & MyFileName & " - Next Delivery " & dateFormat & ".xlsm"
End If
End Sub