View Single Post
  #15   Report Post  
Posted to microsoft.public.excel.programming
DK DK is offline
external usenet poster
 
Posts: 21
Default Need help - extracting data from cell

Hey JackL,
Thanks for this. It worked like a charm!! ;o) Just a slight issue, it
gives a runtime error 6, overflow.

Any ideas how to fix this?

Thank you so very much for your help!
Regards
Dilpreet

JackL wrote:

Try this:

Sub ExtractNumbersFromText()
Dim row As Integer
Dim col As Integer
For Each c In Range("C2:C65536")
ms = ""
For i = 1 To Len(c.Value)
x = Mid(c.Value, i, 1)
If x Like "*[0-9]*" Then
ms = ms & Mid(c, i, 1)
End If
Next i
row = c.Cells.row
col = c.Cells.column
'Set the + 1 below to select the column you want
Cells(row, col + 1) = "'" + Left(Trim(ms), 5)
Next c
End Sub

"DK" wrote:

Jack:
This is how I modified the text
Sub ExtractNumbersFromText()
Dim row As Integer
Dim col As Integer
Dim cell As Integer
For Each c In Range("c2:c65536")
ms = ""
For i = 1 To Len(c.Value)
x = Mid(c.Value, i, 1)
If x Like "*[0-9]*" Then
ms = ms & Mid(c, i, 1)
End If
Next i
Application.Trim (ms)
cell = Range("f2").Select
row = 2
col = 3
Cells(row, col) = "'" + Left(ms, 5)
cell = cell + 1
Next c
End Sub

There is a problem still. The value is coming in cell C2. THat is it. I
need the value in cell F2 for the cell C2 and then increment the value
until it reaches last row.
Pls recommend the changes. Thanks!
JackL wrote:
Hi DK,
The message box is just to see that it is doing what you want. You can
comment it out with a ' and put the value of ms in whatever cell you want.
Use the Left function to extract the first 5 characters i.e.

Cells(row,col) = left(ms,5)

You will have to play around with the format. ms is a string but when you
place the value in a cell it thinks it's a number. i.e

Cells(row,col) = " ' " + left(ms,5) might work for you.

Regards.

"DK" wrote:

Hi Jack,
There is a slight issue:
1. It gives me the value in a display box instead of a cell which I
think can be easily fixed.
2. The cell has more numbers like a date 07.12.03 and this macro is
picking up that as well.

Can you please suggest the changes?
Thanks!
JackL wrote:
Hi Kaur,
This modified reply from Don Guillett to another question should also work
for you.
Set the range as required.
Regards.

Sub ExtractNumbersFromText()
For Each c In Range("a1:a5")
ms = ""
For i = 1 To Len(c.Value)
x = Mid(c.Value, i, 1)
If x Like "*[0-9]*" Then
ms = ms & Mid(c, i, 1)
End If
Next i
MsgBox Application.Trim(ms)
Next c
End Sub

" wrote:

Hi!
I am fairly new to the programming area. And this one I really need
help on. I am importing a text file in excel and then running a macro
to extract useful data.
P:\DAVID\00019\xyz.wpd
P:\DAVID\00019\C\abc.wpd
P:\DAVID\CTC\Baker\99114\abc.wpd
P:\DAVID\CTC\Baker\99114\xyz.wpd
P:\DAVID\CTC\FGH\01161\abc.wpd

The first thing I need to do is to extract the five digit number from
this directory listing in each cell. Now, I thought it was pretty
simple because I could use Mid formula but the problem arises when the
location of the numbers change in every cell.

I am not very good at writing an IF clause for extracting this kind of
data,.

Please help!