【问题标题】:Visual Basic - How to Center a Column in Excel and other Formatting OptionsVisual Basic - 如何在 Excel 和其他格式选项中使列居中
【发布时间】:2020-07-08 18:43:26
【问题描述】:

如何使用 Visual Basic (Visual Studio 2015) 在 Excel 中将列居中?我正在使用 Microsoft.Office.Interop.Excel

这是我用来执行此操作但无法执行的代码段:

Excel3 = CreateObject(“Excel.Application”) 
Excel3.WorkBooks.Add() 

Sheet3 = Excel3.ActiveSheet

Sheet3.Columns("A:A").Select
    With Sheet3.Selection
        .HorizontalAlignment = Sheet3.Columns("A:A").xlCenter
        .VerticalAlignment = Sheet3.Columns("A:A").xlCenter
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = Sheet3.Columns("A:A").xlContext
        .MergeCells = False
    End With

我正在使用 Visual Basic (Visual Studio 2015)。错误消息指出:

“未处理的异常:System.MissingMemberException:未找到类型“工作表”上的公共成员“选择” 在 Microsoft.VisualBasic.CompilerService.LateBindign.LateGet(对象 o,类型 objType,字符串名称,对象 [] 参数,字符串 [] 参数,布尔 [] CopyBack) 在 Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(对象实例,类型类型,字符串成员名称,对象 [] 参数,字符串 [] 参数名称,类型 [] 类型参数,布尔 [] CopyBack)

另一个变体给了我错误:找不到类型范围的选择。

Sheet3.Columns("A").Select
    With Sheet3.Columns.Selection
        .HorizontalAlignment = Sheet3.Columns("A").xlCenter
        .VerticalAlignment = Sheet3.Columns("A").xlCenter
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = Sheet3.Columns("A").xlContext
        .MergeCells = False
    End With

【问题讨论】:

    标签: excel vb.net visual-studio-2015 excel-interop


    【解决方案1】:

    我在阅读a post from this link 后找到了解决方案。这是我采取的步骤。

    更改自: 导入 Microsoft.Office.Interop.Excel

    到: 导入 Excel = Microsoft.Office.Interop.Excel

    以上更改允许我访问具有我可以使用的 xlCenter 的常量对象。

    然后相关代码改为:

    Sheet3.Columns("A:S").EntireColumn.AutoFit
    Sheet3.Range("A:S").VerticalAlignment = Excel.Constants.xlCenter
    Sheet3.Range("A:S").WrapText = True
    Sheet3.Range("A:A").HorizontalAlignment = Excel.Constants.xlCenter
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 2015-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多