简体   繁体   中英

excel macro, that runs when workbook opens, doesnt update cell with formula

Scenario : Need to create an excel that fetches data from multiple sources and concatenate text into a few cell so as to be used as part of report.

Goal : To have better formatting of the final output cell. More precisely, want to get few keywords of output cells to be made bold.

Method used : (simplified here for keeping it precise) Single sheet workbook.

Cell A1 = "Output from DataSource1 contains keyword1 "
Cell A2 = "Output from DataSource2 contains keyword2" 
Cell A3 = "=CONCATENATE(A1, A2)"
Cell A4 = "Output from DataSource1 contains keyword1 Output from DataSource2 contains 

keyword2" (The actual text that is populated in A3)

Press AltF11 and add the following code in 'ThisWorkbook'


Sub Workbook_Open()
Dim i, l As Integer
Dim Keywords(1 To 2) As String
Dim N As Long

Keywords(1) = "keyword1"
Keywords(2) = "keyword2"

For N = LBound(Keywords) To UBound(Keywords)
    i = InStr(1, Range("A3"), Keywords(N), vbTextCompare)
    l = Len(Keywords(N))
    If i > 0 Then
        With Range("A3").Characters(Start:=i, Length:=l).Font
            .FontStyle = "Bold"
        End With
    End If
Next N

For N = LBound(Keywords) To UBound(Keywords)
    i = InStr(1, Range("A4"), Keywords(N), vbTextCompare)
    l = Len(Keywords(N))
    If i > 0 Then
        With Range("A4").Characters(Start:=i, Length:=l).Font
            .FontStyle = "Bold"
        End With
    End If
Next N

End Sub

Observation : Keywords in Cell A4(simple text) get bold. Keywords in Cell A3 do not get bold.

Expected : Keywords in Cell A3 should get bold. That is the main requirement.

Question : (1) Is it possible to get few keywords of a cell be marked as bold when the cell content is coming from a formula (lets say a concatenation of two other cells)? (2) Is it possible to run such a code/macro when workbook opens?

看看这种解决方法(如何格式化串联的单元格):http: //youtu.be/Sxwyp-pUxBk

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM