I have the below code that helps me do some formatting. But I want to increase the efficiency of the code by making it faster. Below are the formatting steps of the macro.
- Convert "Q" and "S" column to number format.
- Replicate the "I" column to new column by inserting a column next to it.
- Cut the column "AD" and paste to column "O".
- Remove columns ("A:A,AD:AG").
- Replace "#" with null and "OUT" with P input value in "AC" column.
- Round the "Q" and "S" column numbers to 2 decimal.
- Change the sign of values in column Q by multiplying -1(i.e.
*-1). - Filter on "Q" column with "0" and filter on "S" column with "0". Then remove those rows with "Q" and "S" is Zero.
- Filter 0 on Q column, Clear only visible cells of "Q" and "R" Columns.
- Filter 0 on "S" column, Clear only visible cells of "S" and "T" Columns.
- Copy the headers (ThisWorkbook.Sheets("Tool").Range("A20:AC20").Copy) and paste to the A1 of file formatted.
- Remove all columns and rows which doesn’t have data apart from used range.
Currently macro working fine but taking some time. As I'm new to VBA, I'm not sure how to optimize the code. Hence I'm here looking for help from experts.
Below is the code:
Sub A_to_B()
Dim LastRow As Long Dim Lastcol As Long Dim P As String
'Display a Dialog Box that allows to select a single file. 'The path
for the file picked will be stored in fullpath variable With
Application.FileDialog(msoFileDialogFilePicker)
'Makes sure the user can select only one file
.AllowMultiSelect = True
'Filter to just the following types of files to narrow down selection options
'.Filters.Add "All Files", "*.xlsx; *.xlsm; *.xls; *.xlsb; *.csv"
'Show the dialog box
.Show
'Store in fullpath variable
fullpath = .SelectedItems.Item(1)
End With
'It's a good idea to still check if the file type selected is accurate.
'Quit the procedure if the user didn't select the type of file we need.
If InStr(fullpath, ".xls") = 0 Then
If InStr(fullpath, ".csv") = 0 Then
Exit Sub
End If
End If
'Open the file selected by the user
Workbooks.Open fullpath
P = InputBox("Please Enter the Version")
Application.ScreenUpdating = False
With ActiveWorkbook
Columns(17).NumberFormat = "0"
Columns(19).NumberFormat = "0"
LastRow = Cells(Rows.Count, 2).End(xlUp).Row
Lastcol = Cells(1, Columns.Count).End(xlToLeft).Column
Columns("I").Copy
Columns("I").Insert Shift:=xlToRight
'Range("AE2").Value = P
'Range("AE2", "AE" & Cells(Rows.Count, "B").End(xlUp).Row).FillDown
Columns("AE").Copy
Columns("P").PasteSpecial xlPasteValues
ActiveSheet.Range("A:A,AE:AG").EntireColumn.Delete
Columns("AC").Replace What:="#", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False,
ReplaceFormat:=False
Columns("AC").Replace What:="OUT", Replacement:=P, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, ReplaceFormat:=False
Range("AD2").Formula = "=Round(Q2,2)"
Range("AD2", "AD" & Cells(Rows.Count, "B").End(xlUp).Row).FillDown
Range("AD2", "AD" & Cells(Rows.Count, "B").End(xlUp).Row).Copy
Range("Q2").PasteSpecial xlPasteValues
Range("AD2").Formula = "=Round(S2,2)"
Range("AD2", "AD" & Cells(Rows.Count, "B").End(xlUp).Row).FillDown
Range("AD2", "AD" & Cells(Rows.Count, "B").End(xlUp).Row).Copy
Range("S2").PasteSpecial xlPasteValues
Range("AD2").Formula = "=(Q2*-1)"
Range("AD2", "AD" & Cells(Rows.Count, "B").End(xlUp).Row).FillDown
Range("AD2", "AD" & Cells(Rows.Count, "B").End(xlUp).Row).Copy
Range("Q2").PasteSpecial xlPasteValues
Columns("AD:AD").EntireColumn.Delete With ActiveSheet.Range("A:AC")
.AutoFilter Field:=17, Criteria1:="0"
.AutoFilter Field:=19, Criteria1:="0"
.Range("A2:A" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
.AutoFilter
.AutoFilter Field:=17, Criteria1:="0"
.Range("Q2:R" & LastRow).SpecialCells(xlCellTypeVisible).Clear
.AutoFilter
.AutoFilter Field:=19, Criteria1:="0"
.Range("S2:T" & LastRow).SpecialCells(xlCellTypeVisible).Clear
.AutoFilter
'.Range("C2").AutoFill .Range("C2:C" & .Cells(.Rows.Count, "B").End(xlUp).Row)
End With End With ThisWorkbook.Sheets("Tool").Range("A20:AC20").Copy
ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteValues
Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Rows("1").SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
'ActiveWorkbook.Save
'ActiveWorkbook.Close
MsgBox "Done With Farmatting"
End Sub