VBA to replace text between brackets

Giggs1991

Board Regular
Joined
Mar 17, 2019
Messages
50
In sheet1, the value of cell A1 is [ABCD] including the brackets. Whent he code executes, the value inside the bracket should change to the contacts of the selected cell in sheet2. For example, If I select Cell A2 in sheet2, the contents on A1 in sheet1 should change from [ABCD] to [contents of A2 in sheet2]. If I select Cell A5 in seet2, the contents on A1 in sheet1 should change from [ABCD] to [contents of A5 in sheet2].
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Try putting this in the ThisWorkbook code module
Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    Dim keyCell As Range
    Set keyCell = Me.Sheets("Sheet1").Range("A1")
    If Target.Cells.Count = 1 Then
        If Target.Address <> keyCell.Address Or Sh.Name <> keyCell.Parent.Name Then
            Application.EnableEvents = False
            keyCell.Value = "[" & Target.Text & "]"
            Application.EnableEvents = True
        End If
    End If
End Sub
 
Upvote 0
Try this code in Sheet2
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim a As Variant
    a = "[" & Target.Value & "]"
    Sheets("Sheet1").Range("A1") = a
End Sub
 
Upvote 0
Thank you for the above solution. However, I am also looking for a solution to another similar issue.

For example, I have the following text in range from A1 to A10 in sheet1 : what[abc]isthis.

If I select only the range from A3 to A5, and run the macro, it should replace only the contents between the square brackets for range A3 to A5 with the contents of selected range in sheet2. This should happen as a macro and not as an event. Also, this macro should not replace the contents of cell A1 , A6,A7,A8,A9,A10 since they have not been selected.
 
Upvote 0
Thanks Phuoc. I just asked 1 more question below for a similar issue below :

For example, I have the following text in range from A1 to A10 in sheet1 : what[abc]isthis.

If I select only the range from A3 to A5, and run the macro, it should replace only the contents between the square brackets for range A3 to A5 with the contents of selected range in sheet2. This should happen as a macro and not as an event. Also, this macro should not replace the contents of cell A1 , A6,A7,A8,A9,A10 since they have not been selected.
 
Upvote 0
For example, I have the following text in range from A1 to A10 in sheet1 : what[abc]isthis.

If I select only the range from A3 to A5, and run the macro, it should replace only the contents between the square brackets for range A3 to A5 with the contents of selected range in sheet2. This should happen as a macro and not as an event. Also, this macro should not replace the contents of cell A1 , A6,A7,A8,A9,A10 since they have not been selected.
Give this macro a try...
Code:
Sub ReplaceSelectCellsWithSheet2Selection()
  Dim Sht2Content As String
  Application.ScreenUpdating = False
  Sheets("Sheet2").Select
  Sht2Content = ActiveCell.Value
  Sheets("Sheet1").Select
  Selection.Replace "[*]", "[" & Sht2Content & "}", xlPart, , , , False, False
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,758
Messages
6,126,704
Members
449,331
Latest member
smckenzie2016

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top