Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 57
Default select case to replace text with different text

I'm trying to use a Select Case in a Private Sub Worksheet_Change event to
do the following:

if I type w in a cell in col B, I want to replace it with WIDGETS
if I type g in a cell in col B, I want to replace it with GIDGETS

seems like it should be simple but I can't come up with the code.




  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 913
Default select case to replace text with different text

On Sun, 10 Jan 2010 10:28:54 -0600, "John" wrote:

I'm trying to use a Select Case in a Private Sub Worksheet_Change event to
do the following:

if I type w in a cell in col B, I want to replace it with WIDGETS
if I type g in a cell in col B, I want to replace it with GIDGETS

seems like it should be simple but I can't come up with the code.




Try this:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(ActiveSheet.Range("B:B"), Target) Is Nothing Then
Select Case Target.Value
Case "w"
Target.Value = "WIDGETS"
Case "g"
Target.Value = "GIDGETS"
End Select
End If
End Sub

Hope this helps / Lars-Åke
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 834
Default select case to replace text with different text

Private Sub Worksheet_Change(ByVal Target As Range)
Const WS_RANGE As String = "B:B" '<== change to suit

On Error GoTo ws_exit
Application.EnableEvents = False

If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
Select Case Target.Value

Case "w": Target.Value = "WIDGETS"
Case "g": Target.Value = "GIDGETS"
End Select
End If

ws_exit:
Application.EnableEvents = True
End Sub

'This is worksheet event code, which means that it needs to be
'placed in the appropriate worksheet code module, not a standard
'code module. To do this, right-click on the sheet tab, select
'the View Code option from the menu, and paste the code in.

---
HTH

Bob Phillips

"John" wrote in message
...
I'm trying to use a Select Case in a Private Sub Worksheet_Change event to
do the following:

if I type w in a cell in col B, I want to replace it with WIDGETS
if I type g in a cell in col B, I want to replace it with GIDGETS

seems like it should be simple but I can't come up with the code.






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default select case to replace text with different text

I know you asked for a Select Case solution, but (assuming you are only
talking about single letter codes) there is a more compact coding solution
available using the Choose function for what you want to do...

Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Target.Column = 2 Then Target.Value = Choose(InStr(1, "wgd", _
Target.Value, vbTextCompare), "WIDGETS", "GIDGETS", "DIGITS")
Application.EnableEvents = True
End Sub

I added one more item to make it more obvious how to structure the code. The
"wgd" correspond to the codes you want to type in the cell ("w" for WIDGETS,
"g" for GIDGETS and "d" for DIGITS) where the letter you type into the cell
can be either upper or lower case (that is what the vbTextCompare argument
in the InStr function call makes possible). The order of the letters in the
"wgd" string must be the same as the listed order of what you want these
letters replaced with in the arguments to the Choose function (as my code
shows).

--
Rick (MVP - Excel)


"John" wrote in message
...
I'm trying to use a Select Case in a Private Sub Worksheet_Change event to
do the following:

if I type w in a cell in col B, I want to replace it with WIDGETS
if I type g in a cell in col B, I want to replace it with GIDGETS

seems like it should be simple but I can't come up with the code.





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
Use Select / Case to find certain text marlea[_10_] Excel Programming 4 February 2nd 06 12:05 PM
Change the text from lower case to upper case in an Excel work boo dave01968 Excel Discussion (Misc queries) 2 December 9th 05 09:09 AM
Select Case with Text Marie Excel Programming 3 February 10th 05 05:31 PM
Select Case from Text Box Input mackerma[_2_] Excel Programming 1 October 19th 04 08:14 PM
Select Case from Text Box Input mackerma Excel Programming 0 October 15th 04 08:50 PM


All times are GMT +1. The time now is 06:12 AM.

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

About Us

"It's about Microsoft Excel"