?
Solved

Trim Function/ Excel 2007 VBA

Posted on 2011-02-24
5
Medium Priority
?
905 Views
Last Modified: 2012-05-11
Experts- Please assist with the following.

Request: I need a VBA (Trim Function) Code that can loop through five Excel 2007 worksheets while removing all leading/trailing spaces.

Additional Info
a)  My dataset range spans from Column A through Column M with a variable number of rows

b) These spaces are created by pasting  data from a web page.

c) There are no spaces between text in cells.

Thanks,

armySSG
 
0
Comment
Question by:armyssg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 6

Accepted Solution

by:
FernandoFernandes earned 1000 total points
ID: 34973187
Try to use this code
Sub TrimSheets
dim i as integer
dim sht as worksheet
dim rng as range
for i = 1 to thisworkbook.sheets.count
set sht = thisworkbook.sheets(i)
sht.unprotect
for each rng in sht.usedrange.cells
rng.formula = trim(rng.formula)
next rng
set sht = nothing
next i
End Sub

Open in new window

0
 
LVL 24

Assisted Solution

by:broomee9
broomee9 earned 1000 total points
ID: 34973199
Try something like this:
Option Explicit

Sub RemoveSpaces()

    Dim wkSht As Worksheet
    Dim cel As Range
    
    For Each wkSht In ThisWorkbook.Worksheets
        For Each cel In wkSht.UsedRange
            cel.Value = Trim(cel.Value)
        Next
    Next wkSht

End Sub

Open in new window

0
 
LVL 22

Expert Comment

by:rspahitz
ID: 34973243
You could also use the built-in Excel functions for that.

Go to a new sheet and in cell A1 put a formula like this:

=Trim(Sheet1!A1)

Copy that to all cells that are in the range of the original sheet's data and you're ready to go.
If you want this to be saved without the spaces, select all cells on the new sheet and Paste-Special-Values back onto itself and you're done.
0
 

Author Closing Comment

by:armyssg
ID: 35210093
Both answers seemed to have worked perfect.  My thanks to both of your for the quick feedback answer.  And my apologies for not awarding the points sooner.  Somehow, I thought I did, but I am guessing it didn't register when I tried last month due to network issues we were having at work.  

Thanks again for the great answers.  

 rspahitz- Your answer was good as well, but I was looking for a vba answer specifically.
0
 
LVL 6

Expert Comment

by:FernandoFernandes
ID: 35210147
:)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question