Solved

Move Prefix word to left field and strip suffix string

Posted on 2016-09-01
9
47 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

707 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

14 Experts available now in Live!

Get 1:1 Help Now