如何选择不选择文件以及选择文件时的条件 [英] How do I give the option of not selecting a file as well as the conditions when a file is selected

查看:78
本文介绍了如何选择不选择文件以及选择文件时的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我选择一个文件时代码没有显示任何内容,因为它退出了sub我认为。但是如果我删除了退出子并选择了一个文件就可以了但如果我没有选择一个文件它显示msgbx然后它也来找不到文件。如何编辑我的代码以便它工作



我尝试过:



the code doesnot show anything when i select a file because its exiting the sub i think.but if i remove the exit sub and select a file its okay but if i do not select a file its showing the msgbx but then its also coming as file not found. how to edit my code so that it works

What I have tried:

Private Sub CommandButton1_Click()
Worksheets("newdata").Activate
Dim scan As Integer
Dim myfilepath As String
myfilepath = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If myfilepath = CStr(False) Then MsgBox ("no files")
Exit Sub
Open myfilepath For Input As #1
x = 0
Do Until EOF(1)
    Line Input #1, linefromfile
    lineitems = Split(linefromfile, vbTab)
    For scan = 0 To UBound(lineitems) - 1
            lineitems(scan) = Replace(lineitems(scan), Chr(34), "")
    Next
    ActiveCell.Offset(x, 0).Value = lineitems(0)
    ActiveCell.Offset(x, 1).Value = lineitems(1)
    ActiveCell.Offset(x, 2).Value = lineitems(2)
    ActiveCell.Offset(x, 3).Value = lineitems(3)
    ActiveCell.Offset(x, 4).Value = lineitems(4)
    ActiveCell.Offset(x, 5).Value = lineitems(5)
    ActiveCell.Offset(x, 6).Value = lineitems(6)
    ActiveCell.Offset(x, 7).Value = lineitems(7)
    ActiveCell.Offset(x, 8).Value = lineitems(8)
    ActiveCell.Offset(x, 9).Value = lineitems(9)
    ActiveCell.Offset(x, 10).Value = lineitems(10)
    ActiveCell.Offset(x, 11).Value = lineitems(11)
    ActiveCell.Offset(x, 12).Value = lineitems(12)
    
    x = x + 1
Loop

Close #1
End Sub

推荐答案

你有可能忘了结束如果

Is it possible that you forgot the End If
If myfilepath = CStr(False) Then
    MsgBox ("no files")
    Exit Sub
End If



嵌入退出Sub


这篇关于如何选择不选择文件以及选择文件时的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆