你用的什么版本?我这边没问题,window2000+office2000

解决方案 »

  1.   

    把这段代码拷到你机器上试试,是好的,但如果把注释去掉,就不行了,列宽不对了.我发觉是.MergeCells = True的问题Private Sub Command1_Click()
    riqi = 31
    Dim xlApp As Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlSheet As Excel.Worksheet
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Visible = True
    Set xlBook = xlApp.Workbooks.Add
    Set xlSheet = xlBook.Worksheets(1)
    xlSheet.Cells(3, 1) = "工号"
    xlSheet.Cells(3, 2) = "姓名"
    For i = 1 To riqi
    xlSheet.Cells(3, i + 2) = i
     Next i
     xlSheet.Cells(3, riqi + 3) = "夜"
     xlSheet.Cells(3, riqi + 4) = "中"
     xlSheet.Cells(3, riqi + 5) = "工"
     xlSheet.Cells(3, riqi + 6) = "病"
     xlSheet.Cells(3, riqi + 7) = "事"
    With xlApp
       '.Range("A1:AG2").Select
       ' With .Selection
       ' .HorizontalAlignment = xlCenter
       ' .MergeCells = True
       ' End With
       '    With .Selection.Font
         '   .Name = "华文行楷"
          '  .Size = 24
          '  .Strikethrough = False
          '  .Superscript = False
          '  .Subscript = False
          '  .OutlineFont = False
          ' .Shadow = False
          ' .Underline = xlUnderlineStyleNone
          ' .ColorIndex = xlAutomatic
       ' End With
       '缩小字体填充
        Range("A4:AO17").Select
        With Selection
        .ShrinkToFit = True
        End With
        Columns("A:A").Select
        Range("A3").Activate
        Selection.ColumnWidth = 3.5
        Columns("B:B").Select
        Range("B3").Activate
        Selection.ColumnWidth = 5.5
        Columns("C:AG").Select
        Range("C3").Activate
        Selection.ColumnWidth = 2
        ActiveWindow.LargeScroll ToRight:=1
        ActiveWindow.SmallScroll ToRight:=-5
        Columns("AH:AO").Select
        Selection.ColumnWidth = 3
        End With
         xlApp.Visible = True '显示表格
    Set xlApp = Nothing '交还控制给Excel
    End Sub
      

  2.   

    把这段代码拷到你机器上试试,是好的,但如果把注释去掉,就不行了,列宽不对了.我发觉是.MergeCells = True的问题Private Sub Command1_Click()
    riqi = 31
    Dim xlApp As Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlSheet As Excel.Worksheet
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Visible = True
    Set xlBook = xlApp.Workbooks.Add
    Set xlSheet = xlBook.Worksheets(1)
    xlSheet.Cells(3, 1) = "工号"
    xlSheet.Cells(3, 2) = "姓名"
    For i = 1 To riqi
    xlSheet.Cells(3, i + 2) = i
     Next i
     xlSheet.Cells(3, riqi + 3) = "夜"
     xlSheet.Cells(3, riqi + 4) = "中"
     xlSheet.Cells(3, riqi + 5) = "工"
     xlSheet.Cells(3, riqi + 6) = "病"
     xlSheet.Cells(3, riqi + 7) = "事"
    With xlApp
       '.Range("A1:AG2").Select
       ' With .Selection
       ' .HorizontalAlignment = xlCenter
       ' .MergeCells = True
       ' End With
       '    With .Selection.Font
         '   .Name = "华文行楷"
          '  .Size = 24
          '  .Strikethrough = False
          '  .Superscript = False
          '  .Subscript = False
          '  .OutlineFont = False
          ' .Shadow = False
          ' .Underline = xlUnderlineStyleNone
          ' .ColorIndex = xlAutomatic
       ' End With
       '缩小字体填充
        Range("A4:AO17").Select
        With Selection
        .ShrinkToFit = True
        End With
        Columns("A:A").Select
        Range("A3").Activate
        Selection.ColumnWidth = 3.5
        Columns("B:B").Select
        Range("B3").Activate
        Selection.ColumnWidth = 5.5
        Columns("C:AG").Select
        Range("C3").Activate
        Selection.ColumnWidth = 2
        ActiveWindow.LargeScroll ToRight:=1
        ActiveWindow.SmallScroll ToRight:=-5
        Columns("AH:AO").Select
        Selection.ColumnWidth = 3
        End With
         xlApp.Visible = True '显示表格
    Set xlApp = Nothing '交还控制给Excel
    End Sub