Programmatically Left Justify a number in an Excel document
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开发者_StackOverflow keep getting:
Public member 'Left' on type 'Integer' not found.
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.
HorizontalAlignment should work for both text and numbers. You should specify the value for the alignment though since it is a property.
Although the @Banjoe answer will work, the documentation specifies the following valid constants for HorizontalAlignment property:
xlHAlignCenter
xlHAlignCenterAcrossSelection
xlHAlignDistributed
xlHAlignFill
xlHAlignGeneral
xlHAlignJustify
xlHAlignLeft
xlHAlignRight
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
精彩评论