I want to choose the file and pull data using CSV or txt format and implement the below functionality. But I don't know how.
Sub EditData2()
Dim wb As Workbook
Dim ws As Worksheet
Dim nws As Worksheet
Dim LastRow As Long
Dim RangeToFilter As Range
Dim DestinationRange As Range
Dim x As Integer
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Records")
LastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
With ws
.Range("P1") = "A_Login"
.Range("Q1") = "B_custom_field: Gender"
.Range("R1") = "D_Email"
End With
' existing coloumn
Set RangeToFilter = ws.Range("A1:R" & LastRow)
'add MY
For i = 2 To LastRow
ws.Cells(i, 16) = "MY" & ws.Cells(i, 1)
'add @xyz.com in email
If ws.Cells(i, 13) = "" Then
ws.Cells(i, 18) = ws.Cells(i, 16) & "@xyz.com"
Else
ws.Cells(i, 18) = ws.Cells(i, 13)
End If
'change gender title
If ws.Cells(i, 2) = "Cik" Then
ws.Cells(i, 17) = "Female"
ElseIf ws.Cells(i, 2) = "Puan" Then
ws.Cells(i, 17) = "Female"
Else: ws.Cells(i, 17) = "Male"
End If
Next i
CriteriaWildCard = Format(Date, "m/dd/yyyy")
ws.Range("T:AL").Clear
' filter inpat/expat and hypen
' filter for date after today and status
With RangeToFilter
.AutoFilter Field:=10, Criteria1:=">=" & CriteriaWildCard, Operator:=xlOr, _
Criteria2:="=-"
.AutoFilter Field:=9, Criteria1:="=Active"
'.AutoFilter Field:=8, Criteria1:="=Inactive"
.AutoFilter Field:=14, Criteria1:="=Inpat"
End With
' copy only visible cells
With ws
.Range("T1") = "G_custom_field: Employee ID"
.Range("U1") = "P_Title"
.Range("V1") = "B_Firstname"
.Range("W1") = "C_Lastname"
.Range("X1") = "I_custom_field: Position"
.Range("Y1") = "L_custom_field: Category"
.Range("Z1") = "K_custom_field: Department"
.Range("AA1") = "O_Location"
.Range("AB1") = "F_Active"
.Range("AC1") = "N_custom_field: Last day of work"
.Range("AD1") = "J_custom_field: Date Joined"
.Range("AE1") = "M_custom_field: Line Manager"
.Range("AF1") = "Q_Business_Email_Address"
.Range("AG1") = "R_Expat/Inpat"
.Range("AH1") = "S_Job_Description"
.Range("AI1") = "A_Login"
.Range("AJ1") = "H_custom_field: Gender"
.Range("AK1") = "D_Email"
.Range("AL1") = "E_User-type"
End With
' new existing coloumn
Set DestinationRange = ws.Range("T2:AL2")
ws.Range("A2", ws.Range("A2").End(xlToRight).End(xlDown)).SpecialCells(xlCellTypeVisible) _
.Copy Destination:=DestinationRange
' clear filter
RangeToFilter.AutoFilter
ws.Range("P:R").Clear
Set nws = wb.Worksheets.Add
' set user-type
Dim LRow3 As Long
LRow3 = ws.Cells(Rows.Count, 20).End(xlUp).Row
For e = 2 To LRow3
ws.Cells(e, 38) = ws.Cells(e, 20)
Select Case ws.Cells(e, 20)
Case "00133"
ws.Cells(e, 38) = "SuperAdmin"
Case "00012"
ws.Cells(e, 38) = "Instructor"
Case Else
ws.Cells(e, 38) = "Learner"
End Select
Next e
'rearrange column
Dim arry As Variant
arry = Array("A_Login", "B_Firstname", "C_Lastname", "D_Email", "E_User-Type", "F_Active", _
"G_custom_field: Employee ID", "H_custom_field: Gender", "I_custom_field: Position", "J_custom_field: Date Joined", _
"K_custom_field: Department", "L_custom_field: Category", "M_custom_field: Line Manager", _
"N_custom_field: Last day of work", "O_Location", "P_Title", "Q_Business_Email_Address", _
"R_Expat/Inpat", "S_Job_Description")
For j = 0 To UBound(arry)
Dim ColNo As Long
ColNo = Application.WorksheetFunction.Match(arry(j), ws.Range("A1:AL1"), 0)
ws.Columns(ColNo).Copy Destination:=nws.Columns(j + 1)
Next j
nws.Range("O:S").Clear
Dim nwsLastRow As Long
nwsLastRow = nws.Cells(Rows.Count, 1).End(xlUp).Row
nwsLastColumn = nws.Cells(1, Columns.Count).End(xlToLeft).Column
'change Active status
For k = 2 To nwsLastRow
If nws.Cells(k, 6) = "Active" Then
nws.Cells(k, 6) = "Yes"
ElseIf nws.Cells(k, 6) = "Inactive" Then
nws.Cells(k, 6) = "Yes"
Else: nws.Cells(k, 6) = "No"
End If
If nws.Cells(k, 14) = "-" Then
nws.Cells(k, 14) = ""
Else: nws.Cells(k, 14) = nws.Cells(k, 14)
End If
'set date format
nws.Cells(k, 10) = Format(nws.Cells(k, 10), "dd/mm/yyyy")
If nws.Cells(k, 14) <> "" Then
nws.Cells(k, 14) = Format(nws.Cells(k, 14), "dd/mm/yyyy")
Else: nws.Cells(k, 14) = ""
End If
Next k
For m = 1 To nwsLastColumn
nws.Cells(1, m) = Right(nws.Cells(1, m), Len(nws.Cells(1, m)) - 2)
Next m
'set font size and autofit all column
With nws
.Cells.Font.Size = 9
.Columns.AutoFit
End With
ws.Range("T:AL").Clear
'add newsheet name
nws.Name = "Filter_" & Format(Date, "yyyymmdd") & wb.Worksheets.Count + 1
End Sub