With xlSheet.Application
.Range("C4:F6").Merge '合并单元格
.Range("C4") = "我"
 .Range("C4").Font.Bold = True '设字体为粗体
    End With
 
xlSheet.Cells.HorizontalAlignment = xlCenter   '设置上下居中
       
        xlSheet.Cells.VerticalAlignment = xlCenter
         xlSheet.Cells.NumberFormatLocal = "@"
以上代码End With以下的不起作用,请教老师,怎么样改才能达到整个工作表xlSheet数据上下左右居中的结果?

解决方案 »

  1.   

    把后面那几句改成这样试试:xlSheet.Cells.Select
    xlSheet.Cells.HorizontalAlignment = xlCenter 
    xlSheet.Cells.VerticalAlignment = xlCenter 
    xlSheet.Cells.NumberFormatLocal = "@" 
     
      

  2.   

    '整个工作表数据上下左右居中  Public ex   As Excel.Application
      Public wb   As Excel.Workbook
      Public sh   As Excel.Worksheet
    Private Sub Command3_Click()
        Set ex = CreateObject("Excel.Application")
        Set wb = ex.Workbooks.Open("C:\A.XLS") '你的文件
        Set sh = wb.Sheets(1)        sh.Cells.HorizontalAlignment = xlCenter
            sh.Cells.VerticalAlignment = xlBottom
            sh.Cells.WrapText = False
            sh.Cells.Orientation = 0
            sh.Cells.AddIndent = False
            sh.Cells.IndentLevel = 0
            sh.Cells.ShrinkToFit = False
            sh.Cells.ReadingOrder = xlContext
            sh.Cells.MergeCells = False     ex.Visible = True
        'wb.Close SaveChanges:=True    '直接关闭不保存
        'ex.Quit
        Set ex = Nothing
        Set wb = Nothing
        Set sh = Nothing
    End Sub
      

  3.   

    Public xlApp   As Excel.Application
      Public xlBook  As Excel.Workbook
      Public xlSheet   As Excel.Worksheet
    Private Sub Command1_Click()
        Set xlApp = CreateObject("Excel.Application")
        Set xlBook = xlApp.Workbooks.Open(App.Path & "\A.xls") '你的文件
        Set xlSheet = xlBook.Sheets(1)
            xlSheet.Cells.HorizontalAlignment = xlCenter
            xlSheet.Cells.VerticalAlignment = xlBottom
           xlSheet.Cells.WrapText = False
           xlSheet.Cells.Orientation = 0
          xlSheet.Cells.AddIndent = False
          xlSheet.Cells.IndentLevel = 0
           xlSheet.Cells.ShrinkToFit = False
         xlSheet.Cells.ReadingOrder = xlContext
           xlSheet.Cells.MergeCells = False
        xlApp.Visible = True
        'wb.Close SaveChanges:=True    '直接关闭不保存
        'ex.Quit
        Set xlApp = Nothing
        Set xlBook = Nothing
        Set sh = Nothing
    End Sub
    结果:开不了文件
      

  4.   

    你看一下进程里是否有多个EXCEL进程没有关
      

  5.   

    EXCEL进程,原来是行的,现在只是换了一部电脑就不行了,是否VB6里还需要什么设置?
      

  6.   

    进程里没有EXCEL进程,运行结果是如下提示:
    实时错误'-2147417851(80010105)' 
    自动化错误 
      

  7.   

    点调试:
     Set xlBook = xlApp.Workbooks.Open(App.Path & "\A.xls") '你的文件
    变色
      

  8.   

    你在目录下有A.XLS这个文件吗?
      

  9.   

    不会犯这么低级的错误的.
    其实我原来程序的前部分也是:
    Public xlApp  As Excel.Application 
      Public xlBook  As Excel.Workbook 
      Public xlSheet  As Excel.Worksheet 
    Private Sub Command1_Click() 
        Set xlApp = CreateObject("Excel.Application") 
        Set xlBook = xlApp.Workbooks.Open(App.Path & "\A.xls") '你的文件 
        Set xlSheet = xlBook.Sheets(1) 
    运行并没有什么出错问题,只是后设置单元格部分语句没起作用,现在连设置单元格的语句运行不到就出错了.真是见鬼了!
      

  10.   

    那就是你路径没写好...要么就是你A.CXL文件放错了
      

  11.   

    Private Sub Command1_Click()
        Set ex = CreateObject("Excel.Application")
        Set wb = ex.Workbooks.Open(App.Path & "\A.xls") '你的文件
        Set sh = wb.Sheets(1)   '第一个工作表
               sh.Rows("4:4").Select
           ex.Selection.Insert Shift:=xlDown
           Set sh = wb.Sheets(2)  '第二个工作表
            wb.Sheets("Sheet2").Select
           sh.Columns("B:B").Select
            ex.Selection.Insert Shift:=xlToRight
         ex.Visible = True
        'wb.Close SaveChanges:=True    '直接关闭不保存
        'ex.Quit
        Set ex = Nothing
        Set wb = Nothing
        Set sh = Nothing
    End Sub
    测试没有问题   应该是你的文件没有放在程序目录下