Summary: MT4's "Save as Detailed Statement" often exports each trade as two messy, misaligned rows. This guide provides a step-by-step Excel VBA macro to automatically merge them, making your trade history usable for analysis in spreadsheets.




You know what's a pain that nobody talks about? That moment you right-click your MT4 account history, hit "Save as Detailed Statement," and think you're about to get a clean spreadsheet for your records. Then you open it, and it's a mess.

Each trade is spread across two rows. Your entry time, symbol, lots, and profit are on one line. Then the next line has your ticket number, magic number, and the trade comment, shifted over so it doesn't even line up with the columns above. It's like the platform is playing a practical joke on you.

I spent an entire afternoon once manually copy-pasting 400 trades for a client report. Never again. The internet is full of people complaining about this, but the common solutions are either "just buy an export script" or some complicated Python thing that most traders don't want to deal with.

I found a better way—something that takes about 5 minutes to set up and then works forever. It's an Excel VBA macro that automatically scoops up that second row of data and glues it to the end of the first row.

Here's how I set it up:

1. Prep the Data

Inside MT4, right-click on the "Account History" tab, select "Custom Period" to pick your date range, then right-click again and choose "Save as Detailed Statement." Save it as an HTML file. Open that HTML file in your browser, select all (Ctrl+A), and copy the data.

Paste it into a blank worksheet in Excel. You'll immediately see the problem: the data for each trade sits on two rows, with the second row offset to the right.

2. Build the Merge Tool

Now for the fix. Open a new blank Excel workbook. Go to File > Options > Customize Ribbon and make sure the "Developer" tab is checked on the right side.

Click the new Developer tab, then click Visual Basic (or just press Alt + F11). In the window that pops up, go to Insert > Module. A blank white space will appear.

Paste the following code into that space. I found this trick on a trader forum a while back, and it's been a lifesaver :

``vba
Sub MergeMT4Statement_Ultimate()
Dim ws As Worksheet
Dim LastRow As Long, LastCol As Long
Dim i As Long, j As Long, TargetCol As Long
Dim MergedCellWarning As Boolean

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
On Error GoTo ErrHandler

Set ws = ActiveSheet
MergedCellWarning = False

If ws.UsedRange.MergeCells Then
MsgBox "Warning: Merged cells detected. Consider unmerging before running.", vbExclamation
MergedCellWarning = True
End If

LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
LastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column

For i = LastRow To 2 Step -1
If Len(Trim(ws.Cells(i, 1).Value)) = 0 Then
TargetCol = ws.Cells(i - 1, ws.Columns.Count).End(xlToLeft).Column + 1

For j = 1 To LastCol
If Not IsEmpty(ws.Cells(i, j)) Then
ws.Cells(i - 1, TargetCol).Value = ws.Cells(i, j).Value
TargetCol = TargetCol + 1
End If
Next j
ws.Rows(i).Interior.Color = RGB(255, 255, 0)
End If
Next i

For i = LastRow To 2 Step -1
If ws.Rows(i).Interior.Color = RGB(255, 255, 0) Then
ws.Rows(i).Delete
End If
Next i

ws.UsedRange.Columns.AutoFit

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

MsgBox "MT4 statement merge complete! Processed " & LastRow & " rows.", vbInformation
Exit Sub

ErrHandler:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
MsgBox "Error on row " & i & ": " & Err.Description, vbCritical
End Sub
`

Close the VBA window. Save this workbook as an Excel Macro-Enabled Workbook (*.xlsm). Name it something like "MT4_Cleaner.xlsm".

3. Run the Script

Go back to your worksheet with the messy MT4 data. Press
Alt + F8, select "MergeMT4Statement_Ultimate," and click "Run."

The macro scans from the bottom up, grabs everything from the second row of each trade pair, and tacks it onto the end of the first row. It then deletes the now-empty second row. The result? One clean row per trade, with the ticket number and comment all in the same line where they belong, ready for pivot tables or whatever analysis you need.

One important quirk I've noticed: If your MT4 statement includes merged cells (some templates do), the macro will warn you. I've had better luck just selecting the pasted data and using "Unmerge Cells" before running it. It avoids weird errors that can crop up on the first pass.

Once you've got the tool set up, you don't need to rebuild it. Just keep that
MT4_Cleaner.xlsm` file handy for next time. Paste fresh statement data into it, hit the macro button, and your data is ready to go in about two seconds.

Reference: Misssoon. "MT4账户历史右键保存为详细户口结单分两行显示的解决方案." misssoon.com, 2025 .

本文首发于FXEAR.com,原创内容,未经授权禁止转载。