Excel

Convert or Trim Data Quickly

Ease of Use

Easy

Version tested with

 

Submitted by:

BrianMH

Description:

This allows you to quickly convert or trim the text of multiple cells to different data types. 

Discussion:

I use this quite often when pasting info from an sql report or from a spreadsheet and then need to work with it as a specific data type. IE dates, currencies, numbers. Many time when it is pasted in it pastes as text. This is particularly annoying with dates. Sometimes there will be spaces at the beginning or end and then you have to edit each cell or you can just use this macro. Say you have 100 cells of text that should be dates. Select those cells and run this macro. It will popup and you will need to enter a number for the conversion type. Choosing 1 will convert to dates 2 will convert to a currency 3 will convert to a decimal 4 will convert to a long (whole number) 5 will simply trim any leading and trailing spaces Trimming is especially useful for ranges you will be doing vlookups on. 

Code:

instructions for use

			

Option Explicit Sub Trimmer() Dim rSel As Range Dim c As Range Dim strV Dim intConv As Integer Dim arr As Variant Dim sDay As String Dim sMonth As String Dim sYear As String Application.ScreenUpdating = False Application.Calculation = xlCalculationManual Set rSel = Selection If Application.CountA(rSel) = 0 Then MsgBox "No values selected", , "Trimmer" Exit Sub End If intConv = Application.InputBox("What would you like to convert to? Please enter a number" & Chr(13) & _ "1. Date " & vbTab & "4. long (whole number)" & Chr(13) & _ "2. Currency " & vbTab & "5. Don't convert just trim values" & Chr(13) & _ "3. Decimal " & vbTab & "6. Convert ISO (YYYYMMDD) dates to normal dates", , , , , , , 1) arr = Array(127, 129, 141, 143, 144, 157, 160) If rSel.Cells.Count > 5000 Then If MsgBox("You have selected a large number of cells, this may take some time, do you want to continue?", vbOKCancel) = vbCancel Then Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True Exit Sub End If End If Select Case intConv Case 1 For Each c In rSel.Cells If c.Value <> "" Then c.Value = CDate(Trim(c.Value)) End If Next c Case 2 For Each c In rSel.Cells If c.Value <> "" Then c.Value = CCur(Trim(c.Value)) End If Next c Case 3 For Each c In rSel.Cells If c.Value <> "" Then c.Value = CDec(Trim(c.Value)) End If Next c Case 4 For Each c In rSel.Cells If c.Value <> "" Then c.Value = CLng(Trim(c.Value)) End If Next c Case 5 For Each c In rSel.Cells If Trim(c.Value) = "" Then c.Value = "" If c.Value <> "" Then strV = Trim(c.Value) For Each a In arr strV = Application.Substitute(strV, Chr(a), "") Next c.Value = strV End If Next c Case 6 For Each c In rSel.Cells c.NumberFormat = "General" If c.Value <> "" Then sDay = Right(c.Value, 2) sMonth = Mid(c.Value, 5, 2) sYear = Left(c.Value, 4) Select Case Application.International(xlDateOrder) Case 0 c.Value = DateValue(month & "/" & day & "/" & year) Case 1 c.Value = DateValue(day & "/" & month & "/" & year) Case 2 c.Value = DateValue(year & "/" & month & "/" & day) End Select End If c.NumberFormat = "dd-mmm-yyyy" Next c Case False MsgBox ("You did not select a conversion type") End Select errhandler: Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True If Err <> 0 Then MsgBox Err.Number & ", " & Err.Description End Sub

How to use:

  1. It is best to add this to a personal workbook file so it is available any time you open excel.
  2. In excel 2007 or 2010 you will need to enable the Developer tab in excel options. Once you have done this go to the developer tab.
  3. click record macro.
  4. In the Macro name type trimmer.
  5. In the store macro in drop down choose "Personal Macro Workbook".
  6. Choose a shortcut key of your choice.
  7. Click OK.
  8. Now click stop recording.
  9. Click the Visual Basic Button.
  10. Expand VBAProject (PERSONAL.XLSB)
  11. Expand Module1
  12. Just above the End Sub line paste the code excluding Sub Trimmer() and End Sub
  13. Click file menu
  14. click save Personal.xlsb
  15. close the Visual Basic Editor
 

Test the code:

  1. Attached is a sample of data that needs to be trimmed or converted. I have bound the macro to ctrl-t in the sample. Simply highly the data that needs to be converted and hit ctrl-t and choose the appropriate data type and hit enter.
 

Sample File:

sample.zip 14.11KB 

Approved by mdmackillop


This entry has been viewed 619 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express