我已经面临一个问题好几天了,希望有人可以帮助我。 基本上,我在新的 Excel 工作表中创建计算报告,但当我显示值时,有时会使用小数分隔符,有时则不会,即使这两种情况的代码完全相同。 我附上了一些图片来突出问题:
问题的核心(可能)是,一旦单元格中显示值,单元格格式就会从“常规”更改为“数字”。我找不到阻止 Excel 执行此操作的方法,也不明白为什么这对于上面的行来说不是问题。
你有什么想法吗?预先感谢您!
我尝试更改整个列的格式类型,并在单元格中显示时将值转换为字符串,但它似乎不起作用。
Public Sub create_report_H(uf As UserForm)
' Adding new sheet to create report on it:
Workbooks("ASME VIII Div.1 Formed head - Macro").Activate
Call Report.add_ws_after_others
' Set worksheet:
Set ws_rep = Worksheets(ActiveSheet.Name)
ws_rep.Protect UserInterfaceOnly:=True
' A4 dimensions:
ws_rep.PageSetup.PaperSize = xlPaperA4
ActiveWindow.View = xlPageLayoutView
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.LeftHeader = "&G"
.LeftHeaderPicture.Height = 30
.RightHeader = "&D &I - &I &T" '"&D &B&ITime:&I&B&T"
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.7)
.RightMargin = Application.InchesToPoints(0.7)
.TopMargin = Application.InchesToPoints(0.75)
.BottomMargin = Application.InchesToPoints(0.75)
.HeaderMargin = Application.InchesToPoints(0.3)
.FooterMargin = Application.InchesToPoints(0.3)
.Zoom = 100
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = True
.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.CenterFooter = "&P"
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
End With
Application.PrintCommunication = True
' Page setup:
Dim last_col As String
Dim first_row_height As Double
Dim row_height As Double
last_col = "I"
row_height = 15
ws_rep.Range("H:H").NumberFormat = "General"
' Material:
ws_rep.Cells(9, "A").Value = "Material"
ws_rep.Cells(9, "A").Font.Size = 14
' Label:
ws_rep.Cells(10, "A").Value = "Product from:"
ws_rep.Cells(11, "A").Value = "ASTM specification:"
ws_rep.Cells(12, "A").Value = "Grade:"
ws_rep.Cells(13, "A").Value = "Max. allowable stress (TDesign):"
ws_rep.Cells(13, "A").Characters(25, 6).Font.Subscript = True
ws_rep.Cells(14, "A").Value = "Max. allowable stress (TRoom):"
ws_rep.Cells(14, "A").Characters(25, 4).Font.Subscript = True
ws_rep.Cells(15, "A").Value = "Corrosion allowance:"
' Values:
ws_rep.Cells(10, "H").Value = "Plate"
ws_rep.Cells(11, "H").Value = uf.CB_spec.Value
ws_rep.Cells(12, "H").Value = uf.CB_grade.Value
ws_rep.Cells(13, "H").Value = CStr(uf.TB_MAWS_DT.Value)
ws_rep.Cells(14, "H").Value = CStr(uf.TB_MAWS_RT.Value)
ws_rep.Cells(15, "H").Value = uf.TB_corrosion.Value
ws_rep.Range(ws_rep.Cells(10, "H"), ws_rep.Cells(15, "H")).HorizontalAlignment = xlRight
' Units:
ws_rep.Cells(13, "I").Value = " " & uf.Label_stress_unit_DT.Caption
ws_rep.Cells(14, "I").Value = " " & uf.Label_stress_unit_DT.Caption
ws_rep.Cells(15, "I").Value = " " & Geometry.Label_E_D.Caption
ws_rep.Range(Cells(9, "A"), Cells(9, last_col)).Interior.Color = RGB(255, 255, 0)
ws_rep.Range(Cells(10, "A"), Cells(15, last_col)).Interior.Color = RGB(230, 230, 230)
我认为此问题可能是由于您的 Excel 设置的语言和区域设置所致。VBA 会根据美国区域设置配置解释公式和值,而不管系统中当前的区域设置配置如何。对于一些对值有不同格式规则的国家来说,这往往是一个问题,就我而言:
这些是使用 VBA 编程时需要考虑的一些实例。
在您的情况下,您的 Excel 似乎被设置为将一个数字后跟一个逗号和 3 个或更多数字解释为千位分隔符。为了解决这个问题,我建议将小数点和千位分隔符更改为所需的字符。
Microsoft 支持中提供了相关说明更改用于分隔千位或小数的字符
或者,如果您希望 EXCEL 不将字符串转换为数字,而是将其保留为字符串,请将代码行更改为以下内容:
ws_rep.Cells(13, "H").Value = "'" & CStr(uf.TB_MAWS_DT.Value)
希望有帮助!