Solved

Move Prefix word to left field and strip suffix string

Posted on 2016-09-01
9
83 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 31

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 24

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 31

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
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
LVL 24

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
 
LVL 31

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 24

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 31

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 24

Author Closing Comment

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

Expert Comment

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

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

695 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