Consulting

Results 1 to 6 of 6

Thread: VBA TO Correct Date format

  1. #1
    Banned VBAX Contributor
    Joined
    Aug 2017
    Posts
    144
    Location

    Thumbs up VBA TO Correct Date format

    Hi Team,
    I am new in VBA, please assist me in below code ,
    I want to put LR (last Row) in colulmn array .
    One Single LR for all columns and where to add that line in looping.
    Also Can someone expalin me the line what it indicates : - Worksheets(SheetNames(X)).Columns(Cols(X)).Replace " *", "", xlPart

    Sub Correct_DateFormat_ineach_sheets_Column()
    Dim wbk As Workbook
    Dim lr As Long
    Set wbk = Workbooks.Open(Sheets("sheet1").Range("b5").Value)

    lr = Cells(Rows.Count, 1).End(xlUp).Row

    Dim X As Long, SheetNames As Variant, Cols As Variant
    SheetNames = Array("Sheet1", "Sheet2", "Sheet4", "Sheet5")
    Cols = Array("J2:J" & lr, "L2:L" & lr, "P2:P" & lr, "R2:R" & lr)
    For X = LBound(SheetNames) To UBound(SheetNames)
    Worksheets(SheetNames(X)).Columns(Cols(X)).Replace " *", "", xlPart
    Worksheets(SheetNames(X)).Columns(Cols(X)).NumberFormat = "yyyy-mm-dd"
    Next
    End Sub

    Thanks in Advance for your Precious time !!

    Regards,
    Mallesh

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you may be a vba newbie, but definitely not a vbaexpress newbie.
    please use code tags when posting your code.

    for replace and date format, you can use whole column.

    Sub vbax_61213_correct_cate_format_sheets_cols()
    
        Dim wbk As Workbook
        Dim x As Long
        Dim SheetNames As Variant, Cols As Variant
        
        Set wbk = Workbooks.Open(Sheets("Sheet1").Range("B5").Value)
        
        SheetNames = Array("Sheet1", "Sheet2", "Sheet4", "Sheet5")
        Cols = Array("J:J", "L:L", "P:P", "R:R")
        
        With ActiveWorkbook
            For x = LBound(SheetNames) To UBound(SheetNames)
                .Worksheets(SheetNames(x)).Columns(Cols(x)).Replace " *", "", xlPart
                .Worksheets(SheetNames(x)).Columns(Cols(x)).NumberFormat = "yyyy-mm-dd"
            Next
        End With
        
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    .Replace " *", "", xlPart
    partially replaces the stings which start with a space in cells' values with null string, ie removes them.

    "ssss" becomes "ssss" (doe not change)
    "ddd ff" becomes "dd"
    "12 345" becomes "12"
    " qwerty" becomes "" (first character is a space)
    "qwerty " becomes "qwerty" (last character is a space)
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    Banned VBAX Contributor
    Joined
    Aug 2017
    Posts
    144
    Location
    Hi Mancubus,

    Thanks for you quick reply and your information on .Replace " *", "", xlPart

    However, I am still facing an issue, Subscript out of Range , for below line,

    I would like to mention I have header in each column. I want the date format till lastrow.

    .Worksheets(SheetNames(x)).Columns(Cols(x)).Replace " *", "", xlPart
    .Worksheets(SheetNames(x)).Columns(Cols(x)).NumberFormat = "yyyy-mm-dd"

    Please can someone assist.

    Thanks in Advance
    Mallesh

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    post your workbook pls
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  6. #6
    Banned VBAX Contributor
    Joined
    Aug 2017
    Posts
    144
    Location
    Hi, Mancubus

    The above code worked for me , When I put sheetName in array. Thanks a log !

    Regards,
    Mallesh

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •