Solved

Move Prefix word to left field and strip suffix string

Posted on 2016-09-01
9
56 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 29

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 29

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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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 29

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 29

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 29

Expert Comment

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

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Outlook Free & Paid Tools
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

810 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