Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 72
Default Altering code to accomodate empty space

I'm using this code to copy and paste 3 character or less names to a sheet in
the workbook called "Short Name". This code picks up names that are in the
column D like "Lee, Barbara" or "Ray, Joyce". The problem is sometimes the
names display with a space before the comma, like "Lee ,Barbara" or "Ray
,Joyce". How can I alter this code to pick up both scenarios? Thanks a
bunch for your help.

Sub ShortName()

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(xlUp))
End With
rw = 1
Set sh1 = Worksheets("Short Name")
sh.Activate
For Each cell In rng
s = Replace(Trim(cell.Value), " ", "")
ipos = InStr(1, s, ",", vbTextCompare)
If ipos <= 4 And ipos < 0 Then
cell.EntireRow.Copy sh1.Cells(rw, 1)
rw = rw + 1
End If
Next
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Altering code to accomodate empty space

Hi Jouioui,

Have you tried the code with the indicatrd names?

Unless I misunderstand you, the code works for names with, or without, a
space preceding the comma.


---
Regards,
Norman



"JOUIOUI" wrote in message
...
I'm using this code to copy and paste 3 character or less names to a sheet
in
the workbook called "Short Name". This code picks up names that are in
the
column D like "Lee, Barbara" or "Ray, Joyce". The problem is sometimes
the
names display with a space before the comma, like "Lee ,Barbara" or "Ray
,Joyce". How can I alter this code to pick up both scenarios? Thanks a
bunch for your help.

Sub ShortName()

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(xlUp))
End With
rw = 1
Set sh1 = Worksheets("Short Name")
sh.Activate
For Each cell In rng
s = Replace(Trim(cell.Value), " ", "")
ipos = InStr(1, s, ",", vbTextCompare)
If ipos <= 4 And ipos < 0 Then
cell.EntireRow.Copy sh1.Cells(rw, 1)
rw = rw + 1
End If
Next
End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 72
Default Altering code to accomodate empty space

I'm sorry Norman, I made a mistake, where this code does not work is in names
like "Na , George" or "Hu , Jane". Do you have any idea how I could use code
to delete the space between the letter and the comma?

thanks so much

"Norman Jones" wrote:

Hi Jouioui,

Have you tried the code with the indicatrd names?

Unless I misunderstand you, the code works for names with, or without, a
space preceding the comma.


---
Regards,
Norman



"JOUIOUI" wrote in message
...
I'm using this code to copy and paste 3 character or less names to a sheet
in
the workbook called "Short Name". This code picks up names that are in
the
column D like "Lee, Barbara" or "Ray, Joyce". The problem is sometimes
the
names display with a space before the comma, like "Lee ,Barbara" or "Ray
,Joyce". How can I alter this code to pick up both scenarios? Thanks a
bunch for your help.

Sub ShortName()

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(xlUp))
End With
rw = 1
Set sh1 = Worksheets("Short Name")
sh.Activate
For Each cell In rng
s = Replace(Trim(cell.Value), " ", "")
ipos = InStr(1, s, ",", vbTextCompare)
If ipos <= 4 And ipos < 0 Then
cell.EntireRow.Copy sh1.Cells(rw, 1)
rw = rw + 1
End If
Next
End Sub




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
How do I free up the space occupied by empty rows? davedadu Excel Discussion (Misc queries) 2 September 25th 09 05:37 PM
Trim Characters Other Than An Empty Space At The End Of A Cell K8_Dog Excel Worksheet Functions 5 April 19th 08 01:29 AM
Empty space around charts when copying BillRobson Excel Discussion (Misc queries) 1 June 21st 05 06:08 PM
Empty space in formulas. Arla M Excel Worksheet Functions 4 January 27th 05 06:35 AM
Code modification to accomodate scaling and print areas. Phil Hageman[_3_] Excel Programming 2 February 26th 04 02:06 PM


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