?
Solved

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

Posted on 2014-11-04
14
Medium Priority
?
276 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
[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
  • 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 1200 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 

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 46

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 800 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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,…
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
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…

765 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