Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 90218

How to automatically update formatting in Excel using VBA

$
0
0

I have found multiple similar questions on the internet but I believe that each one is specific so here is my problem.

I am transfering data from my Access DB to my Excel sheet. The data that I am transferring is mainly contains mainly Time data. Because my Access columns are set to Date/Time - Long Time format, when I transfer the data to Excel, the values shown contain date as well of this kind: 00/01/1900 12:03:10.

The thing is I don't want thee default date to appear as my column only contains hours. So in my VBA code, I am formatting the range automaticlly to hh:mm:ss.

The thing is this formatting works fine when I am debuggin (F8) and I can see my column directly in the correct format. But when I execute the code outright, the formatting does not apply. Here is the code:

Private Sub find_Click()
Dim conn As Object
Dim rs2 As Object
Dim strconn As String
Dim qry2 As String
Dim var2

Set conn = CreateObject("ADODB.connection")
Set rs2 = CreateObject("ADODB.Recordset")

strconn = "Provider=Microsoft.ACE.OLEDB.12.0;"& "Data source = MyPath"

qry2 = "select * from taleA where mat="& Val(Me.mat) & ";"

Application.ScreenUpdating = False
ThisWorkbook.Worksheets("GTT").Range("A2:K10000").ClearContents

conn.Open (strconn)
rs2.Open qry2, conn

If rs2.EOF And rs2.BOF Then
   rs2.Close
   conn.Close
   Set rs2 = Nothing
   Set conn = Nothing
   Application.ScreenUpdating = True

   MsgBox "No result", vbOKOnly + vbInformation
   Exit Sub
End If

ThisWorkbook.Worksheets("GTT").Range("a2").CopyFromRecordset rs2
Sheets(4).Activate
ActiveSheet.Columns("D:I").Select
Selection.NumberFormat = "hh:mm:ss;@"
ActiveWorkbook.Save
Sheets(1).Activate

rs2.Close
conn.Close
Set rs2 = Nothing
Set conn = Nothing

Application.ScreenUpdating = True
Me.ListBox1.RowSource = "outputSource2"


End Sub

Thank You


Viewing all articles
Browse latest Browse all 90218

Trending Articles



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