3

I have attempted to add functionality to an excel add-in ave been developing which trims the leading spaces at the end of used cells, and maybe even parse the text, The reason I need to do this is simply to have it turn into a hyperlink which I have already working but that parts fine.

This is what I have attempted so far, I have it trimming the active.worksheet am on which is fine but I can't figure out how to:

  1. Trim Every cell being used across the whole workbook.
  2. And also parse the text if possible

This is my attempt at Trimming the entire workbook, Its something simple I just know it, I just cant figure it out:

Sub DoTrim(Wb As Workbook)
Dim cell As Range
Dim str As String
Dim nAscii As Integer
Dim wsh As Worksheet

For Each wsh In Worksheets
    With wsh.UsedRange
        For Each cell In ActiveSheet.UsedRange
            str = Trim(cell)
             If Len(str) > 0 Then
                        nAscii = Asc(Left(str, 1))
                        If nAscii < 33 Or nAscii = 160 Then
                            If Len(str) > 1 Then
                              str = Right(str, Len(str) - 1)
                            Else
                                str = ""
                            End If
                        End If
                    End If
                    cell = str
        Next cell
    End With
Next wsh
End Sub

Any advice would be welcome am fairly new to this Language so sorry if I sound like a complete Newb!

TL;DR Trims cells only worksheet am on, needs to run across whole workbook I cant figure out how to iterate it across the whole thing.

EDIT: Is that also a quicker way of trimming these cells, the spreadsheets that are created for whom am designing this are massive and takes a while to trim the cells at times

Community
  • 1
  • 1
Studento919
  • 625
  • 2
  • 15
  • 44
  • Birds View: `ActiveSheet.UsedRange`? Did you mean `wsh.UsedRange`? Looking at the rest of the code now... – Siddharth Rout Jan 09 '14 at 09:20
  • 1
    Are there formulas int eh worksheets or just plain values? – Siddharth Rout Jan 09 '14 at 09:22
  • Because this is going to be an add-in theres a good possibility for some of the workbooks to contain formulas, I have tried to wsh.UsedRange but get the error: Object Variable or With block variable not set but I have not tried the below method, currently running it now – Studento919 Jan 09 '14 at 09:36
  • 1
    Answer Updated to incorporate formula check. You may have to refresh the page to see it. – Siddharth Rout Jan 09 '14 at 09:39

2 Answers2

9

Try this

Sub DoTrim(Wb As Workbook)
    Dim aCell As Range
    Dim wsh As Worksheet
    
    '~~> If you are using it in an Add-In, it is advisable 
    '~~> to keep the user posted :)
    Application.StatusBar = "Processing Worksheets... Please do not disturb..."
    DoEvents
    
    Application.ScreenUpdating = False
    
    For Each wsh In Wb.Worksheets
        With wsh
            Application.StatusBar = "Processing Worksheet " & _
                                    .Name & ". Please do not disturb..."
            DoEvents
            
            For Each aCell In .UsedRange
                If Not aCell.Value = "" And aCell.HasFormula = False Then
                    With aCell
                        .Value = Replace(.Value, Chr(160), "")
                        .Value = Application.WorksheetFunction.Clean(.Value)
                        .Value = Trim(.Value)
                    End With
                End If
            Next aCell
        End With
    Next wsh
    
    Application.ScreenUpdating = True
    Application.StatusBar = "Done"
End Sub
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • Quick question what is : .Value = Replace(.Value, Chr(160), "") .Value = Application.WorksheetFunction.Clean(.Value) Suppose to do am just curious as it doesnt have an effect at all i think :P – Studento919 Jan 09 '14 at 14:10
  • Posting from phone so please ignore typos. Which one do you have a doubt with. Both? – Siddharth Rout Jan 09 '14 at 14:36
  • Both yeah, I have a vauge idea of what there suppose to do ave added removed these from the code snippet and they dont seem to have an effect hence why am asking what they are for ;P – Studento919 Jan 09 '14 at 15:19
  • 1
    Sorry was out. The CLEAN function was designed to remove the first 32 nonprinting characters in the 7-bit ASCII code (values 0 through 31) from text. You can read more about it [HERE](http://office.microsoft.com/en-in/excel-help/clean-function-HP010062560.aspx). It doesn't remove 160 though so we use that separately. – Siddharth Rout Jan 09 '14 at 20:27
6

I agree with Siddarth:

For Each cell In ActiveSheet.UsedRange

Should be:

For Each cell In wsh.UsedRange

I would have thought you should be able to remove with 'With wsh.UsedRange' statement around the loop as well.

As you are passing in a WorkBook reference, perhaps you should consider changin your outer For loop from:

For Each wsh In Worksheets

to:

For Each wsh In Wb.Worksheets
MarkHone
  • 381
  • 5
  • 12
  • That works a treat, Thanks very much I feel a bit dumb now lol A quick question is there a more efficient way to trim cells the spreadsheets created within my team are pretty horrendous leading to long times for this to run or any kind of work around in your opinion – Studento919 Jan 09 '14 at 09:45