Private Sub Command6_Click()
    Dim xlApp As Excel.Application 'Defination
    Dim xlBook As Excel.Workbook
    Dim xlSheet As Excel.Worksheet    Set xlApp = CreateObject("Excel.Application")
    Set xlBook = xlApp.Workbooks.Open("D:\VB\ModelFile\ModelChart.xls") 'Read the TestInformation
    Set xlSheet = xlBook.Worksheets(1)
    
    'Application.AskToUpdateLinks = False 'Auto update setting
    
    f = 1
    
    For ii = 2 To 4084
    xlSheet.Cells(ii, 1) = (ii - 2) * f / 60
    Debug.Print ii
    Next ii
    For ii = 2 To 4084
    For jj = 2 To 3
    xlSheet.Cells(ii, jj) = ii * jj
    Next jj
    Debug.Print ii
    Next ii
    
    ActiveSheet.ChartObjects("ͼ±í 6").Activate
    With xlApp.ActiveChart
    .ChartArea.Select
    .ChartArea.Copy
    End With
    
    xlBook.Close (False)
    xlApp.Application.Quit
    Set xlApp = Nothing    Set xlApp = CreateObject("Excel.Application")
    Set xlBook = xlApp.Workbooks.Open("D:\VB\ModelProfil.xls") 'Read the TestInformation
    Set xlSheet = xlBook.Worksheets(1)
    
    xlSheet.Range("A28").Select
    xlSheet.Paste
  
    xlBook.SaveAs "D:\VB\ChartReport.xls" 'Save the result
    
    xlBook.Close (False)
    xlApp.Application.Quit
    Set xlApp = Nothing
    
End Sub中间的for循环是赋值部分,我用debug.print观察速度,发现太慢了,10000多格单元格要好几分钟了,几百份报告的话要出几个小时了估计,这样不行的,不知道有什么快速的办法没有?

解决方案 »

  1.   

    EXCEL是支持数组方式复制的啊.你可以选定一个范围,然后把这个数组复制到这个范围里来,那样就速度快很多了.objRange.Value = DataArray
      

  2.   

    恩,谢谢楼上,我试一下。说到选择一个范围,比如:。。Range(“B2:C4084”),如果其中的C4084是不固定的怎么办?我试着用一个变量去替换,但是没有办法替换,C4084有可能会是E5023,也有可能是别的,有没有办法用变量替换那?