Solved

vba to insert space in front of first character and then a space after the 12th

Posted on 2014-11-04
14
238 Views
Last Modified: 2014-11-06
Could an expert provide me with VBA that will do the following please:

If the cell begins with ABC remove ABC then insert space in front of first character and then insert a space after the 12th character but keep all of the data in the cell.

If the cell does not begin with ABC then insert space in front of first character and then insert a space after the 12th character but keep all of the data in the cell.

i.e.

if the cell contains

ABCXS1426698194.001//CPN121780 change to [space]XS1426698194 .001//CPN121780

if the cell contains

XS1426698194.001//CPN121780 change to [space]XS1426698194 .001//CPN121780


Hope this is self explanitory

Thanks
0
Comment
Question by:Jagwarman
  • 7
  • 3
  • 2
  • +2
14 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40421641
Where? Over the entire workbook, all spreadsheets? Over one column? Over a selection?
0
 

Author Comment

by:Jagwarman
ID: 40421662
Sorry good point. column 'N'
0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 300 total points
ID: 40421684
Please save your spreadsheet before running this code.

Sub UpdateColumn()
For introw = 1 To 65000
    If Cells(introw, 14) <> "" Then
        If Left(Cells(introw, 14), 3) = "ABC" Then
            Cells(introw, 14) = Mid(Cells(introw, 14), 4, 9999)
        End If
        Cells(introw, 14) = " " & Left(Cells(introw, 14), 12) & " " & Mid(Cells(introw, 14), 13, 9999)
    End If
Next
End Sub

Open in new window

0
 
LVL 2

Expert Comment

by:Glen Richmond
ID: 40421711
VBA is as follows (also example on how to use function as cell function attached)

Function ParseString(myString As String)
      'myString will = "ABCXS1426698194.001//CPN121780 change to [space]XS1426698194 .001//CPN121780"
      
      'optional triim if you want to?
      myString = Trim(myString)
      
      'take off the ABC
      If Left(myString, 3) = "ABC" Then
            myString = Mid(myString, 4)
            
      End If
      
      'now add the spaces
      myString = " " & myString & " "
      
      ParseString = myString
      
End Function

Open in new window

Example.xlsm
0
 
LVL 2

Expert Comment

by:Glen Richmond
ID: 40421733
Phillip Burton
Excel no longer limited to 65k rows..
wouldnt usedrows be a better option?

or my solution which acts as a user defined in cell function.
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40421750
I know Excel is no longer limited to 65,536 rows. But I doubt that Jagwarman's data goes that far. And if it does, he can modify it if he wishes.
0
 
LVL 2

Expert Comment

by:Glen Richmond
ID: 40421760
sorry but i hate built in limitation, or hard coding where it can be avoided.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:Jagwarman
ID: 40421768
Glen,

I am not familiar with functions, apologies for being dumb but how do I get yours to run
0
 
LVL 2

Expert Comment

by:Glen Richmond
ID: 40421811
if you want to include into your workbook, include the modTest into your VBAProject.
(export from example, import into yours)
then in the colum cells add the =parsestring(<col&cell ref>)
as you would do any normal in cell function.

Attached a image to try and help..
example.png
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40421828
I don't see the 12th character space in your two examples.
If the ABC is removed, does the 12th character start with the new first character or the original first character.  Or does the 12th character start with the pre-pended space character?
0
 
LVL 2

Expert Comment

by:Glen Richmond
ID: 40421837
sorry missed that, you want where you have put [space] to be an actual space?
12th chr from the left once the ABC has been removed and leading space added?
Ill make a example you can move where the 12th chr is my editing the code..
0
 
LVL 2

Expert Comment

by:Glen Richmond
ID: 40421847
example with adding in 12th pos chr..

you should have all you need in that function now to pull that string appart as much as you want :)
0
 
LVL 18

Assisted Solution

by:krishnakrkc
krishnakrkc earned 200 total points
ID: 40421892
Hi

Select the range and run this macro.

Option Explicit

Sub kTest()
    
    Dim x   As String
    
    With Selection
        x = .Address
        .Value = Evaluate("=IF(LEFT(" & x & ",3)=""abc"","" ""&REPLACE(MID(" & x & ",4,LEN(" & x & ")),12,1,"" ""),"" ""&REPLACE(" & x & ",12,1,"" ""))")
    End With
    
End Sub

Open in new window


Kris
0
 
LVL 2

Expert Comment

by:Glen Richmond
ID: 40421913
sorry no idea whay second example didnt attach...
here it is (i hope)
Example2.xlsm
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

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

17 Experts available now in Live!

Get 1:1 Help Now