ThrottleWorks
Excel Ninja
Hi,
I am using below mentioned code to trim each column present in the worksheet.
However this code takes time to complete, can anyone please help me with better solution if possible.
Please note, am not facing any issue with this, it is not urgent for me.
Please help me if you get time.
I am using below mentioned code to trim each column present in the worksheet.
However this code takes time to complete, can anyone please help me with better solution if possible.
Please note, am not facing any issue with this, it is not urgent for me.
Please help me if you get time.
Code:
Sub TrimEachCol()
Dim HeaderRng As Range
Dim DeleteBook As Workbook
Dim DeleteSht As Worksheet
Dim rng As Range
Dim TempLr As Long
Dim TempRng As Range
Dim ScrpBk As Workbook
Dim ScrpSht As Worksheet
Dim ColCount As Long
Set HeaderRng = MapSht.Range(MapSht.Cells(2, 1), MapSht.Cells(55, 1))
CLSRawDataDump.Cells.Copy
Workbooks.Add
Range("A1").PasteSpecial xlPasteAll
Set DeleteBook = ActiveWorkbook
Set DeleteSht = ActiveSheet
TempLr = CLSRawDataDump.Cells(CLSRawDataDump.Rows.Count, 1).End(xlUp).Row
CLSRawDataDump.Cells.Clear
ColCount = 0
For Each rng In HeaderRng
ColCount = ColCount + 1
Cells.Find(What:="" & rng, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.EntireColumn.Copy
'--------------------------------------------------
Workbooks.Add
Set ScrpBk = ActiveWorkbook
Set ScrpSht = ScrpBk.Worksheets("Sheet1")
Range("A1").PasteSpecial xlPasteAll
Range("B1:B" & TempLr).FormulaR1C1 = "=TRIM(RC[-1])"
Range("B1:B" & TempLr).Value = Range("B1:B" & TempLr).Value
Range("B1:B" & TempLr).Copy
CLSRawDataDump.Cells(1, ColCount).PasteSpecial xlPasteAll
ScrpBk.Close
DeleteBook.Activate
Next rng
DeleteBook.Close
End Sub