Hanyu_Li 2017-11-23 09:39 采纳率: 0%
浏览 1087

求VB大神协助!将VB界面的MSFlexGrid中的数据导出到Excel指定的单元格

执行结果都是数据导出失败,有没有大神帮忙看看应该怎么改动呢?
Public Function ExporttoExcel(flex As MSFlexGrid)
On Error GoTo ErrHandler

Dim xlApp As Object
Dim xlBook As Object
Dim Rows As Integer, Cols As Integer
Dim iRow As Integer, hCols As Integer, iCol As Integer
Dim New_Col As Boolean
Dim New_Column As Boolean

'g_CommonDialog.CancelError = True
On Error GoTo ErrHandler
'设置标志
'g_CommonDialog.Flags = cdlOFNHideReadOnly
'设置过滤器
'g_CommonDialog.Filter = "All Fills (*.*)|*.*|Excel Files" & "(*.xls)|*xls|Batch Files (*.bat)|*.bat"
'指定缺省的过滤器
'g_CommonDialog.FilterIndex = 2
'显示“打开”对话框
'g_CommonDialog.ShowSave

If flex.Rows <= 1 Then
MsgBox "没有数据!", vbInformation, g_Msgtitle
Exit Function
End If

Set xlApp = CreateObject("Excel.Application")

Set xlBook = xlApp.Workbooks.Open(FileName)
xlApp.Visible = False

With flex
xlApp.Cells(2, 3).Value = .TextMatrix(0, 0)
xlApp.Cells(6, 6).Value = .TextMatrix(0, 1)
xlApp.Cells(7, 3).Value = .TextMatrix(0, 2)
xlApp.Cells(7, 10).Value = .TextMatrix(0, 3)
xlApp.Cells(7, 13).Value = Grid1.TextMatrix(0, 4)
xlApp.Cells(11, 4).Value = Grid1.TextMatrix(0, 5)
xlApp.Cells(11, 5).Value = Grid1.TextMatrix(0, 6)
xlApp.Cells(11, 6).Value = Grid1.TextMatrix(0, 7)
xlApp.Cells(11, 7).Value = Grid1.TextMatrix(0, 8)
End With

With xlApp
.Rows(1).Font.Bold = True
.Cells.Select
.Columns.AutoFit
.Cells(1, 1).Select
.Application.Visible = True
End With

xlBook.SaveAs (g_CommonDialog.FileName)
xlApp.Application.Visible = False
xlApp.DisplayAlerts = False
xlApp.Quit
Set xlApp = Nothing
Set xlApp = Nothing
flex.SetFocus
MsgBox "数据已经导出到Excel中。", vbInformation, "成功"
Exit Function

ErrHandler:
'用户按了“取消”按钮
If Err.Number <> 32755 Then
MsgBox "数据导出失败!", vbInformation, "失败"
End If
End Function

  • 写回答

1条回答

  • wild84 2018-03-05 00:44
    关注

    调试查看 Err.number的值是多少?可获得具体原因然后具体分析

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?