---1--- Example This example sets the formula, column width, and number format for cell B5 on Sheet1 in Spreadsheet1.Sub SetFormula() Dim rngRandomNum
' Set a variable to cell B5 on Sheet1. Set rngRandomNum = Spreadsheet1.Worksheets("Sheet1").Range("B5")
' Insert a formula into cell B5. rngRandomNum.Formula = "=5*RAND()"
' Set the number format for cell B5. rngRandomNum.NumberFormat = "#.###"
' Autofit column B. rngRandomNum.Columns.AutoFitEnd Sub ---2--- Example These examples set the number format for cell A17, row one, and column C (respectively) on Sheet1.Worksheets("Sheet1").Range("A17").NumberFormat = "General" Worksheets("Sheet1").Rows(1).NumberFormat = "hh:mm:ss" Worksheets("Sheet1").Columns("C").NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"
Columns("A:B").Select With Selection.Interior .ColorIndex = 15 .Pattern = xlSolid End With Selection.NumberFormatLocal = "0.00_ " Range("D2").Select
Excelworksheet.Range("A1:M1").ColorIndex=16
Excelworksheet.Range("A1:M1").NumberFormat=""#00.00"
Excell的VBA编程很多都可以用录制宏的方式得到代码,不一定要来求助的。当然用录制宏的方式得到的代码有很多是垃圾代码,我们还要做一定的剪辑。
Example
This example sets the formula, column width, and number format for cell B5 on Sheet1 in Spreadsheet1.Sub SetFormula()
Dim rngRandomNum
' Set a variable to cell B5 on Sheet1.
Set rngRandomNum = Spreadsheet1.Worksheets("Sheet1").Range("B5")
' Insert a formula into cell B5.
rngRandomNum.Formula = "=5*RAND()"
' Set the number format for cell B5.
rngRandomNum.NumberFormat = "#.###"
' Autofit column B.
rngRandomNum.Columns.AutoFitEnd Sub
---2---
Example
These examples set the number format for cell A17, row one, and column C (respectively) on Sheet1.Worksheets("Sheet1").Range("A17").NumberFormat = "General"
Worksheets("Sheet1").Rows(1).NumberFormat = "hh:mm:ss"
Worksheets("Sheet1").Columns("C").NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"
With Selection.Interior
.ColorIndex = 15
.Pattern = xlSolid
End With
Selection.NumberFormatLocal = "0.00_ "
Range("D2").Select
还是一样啊
虽然excel的单元格式变了,但是里面的数的格式还是不变化啊
但是剪切出来再复制进去就变了。
我最后只好用formatnumber函数解决了
下午结贴