Solved

Trim Function/ Excel 2007 VBA

Posted on 2011-02-24
5
873 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
5 Comments
 
LVL 6

Accepted Solution

by:
FernandoFernandes earned 250 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 250 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

809 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