Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables 基于条件语句和“创建新字符串变量”;合并"。dat文件?_Variables_Vba_Loops_Merge_Excel - Fatal编程技术网

Variables 基于条件语句和“创建新字符串变量”;合并"。dat文件?

Variables 基于条件语句和“创建新字符串变量”;合并"。dat文件?,variables,vba,loops,merge,excel,Variables,Vba,Loops,Merge,Excel,我正在尝试编写一个程序,在VBA excel中合并两个或多个.dat文件。基本上,它首先要求用户选择任意数量的文件(多于2个)。然后,它根据用户选择文件的顺序“合并”这些文件。通过合并,我的意思是附加或复制和粘贴一个选定的文件到以前选定的文件,并保存为一个全新的文件。我一直坚持将新变量作为字符串的一部分创建,因为我习惯于只为一个需要打开的文件弹出打开提示窗口。现在它是有条件的,取决于用户在消息框中的选择,即他是否希望另一个文件与上一个文件合并。它会一直询问这个问题,直到用户选择“否”或“取消”。

我正在尝试编写一个程序,在VBA excel中合并两个或多个.dat文件。基本上,它首先要求用户选择任意数量的文件(多于2个)。然后,它根据用户选择文件的顺序“合并”这些文件。通过合并,我的意思是附加或复制和粘贴一个选定的文件到以前选定的文件,并保存为一个全新的文件。我一直坚持将新变量作为字符串的一部分创建,因为我习惯于只为一个需要打开的文件弹出打开提示窗口。现在它是有条件的,取决于用户在消息框中的选择,即他是否希望另一个文件与上一个文件合并。它会一直询问这个问题,直到用户选择“否”或“取消”。因此,每次用户选择yes时,都需要创建一个新变量来存储稍后打开的文件名。我如何进行这个过程?还有,当用户点击“否”停止合并文件时,我如何同时打开所有这些文件,以及是否有任何巧妙的方法来附加或复制并粘贴.dat文件,我已经尝试了Hex编辑器:HxD,如何使用VBA操作Hex编辑程序

   Sub Merge()
    Dim Response, Message As String
  Dim File1 As String 'Needs new variable created each time user selects "ok" on msgbox
   ChDir "C:\"

File1 = Application.GetOpenFilename(Title:="Select File to be Merged")
If File1 = "False" Then Exit Sub
Message = "Select Another File To be Merged With?"
Response = MsgBox(Message, vbQuestion + vbOKCancel, "Merge Files")
 If Response = vbOK Then
  'Loop-mechanism to create a new variable each time. HOW?

 Else
'Open .dat files and start the copy and pasting process HOW with Hex Editor?:I'm using a program called "HxD"
 End If
 End Sub

谢谢

您可以这样循环,将名称存储在字符串数组中,然后分别访问每个字符串进行处理:

Sub Merge()
    Dim File1      As String 'Needs new variable created each time user selects "ok" on msgbox
    Dim AllFiles() As String
    Dim count      As Long

    ChDir "C:\"

    ReDim AllFiles(0)

    Do
        Application.EnableCancelKey = xlDisabled
        File1 = Application.GetOpenFilename("DAT Files (*.dat),*.dat", 1, "Select File to be Merged")
        Application.EnableCancelKey = xlErrorHandler

        If (File1 = "False") Then Exit Do
        ReDim Preserve AllFiles(count)
        AllFiles(count) = File1
        count = (count + 1)
        If (MsgBox("Select Another File To be Merged With?", vbQuestion + vbOKCancel, "Merge Files") = vbCancel) Then Exit Do
    Loop

    If (count = 0) Then
        MsgBox "No selection"
        Exit Sub
    End If

    For count = 0 To UBound(AllFiles)
        MsgBox "User selected file name: " & AllFiles(count)
        '//boogy
    Next
End Sub

GetOpenFilename
也支持
MultiSelect
参数,但是它只在单个目录中工作,并且所选文件的顺序不受保证。

您可以这样循环,将名称存储在字符串数组中,然后逐个访问每个名称进行处理:

Sub Merge()
    Dim File1      As String 'Needs new variable created each time user selects "ok" on msgbox
    Dim AllFiles() As String
    Dim count      As Long

    ChDir "C:\"

    ReDim AllFiles(0)

    Do
        Application.EnableCancelKey = xlDisabled
        File1 = Application.GetOpenFilename("DAT Files (*.dat),*.dat", 1, "Select File to be Merged")
        Application.EnableCancelKey = xlErrorHandler

        If (File1 = "False") Then Exit Do
        ReDim Preserve AllFiles(count)
        AllFiles(count) = File1
        count = (count + 1)
        If (MsgBox("Select Another File To be Merged With?", vbQuestion + vbOKCancel, "Merge Files") = vbCancel) Then Exit Do
    Loop

    If (count = 0) Then
        MsgBox "No selection"
        Exit Sub
    End If

    For count = 0 To UBound(AllFiles)
        MsgBox "User selected file name: " & AllFiles(count)
        '//boogy
    Next
End Sub

GetOpenFilename
也支持
MultiSelect
参数,但是它只在一个目录中工作,并且所选文件的顺序不能保证。

非常感谢,我是vba新手,如果这是c,Id问题。知道如何编写语法。再次感谢!嘿,现在我正试图打开存储在allfile数组中的单个文件,但在打开时遇到了问题。这会给我一个错误,因为下标超出范围:工作簿。打开文件名:=AllFiles(1)如果只有1个文件,它将是@AllFiles(0)。非常感谢,我是vba新手,如果这是c,Id问题。知道如何编写语法。再次感谢!嘿,现在我正在尝试打开存储在allfile数组中的单个文件,但执行此操作时遇到问题。这会给我一个错误,因为下标超出范围:工作簿。打开文件名:=AllFiles(1)如果只有1个文件,则它将为@AllFiles(0)