CustomColorView bounds: (0.0, 0.0, 320.0, 227.0)
coloredView subview bounds: (0.0, 0.0, 320.0, 227.0)
我有一个宏,可以将设定范围导出到CSV文件。如果在InputBox上选择了Cancel,我希望宏退出Sub,但如果选择OK则执行代码。
无论是否选择了“确定”或“取消”,我上面的代码都会退出sub。
如果没有 Sub Export_Click()
Dim Rng As Range
Dim WorkRng As Range
Dim vFile As Variant
Dim x As Range
On Error Resume Next
TitleId = "Export to CSV file"
x = WorkRng
Set WorkRng = Range("D12:I100")
Set WorkRng = Application.InputBox("Range", TitleId, WorkRng.Address, Type:=8)
If x = "" Then Exit Sub
Application.ActiveSheet.Copy
Application.ActiveSheet.Cells.Clear
WorkRng.Copy Application.ActiveSheet.Range("A1")
vFile = Application.GetSaveAsFilename(InitialFileName:=".csv", _
FileFilter:="CSV files (*.csv), *.csv, All files (*.*), *.*")
If vFile <> False Then ThisWorkbook.SaveAs Filename:=vFile, FileFormat:=xlCS
End Sub
语句,代码将导出到csv,无论是否正确或取消。我已经尝试了下面的一些其他选项,看起来问题可能是输入是一个范围。我相对缺乏经验,在搜索本网站和其他网站时已经没有想法了。
我参考了链接尝试了代码的变体:
Handle cancellation of InputBox to select range
我也试过合并下面的代码,但无论选择OK还是取消,它都会导致exit sub:
If x = "" Then Exit Sub
答案 0 :(得分:-1)
如果选择了False
,则输入框会返回Cancel
,因此需要遵循以下代码:
Dim Text As Variant 'You can ommit "As Variant"
Text = Inputbox("Type the text")
'Check for Text type
If TypeName(Text) = "Boolean" Then
'Check if it is False
If Not Text Then Exit Sub
End If