LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Len and InStr problem manipulating strings

I need to manipulate strings and substrings in the active cell of a
spreadsheet. To do this, I need to cleanup extraneous characters in a string
to get them in a standard format. Since users enter the data manually,
cleanup is a must. Here is some problem code that should be self-explanatory
in the comments. It is greatly simplified to make sure it works before I
begin expanding it.

Code is at workbook Module level. Any help or other code routes appreciated.
Same results for Excel 2003 and 2007.

Option Compare Binary
Public Sub periodStripper()
' ActiveCell on spreadsheet formatted as "Text"
' See comment #4. In ActiveCell, want to replace any
' continuous substring of periods until only 1 remains.
' "..." or "...." would become "."
' or "...3a...2[]" would become ".3a.2[]"

Dim myStr As String
myStr = ActiveCell.Value
myStr = Trim(myStr) ' #1 trim lead/trail spaces. works OK.
myStr = Replace(myStr, " ", "") ' #2 remove any spaces. works OK.
myStr = Replace(myStr, ".[", "[") ' #3 delete period next to left bracket.
works OK.
' #4 following fails. should keep replacing any 2 periods until only 1
remains.
' Have also tried putting in a Do Loop.
myStr = Replace(myStr, "..", ".")
ActiveCell.Value = myStr
' String and results
' "....2A4." doesn't strip any periods
' "2.......[a]" strips 1 of 7 periods
' "...3a...2[]" strips 1 period from each group "..3a..2[]"
' same result with Excel 2003 and 2007
End Sub

Public Sub testStrLen()
' want to see if Period at end of string
' problem with Len and inStr
' ActiveCell Examples
' "2A4." 4 chars long, period at end
' "....2A4." 8 chars long, period at end
' "2.......[a]" 11 chars long, no period at end
Dim myStr As String
Dim myStrEnd As Integer
myStr = ActiveCell.Value
myStrLen = Len(myStr)
MsgBox ("Data: " & myStr & _
" -- Length: " & Len(myStr) & _
" -- inStr Value: " & InStr(myStrLen, myStr, "."))
' Start String followed by values that Len and InStr return
' "2A4." --- 4 chars long, period position 4 (Is Correct)
' "....2A4." --- 6 chars long, period position 6 (both wrong)
' "2.......[a]" --- 7 chars long, period position 0 (Len wrong)
' same result with Excel 2003 and 2007
End Sub

 
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
Problem using the INSTR function in VBA [email protected] Excel Programming 1 March 23rd 07 08:12 AM
Manipulating Data Problem Jeff Excel Worksheet Functions 0 June 8th 06 04:28 AM
Manipulating Strings Leslie Coover Excel Programming 2 July 27th 05 06:45 AM
PivotTable and PivotCharts Problem - Manipulating in VBA Brad[_18_] Excel Programming 1 February 19th 04 11:22 AM
Instr function problem Ross Withey Excel Programming 9 November 14th 03 08:02 AM


All times are GMT +1. The time now is 08:38 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"