How do I automate bracketing of text in an Excel cell?

K

Kevin

I have a column of text that all begin and end with a bracket ([]). The text
varies, but the start and finish should all show brackets. How do I automate
the entry of this after all the text is entered. I tried to create a macro by
typing the first bracket, then entering the "end" key and typing the end
bracket. That routine copied all the text between the brackets, as well as
the brackets. Any ideas?
 
J

Jacob Skaria

If you are looking for a macro to convert the entries to this format then try
the below macro.

Sub Macro1()
Dim lngRow As Long
For lngRow = 1 To Cells(Rows.Count, "A").End(xlUp).Row
If Range("A" & lngRow) <> "" And Left(Range("A" & lngRow), 1) <> "[" Then
Range("A" & lngRow) = "[" & Range("a" & lngRow).Text & "]"
End If
Next
End Sub

'If you are looking to automate this as soon as you type try the below
worksheet event. Select the sheet tab which you want to work with. Right
click the sheet tab and click on 'View Code'. This will launch VBE. Paste the
below code to the right blank portion. Get back to to workbook and try out.


Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 And Target.Count = 1 Then
If Target.Text <> "" And Left(Target.Text, 1) <> "[" Then
Application.EnableEvents = False
Target = "[" & Target.Text & "]"
Application.EnableEvents = True
End If
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top