pgmtkl
asked on
converting minutes
I have a vb module that exports multiple queries to a formatted excel workbook. on one column i would like to convert the minutes in those cells to hh:mm. The number of rows/cells will always be different and I dont want to have to manually do formatting. How can i do this in the VB module??
ASKER
sorry im not familirar with that would that be in a dim statement?
No, that's a property assignment. It assumes you have the Excel object model referenced and that you have the range of cells defined in the Range variable TargetCells. More specifically:
Dim TargetCells As Range
Set TargetCells = oExcelApp.Workbooks("Book1 .xls").Wor ksheets("S heet1").Ra nge("A1:A1 00")
TargetCells.NumberFormat = "[h]:mm"
Kevin
Dim TargetCells As Range
Set TargetCells = oExcelApp.Workbooks("Book1
TargetCells.NumberFormat = "[h]:mm"
Kevin
ASKER
woudl this target the entire workbook and cells for a specific column? if i need all of column d on all sheets would i use something similara to below? I am not familrar with the formatting portions. I have 5 worksheets all would need the same for column d, just the row count will always be different.
Dim x
Dim TargetCells As Range
Set TargetCells = oExcelApp.Workbooks("Book1 .xls").Wor ksheets("S heet1").Ra nge("A1:A1 00")
TargetCells.NumberFormat = "[h]:mm"
With xlObj
For x = 1 To .worksheets.Count
.sheets(x).Activate
.Range("A1", .Range("A1").End(xlToRight ).End(xlDo wn)).Colum ns.AutoFit
.Range("A1", .Range("A1").End(xlToRight )).Font.Bo ld = True
.Range("A1", .Range("A1").End(xlToRight )).Horizon talAlignme nt = xlCenter
Dim x
Dim TargetCells As Range
Set TargetCells = oExcelApp.Workbooks("Book1
TargetCells.NumberFormat = "[h]:mm"
With xlObj
For x = 1 To .worksheets.Count
.sheets(x).Activate
.Range("A1", .Range("A1").End(xlToRight
.Range("A1", .Range("A1").End(xlToRight
.Range("A1", .Range("A1").End(xlToRight
Dim Worksheet As Worksheet
For Each Worksheet In xlObj.Workbooks("Book1.xls ").Workshe ets
Worksheet..Range("D2", .Range("D2").End(xlToRight ).End(xlDo wn)).Numbe rFormat = "[h]:mm"
Next Worksheet
Kevin
For Each Worksheet In xlObj.Workbooks("Book1.xls
Worksheet..Range("D2", .Range("D2").End(xlToRight
Next Worksheet
Kevin
ASKER
I get user defined type not found. Does it make a diff if i am running this from access?
Not if you have referenced the Excel object model. Also, I just noticed an error in the above code. Corrected:
Dim Worksheet As Worksheet
For Each Worksheet In xlObj.Workbooks("Book1.xls ").Workshe ets
Worksheet.Range("D2", .Range("D2").End(xlToRight ).End(xlDo wn)).Numbe rFormat = "[h]:mm"
Next Worksheet
You can also try this if you don't want to reference the Excel object model:
Dim Worksheet As Object
For Each Worksheet In xlObj.Workbooks("Book1.xls ").Workshe ets
Worksheet.Range("D2", .Range("D2").End(xlToRight ).End(xlDo wn)).Numbe rFormat = "[h]:mm"
Next Worksheet
Kevin
Dim Worksheet As Worksheet
For Each Worksheet In xlObj.Workbooks("Book1.xls
Worksheet.Range("D2", .Range("D2").End(xlToRight
Next Worksheet
You can also try this if you don't want to reference the Excel object model:
Dim Worksheet As Object
For Each Worksheet In xlObj.Workbooks("Book1.xls
Worksheet.Range("D2", .Range("D2").End(xlToRight
Next Worksheet
Kevin
ASKER
now i get invalid or unqualified reference? do i need to add something additional. thx for your help
ASKER
that unqualified reference is on .Range ??
Sorry...not thinking clearly :-(
Dim Worksheet As Object
For Each Worksheet In xlObj.Workbooks("Book1.xls ").Workshe ets
Worksheet.Range("D2", Worksheet.Range("D2").End( xlDown)).N umberForma t = "[h]:mm"
Next Worksheet
Kevin
Dim Worksheet As Object
For Each Worksheet In xlObj.Workbooks("Book1.xls
Worksheet.Range("D2", Worksheet.Range("D2").End(
Next Worksheet
Kevin
ASKER
ok, now i get it to run, but then i get '91 error. Object variable or With block variable not set on this For Each Worksheet In xlObj.Workbooks("Week.xls" ).Workshee ts
DOes somethng else need to be added?
DOes somethng else need to be added?
I was assuming you had defined and set xlObj to the Excel application object. Have you?
Kevin
Kevin
ASKER
I dont think i do, do i need to add this statement?
Set xlObj = CreateObject("Excel.Applic ation")
xlObj.Workbooks.Open "C:\Week.xls"
With xlObj
Set xlObj = CreateObject("Excel.Applic
xlObj.Workbooks.Open "C:\Week.xls"
With xlObj
Yes.
Kevin
Kevin
ASKER
I added the below and it runs and stops on worksheet.range line with error '1004' application defined error. Is something wrong with how i have it?
Dim Worksheet As Object
Set xlObj = CreateObject("Excel.Applic ation")
xlObj.Workbooks.Open "C:\Week.xls"
With xlObj
For Each Worksheet In xlObj.Workbooks("Week.xls" ).Workshee ts
Worksheet.Range("D2", Worksheet.Range("D2").End( xlDown)).N umberForma t = "[h]:mm"
Next Worksheet
End With
Dim Worksheet As Object
Set xlObj = CreateObject("Excel.Applic
xlObj.Workbooks.Open "C:\Week.xls"
With xlObj
For Each Worksheet In xlObj.Workbooks("Week.xls"
Worksheet.Range("D2", Worksheet.Range("D2").End(
Next Worksheet
End With
Try:
Dim xlObj As Object
Dim Workbook As Object
Dim Worksheet As Object
Set xlObj = CreateObject("Excel.Applic ation")
Set Workbook = xlObj.Workbooks.Open("C:\W eek.xls")
For Each Worksheet In Workbook.Worksheets
Worksheet.Range("D2", Worksheet.Range("D2").End( -4121)).Nu mberFormat = "[h]:mm"
Next Worksheet
Kevin
Dim xlObj As Object
Dim Workbook As Object
Dim Worksheet As Object
Set xlObj = CreateObject("Excel.Applic
Set Workbook = xlObj.Workbooks.Open("C:\W
For Each Worksheet In Workbook.Worksheets
Worksheet.Range("D2", Worksheet.Range("D2").End(
Next Worksheet
Kevin
ASKER
Duplicate declaration in current scope??
Probably xlObj. Remove mine or yours.
Kevin
Kevin
ASKER
When i remove one of the statements, it stops running says it can not access my week.xls workbook? DO i need to maybe rename on of the xlObj statements to xlObja?
At this point I don't know because I have no idea what the rest of code looks like. Can you post it?
Kevin
Kevin
ASKER
Here it is:
Sub exportExcel()
Dim rs As DAO.Recordset
Dim xlObj As Object, Sheet As Object
Dim xlFile As String
xlFile = "c:\MasterWk.xls"
Set xlObj = CreateObject("Excel.Applic ation")
xlObj.Workbooks.Open xlFile
xlObj.Visible = True
Set rs = CurrentDb.OpenRecordset("M on")
Set Sheet = xlObj.activeworkbook.Works heets("Mon ")
Sheet.Range("A5").CopyFrom Recordset rs
Set rs = CurrentDb.OpenRecordset("T ue")
Set Sheet = xlObj.activeworkbook.Works heets("Tue ")
Sheet.Range("A5").CopyFrom Recordset rs
Set rs = CurrentDb.OpenRecordset("W ed")
Set Sheet = xlObj.activeworkbook.Works heets("Wed ")
Sheet.Range("A5").CopyFrom Recordset rs
Set rs = CurrentDb.OpenRecordset("T hu")
Set Sheet = xlObj.activeworkbook.Works heets("Thu ")
Sheet.Range("A5").CopyFrom Recordset rs
Set rs = CurrentDb.OpenRecordset("F ri")
Set Sheet = xlObj.activeworkbook.Works heets("Fri ")
Sheet.Range("A5").CopyFrom Recordset rs
'save the excel file
xlObj.activeworkbook.savea s "C:\Week.xls"
Set Sheet = Nothing
xlObj.Quit
Set xlObj = Nothing
Dim xlObj As Object
'Dim xlObj As Object
Dim Workbook As Object
Dim Worksheet As Object
Set xlObja = CreateObject("Excel.Applic ation")
Set Workbook = xlObja.Workbooks.Open("C:\ Week.xls")
For Each Worksheet In Workbook.Worksheets
Worksheet.Range("D2", Worksheet.Range("D2").End( -4121)).Nu mberFormat = "[h]:mm"
Next Worksheet
End
'save the excel file
xlObja.activeworkbook.save as "C:\Week.xls"
Set Sheet = Nothing
xlObja.Quit
Set xlObj = Nothing
End Sub
Sub exportExcel()
Dim rs As DAO.Recordset
Dim xlObj As Object, Sheet As Object
Dim xlFile As String
xlFile = "c:\MasterWk.xls"
Set xlObj = CreateObject("Excel.Applic
xlObj.Workbooks.Open xlFile
xlObj.Visible = True
Set rs = CurrentDb.OpenRecordset("M
Set Sheet = xlObj.activeworkbook.Works
Sheet.Range("A5").CopyFrom
Set rs = CurrentDb.OpenRecordset("T
Set Sheet = xlObj.activeworkbook.Works
Sheet.Range("A5").CopyFrom
Set rs = CurrentDb.OpenRecordset("W
Set Sheet = xlObj.activeworkbook.Works
Sheet.Range("A5").CopyFrom
Set rs = CurrentDb.OpenRecordset("T
Set Sheet = xlObj.activeworkbook.Works
Sheet.Range("A5").CopyFrom
Set rs = CurrentDb.OpenRecordset("F
Set Sheet = xlObj.activeworkbook.Works
Sheet.Range("A5").CopyFrom
'save the excel file
xlObj.activeworkbook.savea
Set Sheet = Nothing
xlObj.Quit
Set xlObj = Nothing
Dim xlObj As Object
'Dim xlObj As Object
Dim Workbook As Object
Dim Worksheet As Object
Set xlObja = CreateObject("Excel.Applic
Set Workbook = xlObja.Workbooks.Open("C:\
For Each Worksheet In Workbook.Worksheets
Worksheet.Range("D2", Worksheet.Range("D2").End(
Next Worksheet
End
'save the excel file
xlObja.activeworkbook.save
Set Sheet = Nothing
xlObja.Quit
Set xlObj = Nothing
End Sub
Try this:
Sub exportExcel()
Dim rs As DAO.Recordset
Dim xlObj As Object, Sheet As Object
Dim xlFile As String
xlFile = "c:\MasterWk.xls"
Set xlObj = CreateObject("Excel.Applic ation")
xlObj.Workbooks.Open xlFile
xlObj.Visible = True
Set rs = CurrentDb.OpenRecordset("M on")
Set Sheet = xlObj.ActiveWorkbook.Works heets("Mon ")
Sheet.Range("A5").CopyFrom Recordset rs
Set rs = CurrentDb.OpenRecordset("T ue")
Set Sheet = xlObj.ActiveWorkbook.Works heets("Tue ")
Sheet.Range("A5").CopyFrom Recordset rs
Set rs = CurrentDb.OpenRecordset("W ed")
Set Sheet = xlObj.ActiveWorkbook.Works heets("Wed ")
Sheet.Range("A5").CopyFrom Recordset rs
Set rs = CurrentDb.OpenRecordset("T hu")
Set Sheet = xlObj.ActiveWorkbook.Works heets("Thu ")
Sheet.Range("A5").CopyFrom Recordset rs
Set rs = CurrentDb.OpenRecordset("F ri")
Set Sheet = xlObj.ActiveWorkbook.Works heets("Fri ")
Sheet.Range("A5").CopyFrom Recordset rs
For Each Worksheet In xlObj.ActiveWorkbook.Works heets
Worksheet.Range("D2", Worksheet.Range("D2").End( -4121)).Nu mberFormat = "[h]:mm"
Next Worksheet
'save the excel file
xlObj.ActiveWorkbook.SaveA s "C:\Week.xls"
Set Sheet = Nothing
xlObj.Quit
Set xlObj = Nothing
End Sub
Kevin
Sub exportExcel()
Dim rs As DAO.Recordset
Dim xlObj As Object, Sheet As Object
Dim xlFile As String
xlFile = "c:\MasterWk.xls"
Set xlObj = CreateObject("Excel.Applic
xlObj.Workbooks.Open xlFile
xlObj.Visible = True
Set rs = CurrentDb.OpenRecordset("M
Set Sheet = xlObj.ActiveWorkbook.Works
Sheet.Range("A5").CopyFrom
Set rs = CurrentDb.OpenRecordset("T
Set Sheet = xlObj.ActiveWorkbook.Works
Sheet.Range("A5").CopyFrom
Set rs = CurrentDb.OpenRecordset("W
Set Sheet = xlObj.ActiveWorkbook.Works
Sheet.Range("A5").CopyFrom
Set rs = CurrentDb.OpenRecordset("T
Set Sheet = xlObj.ActiveWorkbook.Works
Sheet.Range("A5").CopyFrom
Set rs = CurrentDb.OpenRecordset("F
Set Sheet = xlObj.ActiveWorkbook.Works
Sheet.Range("A5").CopyFrom
For Each Worksheet In xlObj.ActiveWorkbook.Works
Worksheet.Range("D2", Worksheet.Range("D2").End(
Next Worksheet
'save the excel file
xlObj.ActiveWorkbook.SaveA
Set Sheet = Nothing
xlObj.Quit
Set xlObj = Nothing
End Sub
Kevin
ASKER
well that works really good. but now when i go to the workbook the cell that used to have 120 that should display as 02:00 as 1440:00. Do i need to add some additional formats?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
WOrks perfect! Thanks for all of your help.
TargetCells.NumberFormat = "[h]:mm"
Kevin