Solved

Move Prefix word to left field and strip suffix string

Posted on 2016-09-01
9
52 Views
Last Modified: 2016-09-02
For the entire worksheet, I need the first word of field Project Title moved to the left field (PN).
Then, from Project Title, remove the trailing suffix

so it looks like this...

Result
I have a sample excel file attached.
Sample_MoveNumToNextFldStripTail.xlsx
0
Comment
Question by:NVIT
  • 5
  • 4
9 Comments
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41781008
Please try this....
Sub MovePrefix()
Dim lr As Long
Dim rng As Range, cell As Range
Dim Prefix As String
Application.ScreenUpdating = False
lr = Cells(Rows.Count, 3).End(xlUp).Row
Set rng = Range("C2:C" & lr)
For Each cell In rng
   If cell.Offset(0, -1) = "" Then
      Prefix = Left(cell.Value, InStr(cell.Value, " ") - 1)
      cell.Offset(0, -1) = Prefix
      cell.Replace Prefix & " ", ""
   End If
Next cell
Application.ScreenUpdating = True
End Sub

Open in new window

0
 
LVL 23

Author Comment

by:NVIT
ID: 41781011
Thanks Subodh Tiwari (Neeraj),

It does move the first word of field Project Title to the left field (PN).

Can you have it also remove the trailing suffix from Project Title?
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41781014
What do you mean by trailing suffix here?
Would you please show an example of what you are trying to achieve?
0
 
LVL 23

Author Comment

by:NVIT
ID: 41781017
In the original excel attachment Sample_MoveNumToNextFldStripTail.xlsx, each item under  Project Title has a " / total Total Fee" suffix
 before changes
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41781024
Okay try this.....
Sub MovePrefix()
Dim lr As Long
Dim rng As Range, cell As Range
Dim Prefix As String
Application.ScreenUpdating = False
lr = Cells(Rows.Count, 3).End(xlUp).Row
Set rng = Range("C2:C" & lr)
For Each cell In rng
   If cell.Offset(0, -1) = "" Then
      Prefix = Left(cell.Value, InStr(cell.Value, " ") - 1)
      cell.Offset(0, -1) = Prefix
      cell.Replace Prefix & " ", ""
      cell.Replace " / total Total Fee", ""
   End If
Next cell
Application.ScreenUpdating = True
End Sub

Open in new window

0
 
LVL 23

Author Comment

by:NVIT
ID: 41781026
Hi Neeraj,

It has an error partway. I've attached relevant images and another Excel file.
VBA ErrorRecord where error happensWorksheet after errorSampleWithError.xlsx
0
 
LVL 28

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points
ID: 41781030
Okay tweaked the code. And also I assume you don't want to move anything if the string in col. C doesn't start with numeric values. Please confirm if I am right here.

Please try this...
Sub MovePrefix()
Dim lr As Long
Dim rng As Range, cell As Range
Dim Prefix As String
Application.ScreenUpdating = False
lr = Cells(Rows.Count, 3).End(xlUp).Row
Set rng = Range("C2:C" & lr)
For Each cell In rng
   If cell.Offset(0, -1) = "" Then
      If InStr(cell.Value, " ") And IsNumeric(Left(cell.Value, 1)) Then
         Prefix = Left(cell.Value, InStr(cell.Value, " ") - 1)
         cell.Offset(0, -1) = Prefix
         cell.Replace Prefix & " ", ""
         cell.Replace " / total Total Fee", ""
      End If
   End If
Next cell
Application.ScreenUpdating = True
End Sub

Open in new window

0
 
LVL 23

Author Closing Comment

by:NVIT
ID: 41782194
It works, Neeraj!. Thanks so much.
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41782204
You're welcome. Glad to help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My experience with Windows 10 over a one year period and suggestions for smooth operation
Outlook Free & Paid Tools
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now