以编程方式左对齐 Excel 文档中的数字

发布于 2024-12-06 16:50:24 字数 310 浏览 0 评论 0原文

如何在 VB 中以编程方式左对齐 Excel 中的列?我有一个 VB 程序,它根据某些信息编写 Excel 文件。我尝试过使用:

oSheet.Columns.HorizontalAlignment.Left()

这适用于带有文本的列。我有多个列,它们都是严格的数字。此函数不适用于具有数字单元格的列。

以下是我不断收到的 MissingMemberException:

Public member 'Left' on type 'Integer' not found.

How do I left justify my columns in Excel programmatically in VB? I have a VB program that is writing an excel file based off of certain information. I have tried using:

oSheet.Columns.HorizontalAlignment.Left()

This works against columns with text. I have multiple columns that are strictly numbers. This function will not work against columns with Numeric Cells.

Below is the MissingMemberException I keep getting:

Public member 'Left' on type 'Integer' not found.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

热鲨 2024-12-13 16:50:24

Range.Horizo​​ntalAlignment 是一个需要整数常量的属性。左边是xlLeft。 xlLeft 的计算结果为 -4131。 xlDistributed 为 -4117,xlRight 为 -4152,xlCenter 为 -4108,xlJustify 为 -4130。

Range.HorizontalAlignment is a property that requires an integer constant. Left is xlLeft. xlLeft evaluates to -4131. xlDistributed is -4117, xlRight is -4152, xlCenter is -4108, and xlJustify is -4130.

终难遇 2024-12-13 16:50:24

Horizo​​ntalAlignment 应该适用于文本和数字。不过,您应该指定对齐的值,因为它是一个属性。

HorizontalAlignment should work for both text and numbers. You should specify the value for the alignment though since it is a property.

梦巷 2024-12-13 16:50:24

虽然 @Banjoe 答案可行,但文档< /a> 为 Horizo​​ntalAlignment 属性指定以下有效常量:

xlHAlignCenter
xlHAlignCenterAcrossSelection
xlHAlignDistributed
xlHAlignFill
xlHAlignGeneral
xlHAlignJustify
xlHAlignLeft
xlHAlignRight

Although the @Banjoe answer will work, the documentation specifies the following valid constants for HorizontalAlignment property:

xlHAlignCenter
xlHAlignCenterAcrossSelection
xlHAlignDistributed
xlHAlignFill
xlHAlignGeneral
xlHAlignJustify
xlHAlignLeft
xlHAlignRight
归途 2024-12-13 16:50:24

我使用了以下内容,它对我来说非常有效:

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Add()
objExcel.Cells(1, 1).Value = "Some data"
objExcel.Cells(1, 2).Value = "More data"
Set objWorksheet = objWorkbook.Worksheets(1)
objExcel.Columns(2).Select
objExcel.Selection.EntireColumn.HorizontalAlignment = 2

I used the following, and it worked great for me:

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Add()
objExcel.Cells(1, 1).Value = "Some data"
objExcel.Cells(1, 2).Value = "More data"
Set objWorksheet = objWorkbook.Worksheets(1)
objExcel.Columns(2).Select
objExcel.Selection.EntireColumn.HorizontalAlignment = 2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文