Vous êtes sur la page 1sur 3

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 differe nt data types. Discussion: I use this quite often when pasting info from an sql report or from a spreadshee t and then need to work with it as a specific data type. IE dates, currencies, n umbers. Many time when it is pasted in it pastes as text. This is particularly a nnoying with dates. Sometimes there will be spaces at the beginning or end and t hen 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 wi ll 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 trail ing spaces Trimming is especially useful for ranges you will be doing vlookups o n. 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 en ter 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 an y time you open excel. 2. 3. 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. 4. click record macro. 5. In the Macro name type trimmer. 6. In the store macro in drop down choose "Personal Macro Workbook". 7. Choose a shortcut key of your choice. 8. Click OK. 9. Now click stop recording. 10. Click the Visual Basic Button. 11. Expand VBAProject (PERSONAL.XLSB) 12. Expand Module1 13. Just above the End Sub line paste the code excluding Sub Trimmer() and E nd Sub 14. Click file menu 15. click save Personal.xlsb 16. close the Visual Basic Editor Test the code: 1. Attached is a sample of data that needs to be trimmed or converted. I ha ve 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.

Vous aimerez peut-être aussi