Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50068

How to apply in one shot a macro on different columns instead of one column?

$
0
0
Hi Excel experts!

My sheet looks like that:
Code:

          A                    B          C          D          E          F        G
1        This is string1  his        0000      ng1      0000    st      0000
2        A new text      ew        0000      ext        0000   
...

For each line, the macro below finds the substring in B and bold it in A. Like that:
Code:

          A                    B          C          D          E          F        G
1        This is string1  his        0000      ng1      0000    st      0000
2        A new text      ew        0000      ext        0000   
...

Is it possible to modify it to find all the substrings contained in B, D and F (the lines can have different number of substring) and bold them in A at once?
To obtain that:
Code:

          A                    B          C          D          E          F        G
1        This is string1  his        0000      ng1      0000    st      0000
2        A new text      ew        0000      ext        0000
...

My skills in Excel programming are very limited, if someone could tell me if it is at least doable...
Thanks !

Macro:
Code:

Sub formatting()

Dim heightA As Long
Dim heightB As Long
Dim height As Long
Dim i As Long

heightA = Cells(Rows.Count, 1).End(xlUp).Row
heightB = Cells(Rows.Count, 2).End(xlUp).Row
If heightA < heightB Then
    height = heightA
Else
    height = heightB
End If

If height > 1 Then
    For i = 1 To height
        Range("A" & i).Font.Bold = False
        ''Replace the formula with the full string
        Range("A" & i) = Range("A" & i).Value
        'Find the starting position of the string in B within the string produced by the formula
        If Range("B" & i).Value <> "" Then
            myPos = InStr(1, Range("A" & i).Value, Range("B" & i).Value, 1)
            If myPos > 0 Then
                'Bold the string from B column
                Range("A" & i).Characters(myPos, Len(Range("B" & i).Value)).Font.Bold = True
            End If
        End If
    Next i
End If
End Sub


Viewing all articles
Browse latest Browse all 50068

Trending Articles