Results 1 to 3 of 3

Thread: Whats wrong with this code Excel not printing

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    90

    Whats wrong with this code Excel not printing

    I am using the following code but I'm generating a blank Excel worksheet


    Code:
        Dim objExcel As Excel.Application
        Dim bkWorkbook As Excel.Workbook
        Dim shWorkSheet As Excel.Worksheet
        Set objExcel = New Excel.Application
        Set bkWorkbook = objExcel.Workbooks.Add
        Set shWorkSheet = bkWorkbook.ActiveSheet
    With shWorkSheet
        .Rows("1:2").Select
        .Rows.Font.Bold = True
        .Rows.Font.Bold = True
        .Rows.Borders(xlDiagonalDown).LineStyle = xlNone
        .Rows.Borders(xlDiagonalUp).LineStyle = xlNone
        
        With .Rows.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        With .Rows.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        With .Rows.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        With .Rows.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        With .Rows.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        With .Rows.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        Range("F1:H1").Select
        With .Rows
            .HorizontalAlignment = xlGeneral
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
        End With
        Range("I1:K1").Select
        With .Rows
            .HorizontalAlignment = xlGeneral
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
        End With
        Range("L1:N1").Select
        With .Rows
            .HorizontalAlignment = xlGeneral
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
        End With
        Rows("1:1").Select
        With .Rows.Font
            .Name = "Arial"
            .Size = 14
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ColorIndex = xlAutomatic
        End With
        Range("A1").Select
        ActiveCell.value = "Entity ID"
        Columns("A:A").Select
        Columns("A:A").EntireColumn.AutoFit
        Range("B1").Select
        ActiveCell.value = "Entity Name"
        Range("C1").Select
        ActiveCell.value = "Rtn Code"
        Range("D1").Select
        ActiveCell.value = "Rtn Level"
        Range("E1").Select
        ActiveCell.value = "Rtn Description"
        Range("F1:H1").Select
        ActiveCell.value = "Distribution of Revenue"
        Range("F1:H1").Select
        ActiveCell.value = "Distribution of Revenue (Before)"
        Range("I1:K1").Select
        ActiveCell.value = "Distribution of Revenue (After)"
        Range("L1:N1").Select
        ActiveCell.value = "Distribution of Revenue (Variance)"
        Range("F2").Select
        ActiveCell.value = "State"
        Range("G2").Select
        ActiveCell.value = "County"
        Range("H2").Select
        ActiveCell.value = "City"
        Range("I2").Select
        ActiveCell.value = "State"
        Range("J2").Select
        ActiveCell.value = "County"
        Range("K2").Select
        ActiveCell.value = "City"
        Range("L2").Select
        ActiveCell.value = "State"
        Range("M2").Select
        ActiveCell.value = "County"
        Range("N2").Select
        ActiveCell.value = "City"
        Rows("1:2").Select
        
    End With

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Whats wrong with this code Excel not printing

    in the section at the bottom (where u are adding data) you are not referencing excel or the workbook.

    ActiveCell.value = "City"
    Rows("1:2").Select

    should be

    xls.ActiveCell.value = "City"
    xls.Rows("1:2").Select
    or
    shWorkSheet.ActiveCell.value = "City"
    shWorkSheet.Rows("1:2").Select
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

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

    Re: Whats wrong with this code Excel not printing

    ... or better still, avoid using Active* and Selection completely (which are unsafe).

    Change code like this:
    Code:
        Range("N2").Select
        ActiveCell.value = "City"
    to this:
    Code:
        .Range("N2").value = "City"
    (as this is inside a With block, that is effectively: shWorkSheet.Range("N2").value ...)


    Note that further up in the code you also have lines like: Range("F1:H1").Select
    ..these all need (at the least) a . added to the start of them.

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