Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Centering Text

Status
Not open for further replies.

mc13

Programmer
May 26, 2000
26
US
I am trying to center the text in the cells of an Excel spreadsheet through vba coding in Access. Does anyone have<br>any ideas for the properties that can be used for this.<br>Thanks
 
The best way to get sample code is to record a macro, do what you want the program to do, then modify the code to suit your needs.<br><br>Action:<br>RECORD - select &quot;A2&quot; then hit the ctr text button - STOP<br>Result:<br>&nbsp;&nbsp;&nbsp;&nbsp;Range(&quot;A2&quot;).Select<br>&nbsp;&nbsp;&nbsp;&nbsp;With Selection<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.HorizontalAlignment = xlCenter<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.VerticalAlignment = xlBottom<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.WrapText = False<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Orientation = 0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ShrinkToFit = False<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.MergeCells = False<br>&nbsp;&nbsp;&nbsp;&nbsp;End With<br><br>Now you can modify this to just change the alignment:<br>Range(&quot;A2&quot;).HorizontalAlignment = xlCenter<br><br>Hope this helps!<br><br>
 
Missed the VBA from Access part. You will need to get/create the Excel object then use vba as if you were in Excel. Include MS Excel library in References.<br><br>'Attach to the active Excel object<br>Set xl = GetObject(, &quot;Excel.Application&quot;)&nbsp;&nbsp;<br>If xl Is Nothing Then<br>&nbsp;&nbsp;&nbsp;&nbsp;MsgBox &quot;Could Not Attach to Active Excel Object&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;'Do Stuff<br>End If<br><br>'Get handle to the active sheet in ExcelSet <br>xlsh = xl.ActiveSheet&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>xlsh.Range(&quot;A2&quot;).HorizontalAlighment = xlCenter<br>
 
Thanks for all of the help.<br>This worked real well to cover all<br>of the cells needed.<br><br>For j = 1 To 250<br>&nbsp;&nbsp;&nbsp;&nbsp;xlSheet.Range(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top