Application.
ScreenUpdating = False
Application.DisplayAlerts = False
For Each ws In wb.Worksheets
With ws
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
' Tìm vị trí các cột
varCol = 0: codeCol = 0: currCol = 0
Dim c As Long
For c = 1 To lastCol
Select Case UCase(Trim(.Cells(1, c).Value))
Case "CODE": codeCol = c
Case "CURRENCY": currCol = c
End Select
If UCase(Left(.Cells(1, c).Value, 3)) = "VAR" Then varCol = c
Next c
If varCol = 0 Or codeCol = 0 Or currCol = 0 Then GoTo NextSheet
' Xóa dòng có giá trị lỗi trong cột Variable
For r = lastRow To 2 Step -1
v = UCase(Trim(.Cells(r, varCol).Value & ""))
If v = "" _
Or InStr(v, "NA") > 0 _
Or InStr(v, "INVALID CODE OR EXPRESSION ENTERED") > 0 _
Or InStr(v, "NO DATA AVAILABLE") > 0 _
Or InStr(v, "NO WORLDSCOPE DATA FOR THIS CODE") > 0 _
Or InStr(v, "$$ER: E100") > 0 Then
.Rows(r).Delete
End If
Next r
' Đổi tên cột CURRENCY → ID
.Cells(1, currCol).Value = "ID"
' Tạo ID từ CODE (lấy phần trước dấu ngoặc)
lastRow = .Cells(.Rows.Count, codeCol).End(xlUp).Row
For r = 2 To lastRow
v = .Cells(r, codeCol).Value & ""
pos = InStr(v, "(")
If pos > 0 Then
.Cells(r, currCol).Value = Left(v, pos - 1)
Else
.Cells(r, currCol).Value = v
End If
Next r
End With