开发者

Create New String Variables based on conditional statement and "merging" .dat files?

I'm trying to write a program that merge two or more .dat files开发者_如何学Go in VBA excel. Basically, it first asks the user to select any number of files (more than 2). Then it "merges" the files based on the order that the user had selected them. By merge, I mean append or copy and paste one selected file into the previous selected file, and save as a brand new file. I'm stuck on on creating the new variables as Strings part, since I'm used to having the open prompt window pop up for just one file that need to be opened. Now it is conditional based on user's selection at the message box of whether he wants another file merged with the previous. It keeps asking this until the user selects no or cancel. So each time the user selects yes there needs to be a new variable created to store the file name to be opened later. How do I go about this process? And also How do I open up all of these files at the same time once the user hits "no" to stop merging the files, and is there any clever way to append or Copy and Paste .dat files, I've tried Hex Editor: HxD, How do I manipulate the Hex Edit program with VBA?

   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

Thanks!


You can loop like this storing the names in an array of strings, then subsequently access each one individually for processing:

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 also support an MultiSelect argument however it only works in a single directory and the order of selected files is not guaranteed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜