How to merge files?

mrdigitalis

New Member
Joined
Jul 23, 2022
Messages
7
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Dear Formmembers,

I am new to VBA and wrote some code to open 2 files:
1. Mutation file (tab delimited)
2. EXP file (semicolon delimited)

Here is my code so far:
VBA Code:
Sub MergeMutaties()

    Dim Mutatie_File As Variant
    Dim wsMaster1 As Worksheet
    Dim wbTextImport1 As Workbook

    Dim EXP_File As Variant
    Dim wsMaster2 As Worksheet
    Dim wbTextImport2 As Workbook

    Application.ScreenUpdating = False

    'Code om Mutatie-file te openen als text
    Mutatie_File = Application.GetOpenFilename(Title:="Selecteer de Mutatie-file ...", FileFilter:="Mutatie Files (*.txt*), *txt*")
    If Mutatie_File = False Then
        MsgBox "No file selected."
    Else
        Workbooks.OpenText _
                    Filename:=Mutatie_File, _
                    FieldInfo:=Array( _
                      Array(1, xlTextFormat), _
                      Array(2, xlTextFormat), _
                      Array(3, xlTextFormat), _
                      Array(4, xlTextFormat), _
                      Array(5, xlTextFormat), _
                      Array(6, xlTextFormat), _
                      Array(7, xlTextFormat), _
                      Array(8, xlTextFormat) _
                      ), _
                    StartRow:=1, _
                    DataType:=xlDelimited, _
                    Tab:=True
                    
        Set wbTextImport1 = ActiveWorkbook
        Set wsMaster1 = ThisWorkbook.Worksheets("Mutatie_File")

        wbTextImport1.Worksheets(1).Range("A1").CurrentRegion.Copy wsMaster1.Range("A1")
        
        wbTextImport1.Close False
    End If

    'Code om EXP-file te openen als text
    EXP_File = Application.GetOpenFilename(Title:="Selecteer de EXP-file ...", FileFilter:="EXP Files (*.exp*), *exp*")
    If EXP_File = False Then
        MsgBox "No file selected."
    Else
        Workbooks.OpenText _
                    Filename:=EXP_File, _
                    FieldInfo:=Array( _
                      Array(1, xlTextFormat), _
                      Array(2, xlTextFormat), _
                      Array(3, xlTextFormat), _
                      Array(4, xlTextFormat), _
                      Array(5, xlTextFormat), _
                      Array(6, xlTextFormat), _
                      Array(7, xlTextFormat), _
                      Array(8, xlTextFormat), _
                      Array(9, xlTextFormat), _
                      Array(10, xlTextFormat), _
                      Array(11, xlTextFormat) _
                      ), _
                    StartRow:=1, _
                    DataType:=xlDelimited, _
                    Tab:=False, _
                    Semicolon:=True
                    
                    
        Set wbTextImport2 = ActiveWorkbook
        Set wsMaster2 = ThisWorkbook.Worksheets("EXP_File")

        wbTextImport2.Worksheets(1).Range("A1").CurrentRegion.Copy wsMaster2.Range("A1")
        
        wbTextImport2.Close False
    End If
I have now three sheet tabs (Merge_Result / Mutation_File / EXP_File)
Here is a fragment of the EXP file:
Ingangsdatum:21/07/2023
0000100xxxV1015:1012:527h42
Op/Afstaptijd5:10xxxxxx5:13
Prep-out5:13xxxxxx5:15
MAT1800101562395:15xxxxxx5:38
00000180010115015:38xxxxxx6:27
MAT180010122336:27xxxxxx6:29
00000180010115106:33xxxxxx7:00
Loop/Reis7:00xxxxxx7:01
Pauze7:01xxxxxx7:21
Loop/Reis7:21xxxxxx7:22
00000180011115117:22xxxxxx7:53
00000180011115057:55xxxxxx8:14
00000180011115078:26xxxxxx8:35
00000180011115088:35xxxxxx8:43
00000180011115108:53xxxxxx9:16
00000180011115389:18xxxxxx9:43
Loop/Reis9:43xxxxxx9:44
Pauze9:44xxxxxx10:15
Loop/Reis10:15xxxxxx10:16
000001800127154410:16xxxxxx10:47
MAT18001275579510:47xxxxxx10:53
000001800127154511:09xxxxxx12:13
000001800127152312:15xxxxxx12:34
MAT18001275467412:34xxxxxx12:48
Prep-in12:48xxxxxx12:50
Op/Afstaptijd12:50xxxxxx12:52
0000100xxxT10212:04xxxxxx
Op/Afstaptijd12:04xxxxxx12:07
Prep-out12:07xxxxxx12:09
MAT18005155457012:09xxxxxx12:23
000001800515152412:23xxxxxx12:45
000001800515158212:47xxxxxx13:12
13:12xxxxxx13:23
000001800204153913:23xxxxxx13:50
000001800204154813:56xxxxxx14:47
000001800204155514:56xxxxxx15:20
Loop/Reis15:20xxxxxx15:21
Pauze15:21xxxxxx16:07
Loop/Reis16:07xxxxxx16:10
180014016:10xxxxxx19:50
MAT18001405636719:50xxxxxx19:56
Prep-in19:56xxxxxx19:58
Op/Afstaptijd19:58xxxxxx20:00
0000100xxxV1035:14xxxxxx
Op/Afstaptijd5:14xxxxxx5:17
Prep-out5:17xxxxxx5:19
MAT1800102570715:19xxxxxx5:54
000001800102V80025:54xxxxxx6:22
Pauze6:22xxxxxx6:50
000001800102V80096:50xxxxxx8:27
000001800102V80188:33xxxxxx10:05
000001800102V802010:07xxxxxx10:26
MAT18001025707210:26xxxxxx10:29
Loop/Reis10:29xxxxxx10:32
Pauze10:32xxxxxx11:07
Loop/Reis11:07xxxxxx11:08
000001800506103911:08xxxxxx11:31
000001800506104811:41xxxxxx12:40
000001800506105712:45xxxxxx13:20
0000Loop/Reis12713:23xxxxxx13:29
Op/Afstaptijd13:29xxxxxx13:31
0000100xxxT10412:09xxxxxx
Op/Afstaptijd12:09xxxxxx12:12
Prep-out12:12xxxxxx12:14
MAT18002105629712:14xxxxxx12:20
000001800210162812:20xxxxxx13:10
000001800210162913:12xxxxxx13:48
MAT18002105629913:48xxxxxx14:14
MAT18002105630014:27xxxxxx14:29
000001800210203214:29xxxxxx14:58
000001800210203515:00xxxxxx15:26
MAT18002105636515:26xxxxxx15:29
Loop/Reis15:29xxxxxx15:32
Pauze15:32xxxxxx16:07
Loop/Reis16:07xxxxxx16:10
180021016:10xxxxxx19:50
MAT18002105636419:50xxxxxx19:56
Prep-in19:56xxxxxx19:58
Op/Afstaptijd19:58xxxxxx20:00
0000100xxxV1055:17xxxxxx
Op/Afstaptijd5:17xxxxxx5:20
Prep-out5:20xxxxxx5:22
MAT1800103544065:22xxxxxx5:36
00000180010310015:36xxxxxx6:24
00000180010310066:34xxxxxx7:25
00000180010310157:33xxxxxx8:05
Loop/Reis8:05xxxxxx8:06
Pauze8:06xxxxxx8:31
Loop/Reis8:31xxxxxx8:32
00000180011915248:32xxxxxx9:02
00000180011915299:08xxxxxx10:00
MAT1800119224310:00xxxxxx10:02
000001800119154810:04xxxxxx10:29
Loop/Reis10:29xxxxxx10:30
Pauze10:30xxxxxx10:52
Loop/Reis10:52xxxxxx10:53
000001800504103710:53xxxxxx11:16
000001800504104611:26xxxxxx12:25
000001800504105512:30xxxxxx13:05
0000Loop/Reis12613:08xxxxxx13:14
Op/Afstaptijd13:14xxxxxx13:16
0000100xxxT10612:26xxxxxx
Op/Afstaptijd12:26xxxxxx12:29
Loop/Reis12:29xxxxxx12:30
000001800148152312:30xxxxxx12:42
000001800148151712:49xxxxxx13:25
000001800148152013:27xxxxxx14:04
MAT18001485639814:04xxxxxx14:07
Loop/Reis14:07xxxxxx14:10
Pauze14:10xxxxxx14:32
Loop/Reis14:32xxxxxx14:35
MAT18001485639914:35xxxxxx15:01
000001800148163815:01xxxxxx15:44
000001800148163915:46xxxxxx16:22
000001800148164416:31xxxxxx17:14
000001800148164517:16xxxxxx17:50
MAT18001485702017:50xxxxxx18:16
Loop/Reis18:16xxxxxx18:19
Pauze18:19xxxxxx18:35
Loop/Reis18:35xxxxxx18:37
MAT18001485702118:37xxxxxx18:40
0000018001487318:40xxxxxx19:00
0000018001487419:03xxxxxx19:24
MAT18001485628819:24xxxxxx19:27
MAT18001485628919:38xxxxxx19:40
0000018001488519:40xxxxxx20:00
0000018001488620:03xxxxxx20:24
MAT18001485584020:24xxxxxx20:30
Prep-in20:30xxxxxx20:32
Op/Afstaptijd20:32xxxxxx20:34
0000100xxxV1075:19xxxxxx
Op/Afstaptijd5:19xxxxxx5:22
Prep-out5:22xxxxxx5:24
MAT1800104545855:24xxxxxx5:36
00000180010415025:36xxxxxx6:24
00000180010415096:38xxxxxx7:30
MAT180010422287:30xxxxxx7:32
50000180010415187:34xxxxxx7:59
Loop/Reis7:59xxxxxx8:00
Pauze8:00xxxxxx8:21
Loop/Reis8:21xxxxxx8:22
00000180013615198:22xxxxxx8:53
00000180013615098:55xxxxxx9:14
00000180013615119:26xxxxxx9:35
00000180013615129:35xxxxxx9:43
00000180013615149:54xxxxxx10:16
000001800136155010:18xxxxxx10:43
Loop/Reis10:43xxxxxx10:44
Pauze10:44xxxxxx11:11
Loop/Reis11:11xxxxxx11:14
MAT18001025707311:14xxxxxx11:16
000001800102152211:16xxxxxx11:57
000001800102152011:59xxxxxx12:29
000001800102152112:31xxxxxx12:59
000001800102152713:01xxxxxx13:30
Loop/Reis13:30xxxxxx13:31
Op/Afstaptijd13:31xxxxxx13:33
0000100xxxL10813:12xxxxxx
Op/Afstaptijd13:12xxxxxx13:15
0000Loop/Reis2213:15xxxxxx13:21
000001800513155913:23xxxxxx13:50
MAT1800513223813:50xxxxxx13:52
000001800513159614:02xxxxxx15:00
MAT1800513228115:00xxxxxx15:06
000001800513157915:20xxxxxx16:19
MAT1800513224816:19xxxxxx16:21
000001800513162616:32xxxxxx16:57
Loop/Reis16:57xxxxxx16:58
Pauze16:58xxxxxx17:50
Loop/Reis17:50xxxxxx17:51
000001800501159517:51xxxxxx18:20
MAT18005015465418:20xxxxxx18:37
000001800501V154918:58xxxxxx19:38
MAT18005015698419:38xxxxxx20:06
000001800501V155520:28xxxxxx21:08
MAT18005015465721:08xxxxxx21:14
Prep-in21:14xxxxxx21:16
Op/Afstaptijd21:16xxxxxx21:18

And a frament of the Mutation file:
T 102000010016:1019:50Go to bla bla
V 10300001005:546:22Do something
V 103000010010:0710:26Do something
T 104000010016:1019:50Do something
T 106000010014:3515:01Do something
T 106000010017:5018:16Do something
L 108000010019:3820:06Do something
V 10900001007:468:27Do something
V 109000010010:4010:43Do something
V 109000010010:4310:59Do something
L 110000010017:3518:01Do something

What I want to do is read the first row of the mutation file, and then search for 0000100 in the EXP file, and when that is found, search for 102 in the same row. When not found go to the next 0000100 and search again for 102. When that is found search for 16:10 AND 19:50 in the rows below found 102 and the result must be found before the next 0000100. When times are found past Go to bla bla in the EXP file in the same row as found the times, in column I. And the next row and so on. The search direction is from top to bottom. What is the best approach for something like this?
The result looks like this:
0000100xxxT10212:04xxxxxx
Op/Afstaptijd12:04xxxxxx12:07
Prep-out12:07xxxxxx12:09
MAT18005155457012:09xxxxxx12:23
000001800515152412:23xxxxxx12:45
000001800515158212:47xxxxxx13:12
13:12xxxxxx13:23
000001800204153913:23xxxxxx13:50
000001800204154813:56xxxxxx14:47
000001800204155514:56xxxxxx15:20
Loop/Reis15:20xxxxxx15:21
Pauze15:21xxxxxx16:07
Loop/Reis16:07xxxxxx16:10
180014016:10xxxxxx19:50Go to bla bla
MAT18001405636719:50xxxxxx19:56
Prep-in19:56xxxxxx19:58
Op/Afstaptijd19:58xxxxxx20:00

I have no idea how to do that. Who will show me the way?
Thanks in advance!
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
No one an idea of how to retrieve the data? I am asking for hints. Not for a complete script 😅. Do I need the find method or the index match method or make an array. Any help and suggestions are welcome!
 
Upvote 0
This worked for me:
VBA Code:
Sub ZoekLetterEnNummer()
    Dim expSheet As Worksheet
    Dim mutSheet As Worksheet
    Dim lastMutRow As Long
    Dim lastExpRow As Long
    Dim i As Long
    Dim j As Long
    Dim foundRow As Long
    Dim Mut_Dienst As String
    Dim Mut_DagNummer As String
    Dim Mut_BeginTijd As String
    Dim Mut_EindTijd As String
    Dim Mut_Tekst As String
    Dim dienstLetter As String
    Dim dienstNummer As String

    ' Schakel het werkblad "Mutatie_File" in
    Set mutSheet = Sheets("Mutatie_File")

    ' Vind het laatste gebruikte rijnummer in kolom A van het werkblad "Mutatie_File"
    lastMutRow = mutSheet.Cells(mutSheet.Rows.Count, "A").End(xlUp).Row

    ' Loop door elke rij in het werkblad "Mutatie_File"
    For i = 1 To lastMutRow
        ' Haal de waarden op voor de huidige rij uit "Mutatie_File"
        Mut_Dienst = mutSheet.Cells(i, "A").Value
        Mut_DagNummer = mutSheet.Cells(i, "B").Value
        Mut_BeginTijd = mutSheet.Cells(i, "C").Value
        Mut_EindTijd = mutSheet.Cells(i, "D").Value
        Mut_Tekst = Trim(mutSheet.Cells(i, "E").Value) & " " & Trim(mutSheet.Cells(i, "F").Value) & " " & Trim(mutSheet.Cells(i, "G").Value) & " " & Trim(mutSheet.Cells(i, "H").Value)

        ' Controleer of de waarde in cel A van "Mutatie_File" een spatie bevat om de dienstletter en dienstnummer te scheiden
        If InStr(1, Mut_Dienst, " ") > 0 Then
            ' Split de waarde in twee delen: dienstletter en dienstnummer
            dienstLetter = Trim(Left(Mut_Dienst, InStr(1, Mut_Dienst, " ") - 1))
            dienstNummer = Trim(Mid(Mut_Dienst, InStr(1, Mut_Dienst, " ") + 1))
        Else
            ' Als er geen spatie is gevonden, geef een foutmelding en ga door met de volgende rij
            MsgBox "Geen spatie gevonden in cel A" & i & " van Mutatie_File.", vbExclamation
            GoTo NextRow
        End If

        ' Schakel het werkblad "EXP_File" in
        Set expSheet = Sheets("EXP_File")

        ' Vind het laatste gebruikte rijnummer in kolom A van het werkblad "EXP_File"
        lastExpRow = expSheet.Cells(expSheet.Rows.Count, "A").End(xlUp).Row

        foundRow = 0 ' Reset de gevonden rij naar 0 voor elke nieuwe rij in "Mutatie_File"

        ' Doorloop elke rij in het werkblad "EXP_File" om de overeenkomende letter en nummer te vinden
        For j = 1 To lastExpRow
            If Trim(expSheet.Cells(j, "A").Value) = Mut_DagNummer And Trim(expSheet.Cells(j, "C").Value) = dienstLetter And Trim(expSheet.Cells(j, "D").Value) = dienstNummer Then
                ' Als de overeenkomende letter en nummer zijn gevonden, sla de rij op en verlaat de loop
                foundRow = j
                Exit For ' Verlaat de loop nadat de overeenkomende rij is gevonden
            End If
        Next j

        ' Als er geen overeenkomende rij is gevonden, toon een berichtvenster
        If foundRow = 0 Then
            MsgBox "Geen overeenkomende rij gevonden in EXP_File voor rij " & i & " van Mutatie_File.", vbInformation
            GoTo NextRow ' Ga door met de volgende rij in "Mutatie_File"
        End If

        ' Zoek nu vanaf de gevonden rij naar de volgende waardes van cel C (Mut_BeginTijd) en D (Mut_EindTijd)
        For j = foundRow To lastExpRow
            If Trim(expSheet.Cells(j, "E").Value) = Mut_BeginTijd And Trim(expSheet.Cells(j, "H").Value) = Mut_EindTijd Then
                expSheet.Cells(j, "J").Value = Mut_Tekst
                ' Als de waarden van cel C en D zijn gevonden, toon een berichtvenster met de gevonden celwaarden
                Debug.Print "Gevonden in rij " & j & " - Kolom E waarde: " & expSheet.Cells(j, "E").Value & ", Kolom H waarde: " & expSheet.Cells(j, "H").Value
                Debug.Print "Ik heb hem gevonden!"
                Exit For ' Verlaat de loop nadat de waarden van cel C en D zijn gevonden
            End If
        Next j

        ' Als er geen overeenkomende waarden zijn gevonden, toon een berichtvenster
        If j > lastExpRow Then
            Debug.Print "Geen overeenkomende waarden gevonden in EXP_File voor rij " & i & " van Mutatie_File.", vbInformation
        End If

NextRow:
    Next i

    Debug.Print "Einde programma"
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top