Results 1 to 5 of 5

Thread: VBA code in Excel..how do I do this?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    VBA code in Excel..how do I do this?

    How do I do this in right way? No it doesent work...
    VB Code:
    1. Dim xlApp As Excel.Application
    2. Dim xlBook As Object
    3. Dim xlSheet As Object
    4.  
    5.     Set xlApp = CreateObject("Excel.Application")
    6.     Set xlBook = xlApp.Workbooks.Add
    7.     Set xlSheet = xlBook.Worksheets(1)
    8.    
    9. xlApp.ScreenUpdating = False
    10.    
    11.     With xlSheet
    12.         .PageSetup.Orientation = xlLandscape
    13.         .PageSetup.LeftMargin = xlApp.Application.InchesToPoints(0.196850393700787)
    14.         .PageSetup.RightMargin = xlApp.Application.InchesToPoints(0.196850393700787)
    15.         .PageSetup.TopMargin = xlApp.Application.InchesToPoints(0.78740157480315)
    16.         .PageSetup.BottomMargin = xlApp.Application.InchesToPoints(0.78740157480315)
    17.         .Range("A1:P1").Select
    18.         .Selection.MergeCells = True
    19.         .Rows("1:1").RowHeight = 36.75
    20.         .ActiveCell.FormulaR1C1 = "TestHeader"
    21.         .Selection.Font.Size = 14
    22.         .Selection.Font.Bold = True
    23.         .Selection.HorizontalAlignment = xlCenter
    24.         .Selection.VerticalAlignment = xlCenter
    25.         .Range("A2").Select
    26.         .Application.Visible = True
    27.  End With
    28.  
    29.  
    30.     Set xlSheet = Nothing
    31.     Set xlBook = Nothing
    32.     Set xlApp = Nothing

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VBA code in Excel..how do I do this?

    It looks ok to me, except this section:
    VB Code:
    1. .Range("A1:P1").Select
    2.         .Selection.MergeCells = True
    3.         .Rows("1:1").RowHeight = 36.75
    4.         .ActiveCell.FormulaR1C1 = "TestHeader"
    5.         .Selection.Font.Size = 14
    6.         .Selection.Font.Bold = True
    7.         .Selection.HorizontalAlignment = xlCenter
    8.         .Selection.VerticalAlignment = xlCenter
    ..which should ideally be as below (to eliminate Selection objects, which can change if the user is working with Excel too):
    VB Code:
    1. .Range("A1:P1").MergeCells = True
    2.         .Rows("1:1").RowHeight = 36.75
    3.         With .Range("A1:P1")
    4.           .FormulaR1C1 = "TestHeader"   'should this be Value instead of FormulaR1C1?
    5.           .Font.Size = 14
    6.           .Font.Bold = True
    7.           .HorizontalAlignment = xlCenter
    8.           .VerticalAlignment = xlCenter
    9.         End With

    Anyway, what is the problem you are having? "No it doesent work..." tells us virtually nothing.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Re: VBA code in Excel..how do I do this?

    Thanks si_the_geek!

    Yes, this should this be Value and not FormulaR1C1.

    VB Code:
    1. .FormulaR1C1 = "TestHeader"

  4. #4
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: VBA code in Excel..how do I do this?

    You wrote:
    Code:
    Dim xlApp As Excel.Application
    Dim xlBook As Object
    Dim xlSheet As Object
    
        Set xlApp = CreateObject("Excel.Application")
        Set xlBook = xlApp.Workbooks.Add
        Set xlSheet = xlBook.Worksheets(1)
    I would have set Option Explicit, and done the following:
    Code:
    Dim xlApp As Excel.Application
    Dim xlBook As Workbook
    Dim xlSheet As Worksheet
    
        Set xlApp = CreateObject("Excel.Application")
        Set xlBook = xlApp.Workbooks.Add
        Set xlSheet = xlBook.Sheets(1)
    I don't know if this makes any difference or not ...
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VBA code in Excel..how do I do this?

    There is a reason to define them as Object, it means that you can work with multiple versions of Excel (ie: your users do not need the same version as you). I presume that Pirre001 intends to also change the Excel.Application to Object later too.

    Oh, and instead of "as Workbook" you should have used "as Excel.Workbook", else any other classes called Workbook could be used. (same for "as Worksheet")

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width