2015-09-27 73 views
0

在Excel单元格中的文本对齐我知道下面的Word VBA 2007码检查使用Word VBA 2007

objExcel.ActiveWorkbook.Sheets("Book1").Cells(3, 4) 

可以得到一个打开的D3单元格(第3行,第4列)中的文本Excel 工作簿 工作表名为“Book1”。但是如何获得细胞的排列方式(即左,右,中心或正确)?谢谢!

回答

0

控制水平单元对齐有两个方面。首先是手动或通过代码应用的强制非默认单元格对齐方式。第二种是xlGeneral格式,根据数据的性质可以是右对齐,左对齐或中心对齐。

With objExcel.ActiveWorkbook.Sheets("Book1") 
    Select Case .Cells(3, 4).HorizontalAlignment 
     Case xlRight  ' equal to -4152 
      Debug.Print "The cell is right-aligned." 
     Case xlCenter  ' equal to -4108 
      Debug.Print "The cell is center-aligned." 
     Case xlLeft   ' equal to -4131 
      Debug.Print "The cell is left-aligned." 
     Case xlGeneral  ' equal to 1 
      Select Case Application.Evaluate("TYPE(" & .Cells(3, 4).Address(0, 0, external:=True) & ")") 
       Case 1 
        Debug.Print "The cell is right-aligned." 
       Case 2 
        Debug.Print "The cell is left-aligned." 
       Case 4, 16 
        Debug.Print "The cell is center-aligned." 
       Case Else 
        Debug.Print "The cell is part of an array." 
      End Select 
    End Select 
End With 

你确定这是你的工作,而不是你的工作簿被称为第一册

+0

非常感谢!我在问题中更正了“工作簿”一词。 – GreenPenguin