Giúp công thức xoá hàng và cột file csv trong vba (1 người xem)

Liên hệ QC

Người dùng đang xem chủ đề này

nguyenanhdung8111982

Thành viên hoạt động
Tham gia
1/11/19
Bài viết
120
Được thích
33
Giới tính
Nam
Hình 1 là hình gốc muốn xoá từ dòng A1 đến AE48.
Xóa cột G49 đến Z49
Giữ lại A49 đến F49. Sau đó repleace bỏ chữ Z và T:
2019-08-29T02:28:44Z->2019-08-29 02:28:44
khi chạy code dưới dồn về một ô. hình 3, kết quả mong muốn hình 2. Mong cả nhà giúp đỡ

Mã:
Sub ProcessMultipleFiles()
Dim FolderPath As String, FilePath As String, NewFileName As String
    Dim FSO As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = True
        .Filters.Clear
        .Filters.Add "CSV (MS-DOS)", "*.csv"
        If .Show Then
            FolderPath = FSO.GetParentFolderName(.SelectedItems(1)) & "\"
            For i = 1 To .SelectedItems.Count
                FilePath = .SelectedItems(i)
                NewFileName = FSO.GetBaseName(FilePath)
                NewFileName = Left(NewFileName, Len(NewFileName) - 4) & "_N.csv"
                FSO.CopyFile FilePath, FolderPath & NewFileName, True
                CSVAmend2 FolderPath, NewFileName
            Next
        End If
    MsgBox "Ho" & ChrW(224) & "n Th" & ChrW(224) & "nh !!!"
    End With
   End Sub
       
Sub CSVAmend2(FolderPath As String, FileName As String)
    Dim wb As Workbook, ws As Worksheet, rng As Range
    Set wb = Workbooks.Open(FolderPath & FileName)
    Set ws = wb.Sheets(1)
    Application.DisplayAlerts = False
    Columns("F").Replace What:="T", Replacement:=" "
    Columns("F").Replace What:="Z", Replacement:=""
    wb.Worksheets(1).Columns("f:f").NumberFormat = "yyyy-mm-dd hh:mm:ss"
    ws.Range("A1:AE48").SpecialCells(xlCellTypeVisible).Delete
    ws.Range("G49:Z49").EntireColumn.Delete
    Application.DisplayAlerts = True
    wb.Close SaveChanges:=True
End Sub
 

File đính kèm

  • 1.jpg
    1.jpg
    78.9 KB · Đọc: 17
  • 2.jpg
    2.jpg
    340.5 KB · Đọc: 15
  • 3.jpg
    3.jpg
    204.6 KB · Đọc: 14
Lần chỉnh sửa cuối:
Hình 1 là hình gốc muốn xoá từ dòng A1 đến AE48.
Xóa cột G49 đến Z49
Giữ lại A49 đến F49. Sau đó repleace bỏ chữ Z và T:
2019-08-29T02:28:44Z->2019-08-29 02:28:44
khi chạy code dưới dồn về một ô. hình 3, kết quả mong muốn hình 2. Mong cả nhà giúp đỡ

Mã:
Sub ProcessMultipleFiles()
Dim FolderPath As String, FilePath As String, NewFileName As String
    Dim FSO As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = True
        .Filters.Clear
        .Filters.Add "CSV (MS-DOS)", "*.csv"
        If .Show Then
            FolderPath = FSO.GetParentFolderName(.SelectedItems(1)) & "\"
            For i = 1 To .SelectedItems.Count
                FilePath = .SelectedItems(i)
                NewFileName = FSO.GetBaseName(FilePath)
                NewFileName = Left(NewFileName, Len(NewFileName) - 4) & "_N.csv"
                FSO.CopyFile FilePath, FolderPath & NewFileName, True
                CSVAmend2 FolderPath, NewFileName
            Next
        End If
    MsgBox "Ho" & ChrW(224) & "n Th" & ChrW(224) & "nh !!!"
    End With
   End Sub
      
Sub CSVAmend2(FolderPath As String, FileName As String)
    Dim wb As Workbook, ws As Worksheet, rng As Range
    Set wb = Workbooks.Open(FolderPath & FileName)
    Set ws = wb.Sheets(1)
    Application.DisplayAlerts = False
    Columns("F").Replace What:="T", Replacement:=" "
    Columns("F").Replace What:="Z", Replacement:=""
    wb.Worksheets(1).Columns("f:f").NumberFormat = "yyyy-mm-dd hh:mm:ss"
    ws.Range("A1:AE48").SpecialCells(xlCellTypeVisible).Delete
    ws.Range("G49:Z49").EntireColumn.Delete
    Application.DisplayAlerts = True
    wb.Close SaveChanges:=True
End Sub
Ở sub ProcessMultipleFiles()
Bỏ dòng
CSVAmend2 FolderPath, NewFileName
xem thử nó ra cái gì?
 
Web KT

Bài viết mới nhất

Back
Top Bottom