JustAJunior
JustAJunior

Reputation: 23

Printing a String in italics

I am taking input as strings from a Userform and I need to output them in a cell. In the output, all the strings are concatenated but only a few them italicized. I am unable to output the strings italicized. I have tried searching the documentation, stack exchange and few other blogs but all of them require selection from a cell rather than manipulating the string obtained from Userform. Any help or pointers are greatly appreciated.

Output now: [1] R. Welder. How to weld. Welding Shop: Publisher, 2014, pp. 25-32.

Desired Output:[1] R. Welder. How to weld. Welding Shop: Publisher, 2014, pp. 25-32.

Private Sub Ok_Click()
Dim emptyRow As Long
Dim bookAuthor, bookTitle, loc, publish, yearBook, pageBook As String

'Make Sheet2 active
 Sheet2.Activate

'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

'Format Information
bookAuthor = Author.Value + ". "
bookTitle = TitleOfBook.Value + ". " 'Needs to be Italicized
'bookTitle.Font.Italic 'Error: Object Required
'TitleOfBook.Font.Italic = True 'Italicizes in userform but not in cell
loc = Location.Value + ": "
publish = Publisher.Value + ", "
yearBook = Year.Value + ", "
pageBook = "pp. " + Pages.Value + ". "

'Transfer information
Cells(emptyRow, 1).Value = "[" + CStr(emptyRow) + "] " + bookAuthor + bookTitle + loc + publish + yearBook + pageBook

Unload Me
End Sub'

Upvotes: 0

Views: 2906

Answers (1)

Alex K.
Alex K.

Reputation: 175766

Set the font style of the cell your writing into as its the cell that's italic, not the string;

With Range("A1")
    .Font.Italic = True
    .Value = "Roast Beef"
End With

Edit; To italicize part of the cell select its content by offset and length:

emptyRow = 1
bookAuthor = "R. Welder."
bookTitle = "How to weld"
loc = "Welding Shop"
publish = "Publisher"
yearBook = "2014"
pageBook = "pp25-32"

Dim temp As String, begin As Long
'// store everything upto the start of italic part
temp = "[" & emptyRow & "] " & bookAuthor & " "
'// store its length
begin = Len(temp)

With Range("A1")
    '// set the value to everything
    .Value = temp & bookTitle & " " & loc & " " & publish & " " & yearBook & " " & pageBook
    '// we know where the italic text need to be
    .Characters(begin + 1, Len(bookTitle)).Font.Italic = True
End With

Upvotes: 3

Related Questions