前段时间给一个朋友做了一个Excel数据的导入,当时要求是做两个不同表的数据导入,为了方便 所以都做在一个窗体了,每次在导入的时候都需要在组合框设置导入的类型,如下图:
感觉有点麻烦,后来想到用instr,instrRev,Mid 函数截取关键字,然后赋值到组合框,这样就可以减少操作,也防止了人手操作的误操作。
想想有时候懒也是前进的动力
Private Sub btnBroswer_Click()
Dim strFileName As String
Dim strFileType As String
With Application.FileDialog(msoFileDialogOpen)
.InitialFileName = CurrentProject.Path
.Filters.Clear
.Filters.Add "Microsoft Excel", "*.xls*"
If .Show = -1 Then
Me.txtFileName = .SelectedItems(1)
Else
Exit Sub
End If
End With
'加了一下几行代码意义就不一样了
strFileType = Mid(Me.txtFileName, InStrRev(Me.txtFileName, "\") + 1)
' Debug.Print strFileType
If InStr(strFileType, "商城") Then Me.cboSourceType = "商城"
If InStr(strFileType, "充值") Then Me.cboSourceType = "充值"
'为什么要用mid 套着 instrRev 而不是直接使用instr直接取值 是为了防止数据的路径也有同样的关键字导致导致取值错误
End Sub
Private Sub btnImport_Click()
Dim strMsg As String
Select Case Me.cboSourceType.Column(0)
Case "商城"
strMsg = "您选择了商城选项"
Case "充值"
strMsg = "您选择了充值选项"
Case Else
strMsg = "您没有选择选项"
End Select
MsgBox strMsg
End Sub
附 件:
点击下载此附件