![](/img/trans.png)
[英]Using Excel 2007+ VBA and a Cell's NumberFormat Property to Format Number Text
[英]Checking the text alignment of a cell in Excel using Word VBA 2007
我知道以下 Word VBA 2007 代碼
objExcel.ActiveWorkbook.Sheets("Book1").Cells(3, 4)
可以獲取名為“Book1”的已打開 Excel
工作簿
工作表的單元格 D3(第 3 行,第 4 列)內的文本。 但是如何獲得單元格的對齊方式(即左、右、居中或對齊)? 謝謝!
有兩個方面控制水平單元格對齊。 第一個是手動或通過代碼應用的強制非默認單元格對齊。 第二種是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
您確定這是您的工作表而不是您的工作簿,稱為Book1嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.