Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
XP XP is offline
external usenet poster
 
Posts: 389
Default Extract string from cells

Suppose I have data that looks something like:

1A-Data
1B5D-Data-4F
3RCDF-AA-Data
5L-L-Data

The strings may be any length, but I need to get any characters to the left
of the first dash in each string (row). So the above should return:

1A
1B5D
3RCDF
5L

The data resides in one column (col "I") and may be variable in the number
of rows. I would like the extracted info from each row written into the
adjacent column (Col "J").

Thanks much in advance for your example code.
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 735
Default Extract string from cells

Use the Instr function to look for the hash (-) and then take all chars left
of that position

e.g Range("J1") = Mid(Range("I1"),Instr(1,Range("I1"),"-")+1)

.... VBA code for variable length column

Dim xr As Long
With ActiveSheet
For xr = 1 To .Cells(.Rows.Count, "I").End(xlUp).Row
.Cells(xr, "J") = Mid(.Cells(xr, "I"), InStr(1, .Cells(xr, "I"), "-") +
1)
Next xr
End With
--

Regards,
Nigel




"XP" wrote in message
...
Suppose I have data that looks something like:

1A-Data
1B5D-Data-4F
3RCDF-AA-Data
5L-L-Data

The strings may be any length, but I need to get any characters to the
left
of the first dash in each string (row). So the above should return:

1A
1B5D
3RCDF
5L

The data resides in one column (col "I") and may be variable in the number
of rows. I would like the extracted info from each row written into the
adjacent column (Col "J").

Thanks much in advance for your example code.


  #4   Report Post  
Posted to microsoft.public.excel.programming
XP XP is offline
external usenet poster
 
Posts: 389
Default Extract string from cells

I changed the "Mid" to "Left" and it worked great,

Thanks Nigel...

"Nigel" wrote:

Use the Instr function to look for the hash (-) and then take all chars left
of that position

e.g Range("J1") = Mid(Range("I1"),Instr(1,Range("I1"),"-")+1)

... VBA code for variable length column

Dim xr As Long
With ActiveSheet
For xr = 1 To .Cells(.Rows.Count, "I").End(xlUp).Row
.Cells(xr, "J") = Mid(.Cells(xr, "I"), InStr(1, .Cells(xr, "I"), "-") +
1)
Next xr
End With
--

Regards,
Nigel




"XP" wrote in message
...
Suppose I have data that looks something like:

1A-Data
1B5D-Data-4F
3RCDF-AA-Data
5L-L-Data

The strings may be any length, but I need to get any characters to the
left
of the first dash in each string (row). So the above should return:

1A
1B5D
3RCDF
5L

The data resides in one column (col "I") and may be variable in the number
of rows. I would like the extracted info from each row written into the
adjacent column (Col "J").

Thanks much in advance for your example code.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Extract string from cells

Run this on a trial sheet and see if it is what you are looking for.

Sub extrData()
Dim x As String
Dim n, lr As Long
Dim c As Range
lr = Cells(Rows.Count, 9).End(xlUp).Row
Set myRng = Range("I2:I" & lr)
With myRng
For i = 2 To lr
y = Len(Range("I" & i)) - InStr(Range("I" & i), "-")
n = Len(Range("I" & i)) - (y + 1)
x = Left(Range("I" & i), n)
Range("A" & i) = x
Next
End With
End Sub

"XP" wrote:

Suppose I have data that looks something like:

1A-Data
1B5D-Data-4F
3RCDF-AA-Data
5L-L-Data

The strings may be any length, but I need to get any characters to the left
of the first dash in each string (row). So the above should return:

1A
1B5D
3RCDF
5L

The data resides in one column (col "I") and may be variable in the number
of rows. I would like the extracted info from each row written into the
adjacent column (Col "J").

Thanks much in advance for your example code.



  #6   Report Post  
Posted to microsoft.public.excel.programming
Ken Ken is offline
external usenet poster
 
Posts: 207
Default Extract string from cells

If this is a one time thing you can simply copy column L to column J
and do a search and replace of -* with nothing.
Or you can build that into your code.

Ken

On Jan 2, 3:34*pm, JLGWhiz wrote:
Run this on a trial sheet and see if it is what you are looking for.

Sub extrData()
* Dim x As String
* Dim n, lr As Long
* Dim c As Range
* lr = Cells(Rows.Count, 9).End(xlUp).Row
* Set myRng = Range("I2:I" & lr)
* * With myRng
* * * For i = 2 To lr
* * * * y = Len(Range("I" & i)) - InStr(Range("I" & i), "-")
* * * * n = Len(Range("I" & i)) - (y + 1)
* * * * x = Left(Range("I" & i), n)
* * * * Range("A" & i) = x
* * * Next
* * End With
End Sub



"XP" wrote:
Suppose I have data that looks something like:


1A-Data
1B5D-Data-4F
3RCDF-AA-Data
5L-L-Data


The strings may be any length, but I need to get any characters to the left
of the first dash in each string (row). So the above should return:


1A
1B5D
3RCDF
5L


The data resides in one column (col "I") and may be variable in the number
of rows. I would like the extracted info from each row written into the
adjacent column (Col "J").


Thanks much in advance for your example code.- Hide quoted text -


- Show quoted text -


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
Extract a value from mid string XP Excel Programming 5 March 20th 07 05:32 PM
Extract from string mark Excel Discussion (Misc queries) 2 August 8th 06 12:38 PM
extract string owl527[_7_] Excel Programming 3 November 4th 05 10:35 AM
Extract sub string sixbeforedawn Excel Worksheet Functions 2 October 24th 05 09:50 AM
extract string dreamer[_17_] Excel Programming 2 June 1st 04 12:50 PM


All times are GMT +1. The time now is 06:57 PM.

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"