Changing my VBA to effect a range of cells

cjcass

Well-known Member
Joined
Oct 27, 2011
Messages
680
Office Version
  1. 2016
Platform
  1. Windows
Hi,
Hope you might be able to help me with this. I have the following code (see below) that effectively concatenates 3 cells (A1, B1 & C1) adding dividers and putting the result into D1. It also applies font bolding to the A1 and C1 bits.
What would the code be to run this macro for all cells 100 rows down; so getting it to enter the results into cells D1 to D100 for all the corresponding values in columns A,B&C?
Many thanks,
C.

Sub Macro1()


Dim Part1Len, Part2Len, DividerLen As Integer
Dim Divider As String
Part1Len = Len(Range("A1")) + 2
Part2Len = Len(Range("B1"))
Part3Len = Len(Range("C1"))
Divider = " - "
DividerLen = Len(Divider)


Range("D1") = "[" & Range("A1") & "]" & Divider & Range("B1") & Divider & Range("C1")
With Range("D1").Characters(Start:=1, Length:=Part1Len).Font
.FontStyle = "Bold"
With Range("D1").Characters(Start:=Part1Len + DividerLen + Part2Len + DividerLen, Length:=Part3Len).Font
.FontStyle = "Bold"
With Range("D1").Characters(Start:=Part1Len + DividerLen + 1, Length:=Part2Len).Font
.FontStyle = "Regular"
End With
End With
End With


End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
try

Code:
Sub Macro1()

 Dim Part1Len, Part2Len, DividerLen As Integer, i As Long
 Dim Divider As String
 For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
 Part1Len = Len(Range("A" & i)) + 2
 Part2Len = Len(Range("B" & i))
 Part3Len = Len(Range("C" & i))
 Divider = " - "
 DividerLen = Len(Divider)

 Range("D" & i) = "[" & Range("A" & i) & "]" & Divider & Range("B" & i) & Divider & Range("C" & i)
 With Range("D" & i).Characters(Start:=1, Length:=Part1Len).Font
 .FontStyle = "Bold"
 With Range("D" & i).Characters(Start:=Part1Len + DividerLen + Part2Len + DividerLen, Length:=Part3Len).Font
 .FontStyle = "Bold"
 With Range("D" & i).Characters(Start:=Part1Len + DividerLen + 1, Length:=Part2Len).Font
 .FontStyle = "Regular"
 End With
 End With
 End With
Next
 End Sub
 
Upvote 0
Make a copy of your workbook and replace all of your existing code with below, then try:
Code:
Sub Macro1()


    Dim x       As Long
    Dim y       As Long
    Dim str     As String
    Dim temp    As String
    Const delim As String = " - "
    
    str = "[@1]" & delim & "@2" & delim & "@3"
    
    Application.ScreenUpdating = False
    
    For x = 1 To Cells(Rows.Count, 1).End(xlUp).row
        temp = str
        For y = 1 To 3
            temp = Replace(temp, "@" & CStr(y), Cells(x, y).Value)
            temp = Replace(temp, "@" & CStr(y), Cells(x, y).Value)
            temp = Replace(temp, "@" & CStr(y), Cells(x, y).Value)
        Next y
        
        Cells(x, 4).Value = temp
        FormatCell Cells(x, 4), delim
        
        temp = vbNullString
    Next x
    
    Application.ScreenUpdating = True
    
End Sub
Private Sub FormatCell(ByRef rng As Range, ByRef delim As String)


    Dim arr(1 To 3) As Long
    Dim x           As Long
    
    With rng
        .Characters.Font.FontStyle = "Regular"
        For x = 1 To 3
            arr(x) = Len(.Offset(, -4 + x).Value)
            If x <> 3 Then arr(x) = arr(x) + Len(delim)
        Next x
        arr(1) = 2 + arr(1)
        
        .Characters.Font.FontStyle = "Bold"
        .Characters(arr(1) + 1, arr(2)).Font.FontStyle = "Regular"
        
    End With
    
    Erase arr
    
End Sub
Nevermind, Barry beat me to it!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,212
Messages
6,129,540
Members
449,515
Latest member
lukaderanged

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