【问题标题】:Import txt file with open file dialog box and break them into cells使用打开文件对话框导入 txt 文件并将它们分成单元格
【发布时间】:2015-01-15 06:15:30
【问题描述】:

我知道我在这行之后犯了一个错误: If intChoice <> 0 Then
有人可以帮我改正吗?

Private Sub CommandButton1_Click()
    Dim intChoice As Integer
    'Select the start folder
    Application.FileDialog(msoFileDialogOpen).InitialFileName = "I:\Dunnings"
    'make the file dialog visible to the user
    intChoice = Application.FileDialog(msoFileDialogOpen).Show
    'determine what choice the user made

    If intChoice <> 0 Then
        Workbooks.OpenText.Filename:= intChoice, Origin:=xlMSDOS, StartRow:=23, DataType:=xlFixedWidth, FieldInfo:= _
            Array(Array(0, 1), Array(6, 2), Array(23, 1), Array(30, 2), Array(63, 2), Array(68, 1), _
            Array(77, 4), Array(88, 4), Array(101, 1), Array(117, 1)), TrailingMinusNumbers:= _
            True
        NewPath = Mid(ThisWorkbook.FullName, 1, _
        Len(ThisWorkbook.FullName) - Len(ThisWorkbook.Name)) & "\" & _
            "Dunnings - " & Format(Date, "dd-mm-yyyy") & ".xlsm"
        ThisWorkbook.SaveAs (NewPath)
    End If
End Sub

【问题讨论】:

  • 请问什么错误和什么行?
  • 它在 If intChoice 0 之后的行中...不能在那里使用该变量 intChoice...

标签: vba excel


【解决方案1】:
Private Sub CommandButton1_Click()
    With Application.FileDialog(msoFileDialogOpen)
        .InitialFileName = "I:\"
        .Filters.Clear
        .Title = "Your Title"
        If Not .Show Then
            MsgBox "No file selected.": Exit Sub
        End If
        Workbooks.OpenText .SelectedItems(1), Origin:=xlMSDOS, StartRow:=23, _
                DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(6, 2), _
                Array(23, 1), Array(30, 2), Array(63, 2), Array(68, 1), _
                Array(77, 4), Array(88, 4), Array(101, 1), Array(117, 1)), _
                TrailingMinusNumbers:=True
        NewPath = Mid(ThisWorkbook.FullName, 1, _
            Len(ThisWorkbook.FullName) - Len(ThisWorkbook.Name)) & "\" & _
            "Dunnings - " & Format(Date, "dd-mm-yyyy") & ".xlsm"
        ThisWorkbook.SaveAs (NewPath)
    End With
End Sub

基本上可以使用RecordMacro记录其余代码,然后将生成的代码复制到您的VB代码中。

【讨论】:

  • 当我在一个新文件中尝试同样的技术时,它会出现一个错误,... NewPath = Mid(ThisWorkbook.FullName, 1, _ Len(ThisWorkbook.FullName) - Len(ThisWorkbook .Name)) & "\" & _ "Dunnings -" & Format(Date, "dd-mm-yyyy") & ".xlsm" ThisWorkbook.SaveAs (NewPath) 与此问题相同的错误 stackoverflow.com/questions/17173898/…
【解决方案2】:

IntChoice 是整数。
文件名 参数需要字符串。像这样的:

With Application.FileDialog(msoFileDialogOpen)
    .InitialFileName = "I:\Dunnings"
    .Filters.Clear
    .title = "Your Title"
    If Not .Show Then
        MsgBox "No file selected.": Exit Sub
    End If
    Workbooks.OpenText .SelectedItems(1) 'and the rest of your code.
End With

【讨论】:

  • 是的,我知道,与其调用特定位置的文件,我需要调用我在顶部选择的文件。这就是我挣扎的地方...我不擅长 VBA ...这是这个的主要问题....
  • 您的建议对我来说非常有效,我得到了我想要的结果。多谢。如果将来有人需要,这是我的最终结果...
  • 当我在一个新文件中尝试同样的技术时,它会出现错误,... NewPath = Mid(ThisWorkbook.FullName, 1, _ Len(ThisWorkbook.FullName) - Len(ThisWorkbook .Name)) & "\" & _ "Dunnings -" & Format(Date, "dd-mm-yyyy") & ".xlsm" ThisWorkbook.SaveAs (NewPath) 与此问题相同的错误 stackoverflow.com/questions/17173898/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-30
  • 1970-01-01
相关资源
最近更新 更多