Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Move Prefix word to left field and strip suffix string

Posted on 2016-09-01
9
Medium Priority
?
103 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 32

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 25

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 32

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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 25

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 32

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 25

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 32

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 2000 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 25

Author Closing Comment

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

Expert Comment

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

Featured Post

Office 365 Training for Admins - 7 Day Trial

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.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
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…

722 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