Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Ray Ray is offline
external usenet poster
 
Posts: 267
Default Loop thru cells & isolate numeric string

Hi!

I tried to search previous posts for this, but honestly couldn't figure what search terms to use ...

My payroll system spits out a file each day that has a horrendous format .... lots of merged cells and such. I'm trying to build code that prepares this data file for use in a tracking template, the first step of which is to parse the co-worker key (name & employee #). And, it's this first step that has me stuck.

The data (co-worker name & #) starts in J19, with 53 rows btw each subsequent entry. The format for this entry is John Doe (12345678). The number should be 8 digits but isn't always; it *is* always between the ( ).

Can you help me with coding to isolate just the employee # and put in the cell next to the original ... and do this until the next cell (53 rows below) is blank?

Thanks in advance!

Ray
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Loop thru cells & isolate numeric string

Hi Ray,

Am Wed, 8 Apr 2015 05:20:43 -0700 (PDT) schrieb Ray:

The data (co-worker name & #) starts in J19, with 53 rows btw each subsequent entry. The format for this entry is John Doe (12345678). The number should be 8 digits but isn't always; it *is* always between the ( ).


column K must be empty. If not, then insert a new column.

Sub Test()
Dim varData As Variant, varOut() As Variant
Dim LRow As Long, i As Long, n As Long

With ActiveSheet
LRow = .Cells(Rows.Count, "J").End(xlUp).Row
ReDim Preserve varOut(LRow - 1, 1)
varData = .Range("J19:J" & LRow)
For i = 1 To UBound(varData)
If InStr(varData(i, 1), "(") Then
varOut(n, 0) = WorksheetFunction.Trim(Mid(varData(i, 1), _
1, InStr(varData(i, 1), "(") - 1))
varOut(n, 1) =
WorksheetFunction.Trim(Replace(Replace(Replace(var Data(i, 1), _
varOut(n, 0), ""), "(", ""), ")", ""))
Else
varOut(n, 0) = ""
varOut(n, 1) = ""
End If
n = n + 1
Next
.Range("J19").Resize(n, 2) = varOut
End With
End Sub


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional
  #3   Report Post  
Posted to microsoft.public.excel.programming
Ray Ray is offline
external usenet poster
 
Posts: 267
Default Loop thru cells & isolate numeric string

Hi Claus -

Thanks so much, works perfectly! Now I'll kill the rest of my morning dissecting the code to figure out how it works ... ;)

/ray
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Loop thru cells & isolate numeric string

Hi Ray,

Am Wed, 8 Apr 2015 06:01:50 -0700 (PDT) schrieb Ray:

Thanks so much, works perfectly! Now I'll kill the rest of my morning dissecting the code to figure out how it works ... ;)


you are welcome.
You also can do it with Regular Expressions:

Sub Test()
Dim LRow As Long, i As Long
Dim varData1 As Variant, varData2 As Variant
Dim ptrn1 As String, ptrn2 As String
Dim re As Object

'Set a reference to Microsoft VBScript Regular Expressions
With ActiveSheet
LRow = .Cells(Rows.Count, "J").End(xlUp).Row
varData1 = .Range("J19:J" & LRow)
varData2 = .Range("J19:J" & LRow)

ptrn1 = "\d|\(|\)"
ptrn2 = "\D"

Set re = New RegExp
re.Pattern = ptrn1
re.IgnoreCase = False
re.Global = True

For i = 1 To UBound(varData1)
varData1(i, 1) = WorksheetFunction.Trim(re.Replace(varData1(i,
1), ""))
Next

re.Pattern = ptrn2
re.IgnoreCase = False
re.Global = True

For i = 1 To UBound(varData2)
varData2(i, 1) = WorksheetFunction.Trim(re.Replace(varData2(i,
1), ""))
Next

.Range("J19").Resize(UBound(varData1)) = varData1
.Range("K19").Resize(UBound(varData2)) = varData2
End With

End Sub


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Loop thru cells & isolate numeric string

Il giorno mercoledì 8 aprile 2015 14:20:51 UTC+2, Ray ha scritto:
I tried to search previous posts for this, but honestly couldn't figure what search terms to use ...
My payroll system spits out a file each day that has a horrendous format .... lots of merged cells and such. I'm trying to build code that prepares this data file for use in a tracking template, the first step of which is to parse the co-worker key (name & employee #). And, it's this first step that has me stuck.
The data (co-worker name & #) starts in J19, with 53 rows btw each subsequent entry. The format for this entry is John Doe (12345678). The number should be 8 digits but isn't always; it *is* always between the ( ).
Can you help me with coding to isolate just the employee # and put in the cell next to the original ... and do this until the next cell (53 rows below) is blank?


Hi Ray,
try:

Public Sub Test1()

' --- CUSTOMIZE ----------
'
Const cstrSheet As String = "Sheet1"
Const cstrRange As String = "J19"
Const cstrFormat As String = "00000000"
'
' --- CUSTOMIZE ---------- <

Dim wbk As Excel.Workbook
Dim wsh As Excel.Worksheet
Dim rng As Excel.Range
Dim r As Long
Dim s As String
Dim p1 As Long
Dim p2 As Long

Set wbk = Application.ThisWorkbook
Set wsh = wbk.Worksheets(cstrSheet)
Set rng = wsh.Range(cstrRange)

'r = 0: s = ""
Do
s = rng.Offset(r).Value
If Len(s) = 0 Then Exit Do
p1 = InStr(s, "(")
p2 = InStr(s, ")")
rng.Offset(r, 1).Value = "'" _
& Right$(cstrFormat _
& Mid$(s, p1 + 1, p2 - p1 - 1) _
, Len(cstrFormat))
r = r + 1
Loop

Set rng = Nothing
Set wsh = Nothing
Set wbk = Nothing

End Sub

--
Ciao! :)
Maurizio


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
Loop through cells to find a string and copy/paste the matching column to a new wkbk KeriM Excel Programming 3 September 21st 12 10:40 PM
How can I isolate the last two words in a text string? Steve Gibbs Excel Discussion (Misc queries) 7 November 28th 08 11:23 PM
isolate date from a text string into another cell Tacrier Excel Discussion (Misc queries) 5 October 11th 08 12:00 AM
Loop through range of cells, string search, take action [email protected] Excel Programming 1 November 3rd 06 12:56 PM
How do I loop a range of cells until I get a numeric value? ManKind New Users to Excel 1 June 4th 06 06:48 AM


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