Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50151

Date Formatting

$
0
0
Hi everyone,

I'm trying to adjust the following code so that column B in the "Export" worksheet has a date format of "yyyy-mm-dd" and has a text string "TTTTT" attached to it. However whenever I have a date format, it just reverts to the mm/dd/yyyy format.

So for example, when the date value (in "mm/dd/yyyy" format) in the Import worksheet gets processed and placed in the Export worksheet, it should be yyyy-mm-ddTTTTT

Can someone assist me with this? I tried just starting to convert it to a "yyyy-mm-dd" format using the last line but it doesn't work. I think the issue might be that whenever I paste in data to the import worksheet, it is a text format or something different.

Code:

Option Explicit

Sub copyData()
Dim wsI As Worksheet, wsAT As Worksheet, wsEx As Worksheet
Dim matchRng As Variant
Dim k As Long, adjV As Double
Dim resRng As Range, rs As Range
Set wsI = Worksheets("Import")
Set wsAT = Worksheets("Adjustment Table")
Set wsEx = Worksheets("Export")
wsEx.Range("A2").Resize(wsEx.Cells(Rows.Count, "A").End(xlUp).Row, 5).Clear
matchRng = Application.Index(Application.Transpose(wsAT.Range("A4:B" & wsAT.Cells(Rows.Count, "B").End(xlUp).Row)), 0, 0)
For k = LBound(matchRng, 2) To UBound(matchRng, 2)
    Set resRng = Find_Range(matchRng(1, k), wsI.Columns("D"), xlValues, xlWhole)
    If Not resRng Is Nothing Then
        For Each rs In resRng
            wsI.Range("A" & rs.Row).Resize(, 5).Copy wsEx.Range("A" & wsEx.Cells(Rows.Count, "A").End(xlUp).Row + 1)
            adjV = wsEx.Range("E" & wsEx.Cells(Rows.Count, "E").End(xlUp).Row)
            adjV = adjV + (adjV * matchRng(2, k))
            wsEx.Range("E" & wsEx.Cells(Rows.Count, "E").End(xlUp).Row) = adjV
        Next
    End If
Next
Call sortData(wsEx)
End Sub

Sub sortData(ws As Worksheet)
Dim lrow As Long
    ws.Select
    lrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
    ws.Range("A1:E" & lrow).Select
    ws.Sort.SortFields.Clear
    ws.Sort.SortFields.Add Key:=Range("D2:D" & lrow) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ws.Sort.SortFields.Add Key:=Range("C2:C" & lrow) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ws.Sort.SortFields.Add Key:=Range("B2:B" & lrow) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ws.Sort
        .SetRange Range("A1:E" & lrow)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    With Worksheets("Export")
    .Range("a:a").NumberFormat = "000#"
    .Range("e:e").NumberFormat = "0"
    .Range("B:B").NumberFormat = "yyyy-mm-dd"   
End With
End Sub


Viewing all articles
Browse latest Browse all 50151

Trending Articles