0

I have the following function in vba:

Function ConcatinateAllCellValuesInRange(sourceRange As Excel.Range) As String
        Dim finalValue As String

        Dim cell As Excel.Range

        i = 0       
        For Each cell In sourceRange.Cells
        i = i + 1
        Rzad = cell.Row
            finalValue = finalValue & CStr(i) & ". " & CStr(Sheets(1).Cells(Rząd, 6)) & "/" & CStr(Sheets(1).Cells(Rząd, 7)) & ": " & Format(cell.Value, Bold) & "; " & vbCrLf

        Next cell

        ConcatinateAllCellValuesInRange = finalValue
    End Function

I want to make a part of the text bold but 'Format(cell.Value, Bold)' does not work. Can you suggest a solution?

Grzegorz
  • 25
  • 2
  • 7
  • not an answer, but can give you a hint http://stackoverflow.com/questions/21514704/how-to-extract-text-based-on-font-color-from-a-cell-with-text-of-multiple-colors/21514802#21514802 – Dmitry Pavliv Sep 09 '16 at 11:47
  • 1
    See [here](http://stackoverflow.com/questions/10295380/excel-vba-make-part-of-string-bold) – Clusks Sep 09 '16 at 12:08
  • Is the target cell (i.e. the range where you want to store the output in) known in advance? Is it a fixed cell on the worksheet or always in the same column relative to the data you are processing? – silentsurfer Sep 09 '16 at 13:32

1 Answers1

1

try:

cell.Font.Bold = True

if you want the whole cell to be bold.

If you want to make only part of the cell content bold, see: excel vba: make part of string bold

Community
  • 1
  • 1
silentsurfer
  • 1,998
  • 2
  • 17
  • 29
  • Neither 'cell.Font.Bold = True' nor 'cell.Characters.Font.Bold = True' does not work, because cell is not an object. – Grzegorz Sep 09 '16 at 11:53
  • One cell, which is filled by calling the above function looks like this: – Grzegorz Sep 09 '16 at 12:16
  • It is an object... please update your code indicating where you would like to make the cell bold, and the error you are receiving. – silentsurfer Sep 09 '16 at 12:18
  • 1
    the code of silentsurfer should work. Your `cell` is indeed an **object** and it is even a **range** object, thus it has a `.font.bold` property. It's starting to look like an XY problem. – Thomas G Sep 09 '16 at 12:19
  • Your function returns a string, which does not store any formatting information (just plain text). You need to break up your process into two steps, one for concatenating the text value and one for writing the result into a cell and doing the formatting. – silentsurfer Sep 09 '16 at 12:21
  • One cell, which is filled by calling the above function should look like this: 1. PGM ABC7/PGM 20P2: nie rozlicza się; 2. PGM 1Y77/PGM 40E2: nie rozlicza się; 3. PGM 6B63/PGM 4AH8: Lech wpisał testowo do starej przepustki; 4. PGM 6B65/PGM XX22: nie rozlicza się; And after each calling one cell is filled up. That's why I want to make a part of a string bold in the function called and not in the main body of my macro. – Grzegorz Sep 09 '16 at 12:23
  • 'cell.Font.Bold' - Compile error: invalid use of property – Grzegorz Sep 09 '16 at 12:27
  • {Beginning = InStr(finalValue, ": ") Ending = InStr(finalValue, "; ") finalValue.Characters(Początek, Koniec).Font.Bold} after the finalValue =…. part does not work either. – Grzegorz Sep 09 '16 at 12:49
  • 1
    again... `finalValue` is not a range object, `cell` is. You can only change the `Bold` property of a range object. I advise you use a `Sub` instead of a function, that: 1) Concatenates the string 2) Determines the part of the string that needs to be bold 3)Writes the concatenated string to a target cell 4)Formats the target cell to make it bold at the right place. – silentsurfer Sep 09 '16 at 13:17