ms access vba code to change data type in a column in an excel file

Posted on 2013-10-13
Medium Priority
Last Modified: 2013-10-13
I am using MS Access VBA and want to use vba coding to change the data type value in column H  in an Excel file (called "Next Delivery Date") to date.  Not sure how to do this.  Could use expert help.  I can rename the certain columns as seen below.  But do not know how to change the datatype (format cells) in column H to date type.

Sub Excel_Rename_Columns()

Dim xlObj As Excel.Application
Dim xlPath As String, shtName As String
Dim filArr(), j
Dim i As Integer

DoCmd.SetWarnings False

filArr = Array("Stock_Status_Report.xls")
xlPath = "C:\Users\sanjayg\Desktop"
Set xlObj = CreateObject("Excel.Application")

For j = LBound(filArr) To UBound(filArr)
    xlObj.Workbooks.Open xlPath & "\" & filArr(j)
    shtName = xlObj.Worksheets(1).Name
    For i = 1 To xlObj.Worksheets(shtName).UsedRange.Columns.Count
        If xlObj.Worksheets(shtName).Cells(1, i).Value = "Description" Then
            xlObj.Worksheets(shtName).Cells(1, i).Value = "Desc"
        ElseIf xlObj.Worksheets(shtName).Cells(1, i).Value = "Annual Dep. Usage" Then
            xlObj.Worksheets(shtName).Cells(1, i).Value = "Annual Dep Usage"
        ElseIf xlObj.Worksheets(shtName).Cells(1, i).Value = "Annual Indep. Usage" Then
             xlObj.Worksheets(shtName).Cells(1, i).Value = "Annual Indep Usage"
        End If

Set xlObj = Nothing

DoCmd.SetWarnings True

End Sub
Question by:sxxgupta
  • 2
  • 2
LVL 74

Accepted Solution

Jeffrey Coachman earned 2000 total points
ID: 39569337
I think...?:

    xlObj.Worksheets(shtName).Selection.NumberFormat = "m/d/yyyy"


    xlObj.Worksheets(shtName).Columns("H:H").NumberFormat = "m/d/yyyy"


Author Comment

ID: 39569348
The second part worked.  Thanks Jeff

Author Closing Comment

ID: 39569353
The second part worked.  First part gave an object does not support this method error.
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 39569375

Lucky guess on my part...

Glad it helped

Enjoy the weekend!



Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
Audit trails are very important in any system to hold people responsible for certain transactions and hold them to take ownership of their actions. This article is dedicated to all novice "Microsoft Access" developers.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question