Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 72
Default how to specify only if 3 characters or less

I download a report that has a column heading "Name" and is in column D. The
name is last name then a comma then a space then the first name, for example
SMITH, FRED.

I would like to write a macro that would generate a report to pull only the
last names with 3 characters or less. That new report would be on a sheet
titled, "Short Names". What I want to accomplish is having a sheet that
lists all the 3 character last names or less from the main report called "All
Records". Names like "Ray", "Fox", "Lee" or " Hu". Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default how to specify only if 3 characters or less

Sub GetShortNames()
Dim sh as Worksheet, sh1 as Worksheet
Dim rw as Long, s as String, ipos as long
Dim cell as Range, rng as Range
Set sh = Activesheet
With sh
set rng = .Range(.Cells(2,"D"),.Cells(rows.count,"D").End(xl up))
End with
rw = 1
worksheets.Add after:=sheets(sheets.count)
set sh1 = Activesheet
sh.Activate
for each cell in rng
s = Replace(Trim(cell.Value)," ","")
ipos = instr(1,s,",",vbTextCompare)
if ipos <= 4 and ipos < 0 then
s1 = left(s,ipos-1)
sh1.Cells(rw,1).Value = s1
rw = rw + 1
end if
Next
End Sub

--
Regards,
Tom Ogilvy

"JOUIOUI" wrote in message
...
I download a report that has a column heading "Name" and is in column D.

The
name is last name then a comma then a space then the first name, for

example
SMITH, FRED.

I would like to write a macro that would generate a report to pull only

the
last names with 3 characters or less. That new report would be on a sheet
titled, "Short Names". What I want to accomplish is having a sheet that
lists all the 3 character last names or less from the main report called

"All
Records". Names like "Ray", "Fox", "Lee" or " Hu". Thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 72
Default how to specify only if 3 characters or less

Hi Tom,

This worked great, however I didn't state my question properly, I would like
to copy the entire row for each of these cases of names with 3 characters or
less. I was looking at the code and not quite sure where I would specify
that. Also how would I specify to place this data in the sheet called "Short
Names" rather than the last sheet in the workbook.

Thanks so much for your help! Enjoy your day.
Joyce

"Tom Ogilvy" wrote:

Sub GetShortNames()
Dim sh as Worksheet, sh1 as Worksheet
Dim rw as Long, s as String, ipos as long
Dim cell as Range, rng as Range
Set sh = Activesheet
With sh
set rng = .Range(.Cells(2,"D"),.Cells(rows.count,"D").End(xl up))
End with
rw = 1
worksheets.Add after:=sheets(sheets.count)
set sh1 = Activesheet
sh.Activate
for each cell in rng
s = Replace(Trim(cell.Value)," ","")
ipos = instr(1,s,",",vbTextCompare)
if ipos <= 4 and ipos < 0 then
s1 = left(s,ipos-1)
sh1.Cells(rw,1).Value = s1
rw = rw + 1
end if
Next
End Sub

--
Regards,
Tom Ogilvy

"JOUIOUI" wrote in message
...
I download a report that has a column heading "Name" and is in column D.

The
name is last name then a comma then a space then the first name, for

example
SMITH, FRED.

I would like to write a macro that would generate a report to pull only

the
last names with 3 characters or less. That new report would be on a sheet
titled, "Short Names". What I want to accomplish is having a sheet that
lists all the 3 character last names or less from the main report called

"All
Records". Names like "Ray", "Fox", "Lee" or " Hu". Thanks




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default how to specify only if 3 characters or less

Sub GetShortNames()
Dim sh as Worksheet, sh1 as Worksheet
Dim rw as Long, s as String, ipos as long
Dim cell as Range, rng as Range
Set sh = Activesheet
With sh
set rng = .Range(.Cells(2,"D"),.Cells(rows.count,"D").End(xl up))
End with
rw = 1
'worksheets.Add after:=sheets(sheets.count)
set sh1 = Worksheets("Short")
sh.Activate
for each cell in rng
s = Replace(Trim(cell.Value)," ","")
ipos = instr(1,s,",",vbTextCompare)
if ipos <= 4 and ipos < 0 then
's1 = left(s,ipos-1)
sh1.Cells(rw,1).Value = cell.Value
'or for the whole row
'cell.EntireRow.copy sh1.Cells(rw,1)
rw = rw + 1
end if
Next
End Sub


--
Regards,
Tom Ogilvy


"JOUIOUI" wrote in message
...
Hi Tom,

This worked great, however I didn't state my question properly, I would

like
to copy the entire row for each of these cases of names with 3 characters

or
less. I was looking at the code and not quite sure where I would specify
that. Also how would I specify to place this data in the sheet called

"Short
Names" rather than the last sheet in the workbook.

Thanks so much for your help! Enjoy your day.
Joyce

"Tom Ogilvy" wrote:

Sub GetShortNames()
Dim sh as Worksheet, sh1 as Worksheet
Dim rw as Long, s as String, ipos as long
Dim cell as Range, rng as Range
Set sh = Activesheet
With sh
set rng = .Range(.Cells(2,"D"),.Cells(rows.count,"D").End(xl up))
End with
rw = 1
worksheets.Add after:=sheets(sheets.count)
set sh1 = Activesheet
sh.Activate
for each cell in rng
s = Replace(Trim(cell.Value)," ","")
ipos = instr(1,s,",",vbTextCompare)
if ipos <= 4 and ipos < 0 then
s1 = left(s,ipos-1)
sh1.Cells(rw,1).Value = s1
rw = rw + 1
end if
Next
End Sub

--
Regards,
Tom Ogilvy

"JOUIOUI" wrote in message
...
I download a report that has a column heading "Name" and is in column

D.
The
name is last name then a comma then a space then the first name, for

example
SMITH, FRED.

I would like to write a macro that would generate a report to pull

only
the
last names with 3 characters or less. That new report would be on a

sheet
titled, "Short Names". What I want to accomplish is having a sheet

that
lists all the 3 character last names or less from the main report

called
"All
Records". Names like "Ray", "Fox", "Lee" or " Hu". Thanks






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
convert 5 characters in a cell to 6 characters by adding a zero Helenf Excel Discussion (Misc queries) 4 May 18th 09 04:43 PM
Insert Leading Characters If String Is Only 7 Characters Paperback Writer Excel Discussion (Misc queries) 2 April 21st 09 09:07 PM
In Excel find characters when multiple characters exist w/i a cel teacher-deburg Excel Worksheet Functions 1 December 5th 05 10:22 PM
HOW DO I EXTRACT ALL CHARACTERS AFTER 5 CHARACTERS ON LEFT GRYSYF Excel Worksheet Functions 5 October 12th 05 10:58 AM
What do these characters mean? sebastienm Excel Programming 0 September 20th 04 07:05 PM


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