Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Removing text

Hello-

Is there a simple macro already written that removes text from cells? My
situation has me importing data into a spreadsheet from another
application. This application stores time information, a duration of
time, with the corresponding unit, hours, days, etc. For example, 2 hrs,
1 hr, 5 days, 1 day, etc. Unfortunately this is also how it imports into
Excel. And, I'm actually doing a copy-paste, not an import.

It's useful for me to add the durations. In order to do this, I can
think of only one way, to remove the text that represents the unit.
Having not worked with the macro language in Excel, I'm curious if there
is code already written to do this.

Thanks.
-Kurt

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 135
Default Removing text

Kurt,

This will take imported data from column 3 and put the value in column 1 and
the unit in column 2.
(Assumes the data starts on Row 1. If not, have the For/Next loop start
with the Row number of the first row of data).

Sub Tester()

LastRow = Cells(65536, 3).End(xlUp).Row
For i = 1 To LastRow
sString = Cells(i, 3)
Cells(i, 1) = Val(sString)
sUnit = Trim(Mid(sString, 2))

'If unit is not plural, make it plural
If Right(sUnit, 1) < "s" Then _
sUnit = sUnit & "s"

Cells(i, 2) = sUnit
Next i

End Sub

HTH,
Shockley


"Kurt Radecki" wrote in message
...
Hello-

Is there a simple macro already written that removes text from cells? My
situation has me importing data into a spreadsheet from another
application. This application stores time information, a duration of
time, with the corresponding unit, hours, days, etc. For example, 2 hrs,
1 hr, 5 days, 1 day, etc. Unfortunately this is also how it imports into
Excel. And, I'm actually doing a copy-paste, not an import.

It's useful for me to add the durations. In order to do this, I can
think of only one way, to remove the text that represents the unit.
Having not worked with the macro language in Excel, I'm curious if there
is code already written to do this.

Thanks.
-Kurt



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Removing text

Perfect! Thanks so much. -Kurt


shockley wrote:
Kurt,

This will take imported data from column 3 and put the value in column 1 and
the unit in column 2.
(Assumes the data starts on Row 1. If not, have the For/Next loop start
with the Row number of the first row of data).

Sub Tester()

LastRow = Cells(65536, 3).End(xlUp).Row
For i = 1 To LastRow
sString = Cells(i, 3)
Cells(i, 1) = Val(sString)
sUnit = Trim(Mid(sString, 2))

'If unit is not plural, make it plural
If Right(sUnit, 1) < "s" Then _
sUnit = sUnit & "s"

Cells(i, 2) = sUnit
Next i

End Sub

HTH,
Shockley


"Kurt Radecki" wrote in message
...

Hello-

Is there a simple macro already written that removes text from cells? My
situation has me importing data into a spreadsheet from another
application. This application stores time information, a duration of
time, with the corresponding unit, hours, days, etc. For example, 2 hrs,
1 hr, 5 days, 1 day, etc. Unfortunately this is also how it imports into
Excel. And, I'm actually doing a copy-paste, not an import.

It's useful for me to add the durations. In order to do this, I can
think of only one way, to remove the text that represents the unit.
Having not worked with the macro language in Excel, I'm curious if there
is code already written to do this.

Thanks.
-Kurt





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Removing text

I like:


Sub changeTexttoValue()
'Replaces the "unit" with a value for the unit
'I.E. day = 24 hrs
Dim cell As Range
Dim MyRange As Range
Dim ValueinCell As Variant
Application.Sheets("Sheet1").Select
Range("A1").SpecialCells(xlLastCell).Select
Set MyRange = Range(Cells(1, 1), _
Cells(ActiveCell.Row, ActiveCell.Column))
For Each cell In MyRange
ValueinCell = Val(cell.Value)
If InStr(cell.Value, "day") Then
ValueinCell = ValueinCell * 24
End If
If InStr(cell.Value, "hr") Then
ValueinCell = ValueinCell
End If

'For testing
If cell.Value < "" Then
Debug.Print cell.Address, ValueinCell
End If

'Replaces the value in each cell
If cell.Value < "" Then
cell.Value = ValueinCell
End If

Next cell
Range("A1").Select
End Sub

Ken

"Kurt Radecki" wrote in message
...
Hello-

Is there a simple macro already written that removes text from cells? My
situation has me importing data into a spreadsheet from another
application. This application stores time information, a duration of
time, with the corresponding unit, hours, days, etc. For example, 2 hrs,
1 hr, 5 days, 1 day, etc. Unfortunately this is also how it imports into
Excel. And, I'm actually doing a copy-paste, not an import.

It's useful for me to add the durations. In order to do this, I can
think of only one way, to remove the text that represents the unit.
Having not worked with the macro language in Excel, I'm curious if there
is code already written to do this.

Thanks.
-Kurt



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing Text Rob Excel Discussion (Misc queries) 1 January 5th 09 06:39 PM
Removing Text infinite1013 Excel Discussion (Misc queries) 5 August 4th 08 04:55 PM
Removing text Dave Excel Discussion (Misc queries) 13 July 23rd 08 09:02 PM
Removing text from a cell with text and numbers DoubleZ Excel Discussion (Misc queries) 2 July 8th 08 10:14 PM
Removing text pokdbz Excel Discussion (Misc queries) 10 August 7th 07 01:44 AM


All times are GMT +1. The time now is 09:46 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"