简体   繁体   中英

Excel VBA Formatting of Text

I have code that is printing text into a given set of cells as shown below:

'SAMPLE
Sheets("Sheet1").Range("A11") = "B. TEST SAMPLES"
Sheets("Sheet1").Range("B12") = "SS"
Sheets("Sheet1").Range("C12") = "PART NO."
Sheets("Sheet1").Range("C12:H12").Merge
Sheets("Sheet1").Range("C12:H12").HorizontalAlignment = xlCenter
Sheets("Sheet1").Range("I12") = "VALVE CODE"
Sheets("Sheet1").Range("I12:M12").Merge
Sheets("Sheet1").Range("I12:M12").HorizontalAlignment = xlCenter
Sheets("Sheet1").Range("N12") = "TYPE"
Sheets("Sheet1").Range("N12:P12").Merge
Sheets("Sheet1").Range("N12:P12").HorizontalAlignment = xlCenter
Sheets("Sheet1").Range("Q12") = "TA/SEALED"
Sheets("Sheet1").Range("Q12:T12").Merge
Sheets("Sheet1").Range("Q12:T12").HorizontalAlignment = xlCenter
Sheets("Sheet1").Range("U12") = "Qt"
Sheets("Sheet1").Range("V12") = "Spring"
Sheets("Sheet1").Range("V12:X12").Merge
Sheets("Sheet1").Range("V12:X12").HorizontalAlignment = xlCenter
Sheets("Sheet1").Range("Y12") = "DESCRIPTION"

It does this perfectly, now I want to bold the top cells. I write the following code to achieve this:

'FORMATTING
Range("B12").Font.Bold = True
Range("C12").Font.Bold = True
Range("I12").Font.Bold = True
Range("N12").Font.Bold = True
Range("Q12").Font.Bold = True
Range("U12").Font.Bold = True
Range("V12").Font.Bold = True
Range("Y12").Font.Bold = True

I do not see anything wrong with this code, the syntax seems fine but the formatting is not changed. ie. The cells I want bolded remain unbolded.

Could anyone provide some insight on why this is happening and how it can possibly be fixed?

确保在Range().Font.Bold = True之前有Sheets("Sheet1") Range().Font.Bold = True

Here is the working code:

Sheets("Sheet1").Select

Range("B12","C12","I12","N12","Q12","U12","V12","Y12").Font.Bold = True  

You can select all your cells in one range.

Is there are reason you didn't use sheets("")

'FORMATTING
Sheets("Sheet1").Range("B12").Font.Bold = True
Sheets("Sheet1").Range("C12").Font.Bold = True
Sheets("Sheet1").Range("I12").Font.Bold = True
Sheets("Sheet1").Range("N12").Font.Bold = True

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