运行时间错误1004,未选择任何数据进行解析

时间:2016-06-11 07:58:45

标签: excel-vba vba excel

我将SVG文件放在文件夹中,我已经编写了vba代码来获取SVG文件中的XML数据,这里是代码

Sub Macro4()

Dim lines As Long
Dim letter As String
Dim no As String
Dim count As Integer
Dim i As Integer
Dim xRow As Long
Dim xDirect$, xFname$, InitialFoldr$

    count = Sheets.count
    If count > 1 Then
    For i = 1 To Sheets.count
        Sheets(i).Select
        On Error GoTo loopexit
        If ActiveSheet.Name = "Execute" Then
        ElseIf ActiveSheet.Name = "Sheet1" Then
        Else
            Application.DisplayAlerts = False
            ActiveSheet.Delete
            Application.DisplayAlerts = False
            i = i - 1
        End If
    Next i
    End If
loopexit:
    InitialFoldr$ = "C:\"
    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = Application.DefaultFilePath & "\"
        .Title = "Please select a folder to list Files from"
        .InitialFileName = InitialFoldr$
        .Show
            If .SelectedItems.count <> 0 Then
                xDirect$ = .SelectedItems(1) & "\"
                xFname$ = Dir(xDirect$, 7)
                Cells(1, 1).Select
                    Do While xFname$ <> ""
                       Application.DisplayAlerts = False
                       With ActiveSheet.QueryTables.Add(Connection:= _
                            "TEXT;" & xDirect$ & xFname$, Destination:=Range( _
                            "$A$1"))
                        .Name = Replace(xFname$, ".svg", "")
                        .FieldNames = True
                        .RowNumbers = False
                        .FillAdjacentFormulas = False
                        .PreserveFormatting = True
                        .RefreshOnFileOpen = False
                        .RefreshStyle = xlInsertDeleteCells
                        .SavePassword = False
                        .SaveData = True
                        .AdjustColumnWidth = True
                        .RefreshPeriod = 0
                        .TextFilePromptOnRefresh = False
                        .TextFilePlatform = 932
                        .TextFileStartRow = 1
                        .TextFileParseType = xlDelimited
                        .TextFileTextQualifier = xlTextQualifierDoubleQuote
                        .TextFileConsecutiveDelimiter = False
                        .TextFileTabDelimiter = False
                        .TextFileSemicolonDelimiter = False
                        .TextFileCommaDelimiter = False
                        .TextFileSpaceDelimiter = False
                        .TextFileColumnDataTypes = Array(1)
                        .TextFileTrailingMinusNumbers = True
                        .Refresh BackgroundQuery:=False
                        Application.DisplayAlerts = False
                    End With
                    Rows("1:1").Select
                    Selection.Insert Shift:=xlDown
                    lines = ActiveSheet.Range("A65536").End(xlUp).Row
                    Rows("1:1").Select
                    Selection.AutoFilter
                    Range("A1").Select
                    ActiveSheet.UsedRange.AutoFilter Field:=1, Criteria1:= _
                        "<>*text transform*", Operator:=xlAnd
                    ActiveSheet.Range("$A$1:$I$" & lines).Offset(1, 0).SpecialCells _
                        (xlCellTypeVisible).EntireRow.Delete
                    Range("A1").Select
                    Selection.AutoFilter
                    Range("A2").Select
                    Range(Selection, Selection.End(xlDown)).Select
                    Columns("A:A").Select
                    Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
                        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
                        Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
                        :=">", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1)), _
                        TrailingMinusNumbers:=True

我得到运行时错误1004 ,所以我决定在代码中逐行执行 我正在应用过滤器来过滤数据“文本转换”和我将删除的剩余数据,所以在代码中

 Rows("1:1").Select
                    Selection.AutoFilter
                    Range("A1").Select
                    ActiveSheet.UsedRange.AutoFilter Field:=1, Criteria1:= _
                        "<>*text transform*", Operator:=xlAnd
                    ActiveSheet.Range("$A$1:$I$" & lines).Offset(1, 0).SpecialCells _
                        (xlCellTypeVisible).EntireRow.Delete

它将删除除“文本转换”数据之外的所有数据,但是当我执行此过滤器时删除我在 XML文件中获得的所有数据,因此它在行上显示错误无数据被选中解析

Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
                        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
                        Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
                        :=">", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1)), _
                        TrailingMinusNumbers:=True

为什么会出现这种错误,有人可以帮我纠正吗?

1 个答案:

答案 0 :(得分:0)

如果不是范围(&#34; C:C&#34;)。查找(&#34; *&#34;)什么都没有那么&#39;如果它不是什么,那么列包含数据

&#39;运行您的代码

否则

不要运行您的代码 - 做其他事情,例如警告用户?

结束如果

检查链接:https://www.mrexcel.com/forum/excel-questions/447678-runtime-error-1004-no-data-selected-parse.html