Option Explicit
Private Sub Form_Load()
Call msh_初始化
End Sub
Private Sub Form_Resize()
With msh
.Left = 0
.Top = 0
.Width = Me.ScaleWidth
.Height = Me.ScaleHeight
End With
End Sub
Private Sub msh_初始化()
With msh
.Clear
.Cols = 1
.Rows = 2
.FixedCols = 0
.FixedRows = 1
.AllowUserResizing = flexResizeColumns
End With
End Sub
Private Sub mnu导入Excel_Click()
Dim xls As Excel.Application, xlsWorkBook As Excel.Workbook, xlsWorksheet As Excel.Worksheet
Dim strFile As String, lngRows As Long, lngCols As Long, iRow As Long, iCol As Long, strCaption As String
With cdg
.DialogTitle = "导入Excel文件"
.Filter = "Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*"
.ShowOpen
strFile = .FileName
End With
If strFile = "" Then
MsgBox "未选中文件!", vbOKOnly Or vbInformation, "提示"
Exit Sub
End If
Set xls = CreateObject("Excel.Application")
If xls Is Nothing Then
MsgBox "未发现Excel组件,请检查!", vbOKOnly Or vbInformation, "提示"
Exit Sub
End If
Set xlsWorkBook = xls.Workbooks.Open(strFile)
Set xlsWorksheet = xlsWorkBook.Sheets(1)
lngRows = xlsWorksheet.UsedRange.Rows.Count
lngCols = xlsWorksheet.UsedRange.Columns.Count
If lngRows >= 1 Or lngCols >= 1 Then
If MsgBox("导入工作可能持续一段时间,您是否真的确定要导入?", vbOKCancel Or vbQuestion, "提示") = vbCancel Then
Exit Sub
End If
Screen.MousePointer = vbHourglass
strCaption = Me.Caption
Call msh_初始化
With msh
.Cols = lngCols
.Rows = lngRows
For iRow = 1 To lngRows Step 1
Me.Caption = "正在导入:" & iRow & " / " & lngRows
For iCol = 1 To lngCols Step 1
.TextMatrix(iRow - 1, iCol - 1) = xlsWorksheet.Cells(iRow, iCol)
Next iCol
Next iRow
Me.Caption = strCaption
End With
Screen.MousePointer = vbDefault
End If
Set xlsWorksheet = Nothing
Set xlsWorkBook = Nothing
Set xls = Nothing
End Sub
Private Sub Form_Load()
Call msh_初始化
End Sub
Private Sub Form_Resize()
With msh
.Left = 0
.Top = 0
.Width = Me.ScaleWidth
.Height = Me.ScaleHeight
End With
End Sub
Private Sub msh_初始化()
With msh
.Clear
.Cols = 1
.Rows = 2
.FixedCols = 0
.FixedRows = 1
.AllowUserResizing = flexResizeColumns
End With
End Sub
Private Sub mnu导入Excel_Click()
Dim xls As Excel.Application, xlsWorkBook As Excel.Workbook, xlsWorksheet As Excel.Worksheet
Dim strFile As String, lngRows As Long, lngCols As Long, iRow As Long, iCol As Long, strCaption As String
With cdg
.DialogTitle = "导入Excel文件"
.Filter = "Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*"
.ShowOpen
strFile = .FileName
End With
If strFile = "" Then
MsgBox "未选中文件!", vbOKOnly Or vbInformation, "提示"
Exit Sub
End If
Set xls = CreateObject("Excel.Application")
If xls Is Nothing Then
MsgBox "未发现Excel组件,请检查!", vbOKOnly Or vbInformation, "提示"
Exit Sub
End If
Set xlsWorkBook = xls.Workbooks.Open(strFile)
Set xlsWorksheet = xlsWorkBook.Sheets(1)
lngRows = xlsWorksheet.UsedRange.Rows.Count
lngCols = xlsWorksheet.UsedRange.Columns.Count
If lngRows >= 1 Or lngCols >= 1 Then
If MsgBox("导入工作可能持续一段时间,您是否真的确定要导入?", vbOKCancel Or vbQuestion, "提示") = vbCancel Then
Exit Sub
End If
Screen.MousePointer = vbHourglass
strCaption = Me.Caption
Call msh_初始化
With msh
.Cols = lngCols
.Rows = lngRows
For iRow = 1 To lngRows Step 1
Me.Caption = "正在导入:" & iRow & " / " & lngRows
For iCol = 1 To lngCols Step 1
.TextMatrix(iRow - 1, iCol - 1) = xlsWorksheet.Cells(iRow, iCol)
Next iCol
Next iRow
Me.Caption = strCaption
End With
Screen.MousePointer = vbDefault
End If
Set xlsWorksheet = Nothing
Set xlsWorkBook = Nothing
Set xls = Nothing
End Sub
本文介绍了一种使用VBA从Excel文件中读取数据的方法。通过创建Excel应用程序对象并打开指定的工作簿,可以获取工作表中的数据,并将其导入到当前应用的表格中。此过程包括文件选择、错误处理及数据导入的进度显示。
512

被折叠的 条评论
为什么被折叠?



