Quantcast
Channel: Excel IT Pro Discussions forum
Viewing all articles
Browse latest Browse all 11829

Excel VBA across Workbooks

$
0
0

I've created a workbook (wbCode) with the following code in it to convert a yyyymmdd hh:mm:ss.000 date format into mm/dd/yyyy format.  This code works perfectly when I copy and paste a worksheet from a (wbNoCode) workbook into wbCode, however I want these subs to run on workbooks without having to copy and past the worksheets.  I've created a Quick Access Bar icon that points to wbCode:procDateConvert sub which when run in wbNoCode Workbooks, launches wbCode and attempts to run but it doesn't seem to highlight the columns correctly.  Is there some tweak that I need to be aware of when running code remote to a workbook?

Thanks in advance.

Sub procDateConvert()
    Dim Lastrow As Integer
    Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

    Range("D2:D" & Lastrow).Select
    Call ConvertYYYYMMDDToDate

    Range("E2:E" & Lastrow).Select
    Call ConvertYYYYMMDDToDate

    Range("G2:G" & Lastrow).Select
    Call ConvertYYYYMMDDToDateTime

End Sub

Sub ConvertYYYYMMDDToDate()
   Dim c As Range
   For Each c In Selection.Cells
       c.Value = DateSerial(Left(c.Value, 4), Mid(c.Value, 6, 2), Mid(c.Value, 9, 2))
       'Following line added only to enforce the format.
       c.NumberFormat = "mm/dd/yyyy"
   Next
End Sub

Sub ConvertYYYYMMDDToDateTime()
    Dim c As Range
    Dim strDate As String
    Dim strTime As String
   For Each c In Selection.Cells
       strDate = DateSerial(Left(c.Value, 4), Mid(c.Value, 6, 2), Mid(c.Value, 9, 2))
        strTime = TimeSerial(Mid(c.Value, 12, 2), Mid(c.Value, 15, 2), Mid(c.Value, 18, 2))
       c.Value = strDate & " " & strTime
       'Following line added only to enforce the format.
       c.NumberFormat = "mm/dd/yyyy hh:mm:ss"
   Next
End Sub

I'll nip this question in the bud, reason being want to get this data in to Access and Access will not import dates that are not in mm/dd/yyyy format or at least I can't get it to.

sample unmodified dates:


Viewing all articles
Browse latest Browse all 11829

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>