borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Word 2000 - UserForm behavior)Next Topic (report in access) New Topic New Poll Post Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : Aligning text in cells Solved Topic
Poster Message
iliekater
Level: Master

Registered: 04-02-2005
Posts: 123

icon Aligning text in cells

I wonder if there is any way to align the text inside a cell . Lets say we have inserted data in the cells  from A1 down to cell E4. How can one align the text inside those cells using VBA ?

03-09-2006 at 04:30 PM
View Profile Send Email to User Show All Posts | Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 891
icon Re: Aligning text in cells

Hi,
Use the macro record facility for most of your VBA questions.

Tools/Macros/Record new macro
give it a name
do what you want (ie. select cells, format cell alignment)
stop recording

Now, open the VBA editor and you should see the code in a module.

hope this helps
Kieron


____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)

14-09-2006 at 10:46 AM
View Profile Send Email to User Show All Posts | Quote Reply
iliekater
Level: Master

Registered: 04-02-2005
Posts: 123
icon Re: Aligning text in cells

Oh , sorry , my mistake . I meant how can one do that by using VB 6 (not VBA) ?

19-09-2006 at 08:23 PM
View Profile Send Email to User Show All Posts | Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 891
icon Re: Aligning text in cells

Hi,
I assume you are already using VB6 to pouplate data and create worksheets and stuff, so just record the macro, then paste it into your vb application. Just prefix "ActiveWorksheet" with your application variable.
example:

dim myApp as Excel.Application
dim myWS as Excel.Worksheet

set MyWb = MyApp.ActiveWorksheet

myWb.Range("A1").Format.Align=2

Hope this helps,
Kieron


____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)

20-09-2006 at 04:21 PM
View Profile Send Email to User Show All Posts | Quote Reply
iliekater
Level: Master

Registered: 04-02-2005
Posts: 123
icon Re: Aligning text in cells

There's something wrong . I am using :

ExcelWorksheet.Range("A1:X9").Format.Align = 2

but I get the message :
"Object does not support this method or property ."
So , I went over and found that you can not use :
ExcelWorksheet.Range("A1:A9").Format ,
becouse the the Format property is not reckognized .

20-09-2006 at 07:18 PM
View Profile Send Email to User Show All Posts | Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 891
icon Re: Aligning text in cells

Hi,
Did you record the macro in excel as I suggested?
If you had, you would have found the following:

    With Selection
        .HorizontalAlignment = xlRight
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With


which can be translated to:

Worksheets(1).Range("A1").HorizontalAlignment = xlRight


Regards
Kieron


____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)

21-09-2006 at 10:32 AM
View Profile Send Email to User Show All Posts | Quote Reply
iliekater
Level: Master

Registered: 04-02-2005
Posts: 123
icon Re: Aligning text in cells

Now , this is a correct code . This one really worked . I guess the first time you were using VB Net instead of VB6 . And , by the way , in order to align text to the center , I used :
ExcelWorksheet.Range("A1:X9").HorizontalAlignment = xlCenter

Thanks stickleprojects .

24-09-2006 at 04:26 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : Aligning text in cells Solved Topic
Previous Topic (Word 2000 - UserForm behavior)Next Topic (report in access) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder