Hi,
I am trying to export and then format a series of queries into worksheets in excel.
I have managed so far to get this to work and I have managed to achieve some formatting but I don't really understand why it is doing what it is doing and some things that I think should work, don't!!
Here is a selection of the code that I have working, I have managed so far to change the worksheet names, autofit the columns, and apply some font changes:
Set objXL = CreateObject("Excel.Application")
objXL.Visible = True
Set objWB = objXL.WorkBooks.Open(strPathFile)
objXL.UserControl = True
Set xlWB = objXL.WorkBooks.Open(strPathFile)
Dim intCountofSheets, intCurrentSheet As Integer
intCountofSheets = xlWB.Sheets.Count
intCurrentSheet = 1
xlWB.WorkSheets(1).Activate
objXL.ActiveSheet.Name = "Income"
xlWB.WorkSheets(2).Activate
objXL.ActiveSheet.Name = "Expenditure"
xlWB.WorkSheets(3).Activate
objXL.ActiveSheet.Name = "Recharges"
xlWB.WorkSheets(4).Activate
objXL.ActiveSheet.Name = "Interest & Tax"
Do While intCurrentSheet <= intCountofSheets
xlWB.WorkSheets(intCurrentSheet).Activate
objXL.ActiveSheet.Cells.Select
With objXL.Selection.Font
.Name = "Tahoma"
.Size = 10
End With
With objXL.ActiveSheet
.Columns.Select
.Columns.EntireColumn.Autofit
.Range("A1").Select 'Selects first cell to deselect the rest of the sheet.
End With
intCurrentSheet = intCurrentSheet + 1
Loop
xlWB.WorkSheets(1).Activate 'Returns the selection to the first worksheet.
xlWB.Save
Set xlWB = Nothing
Set objXL = Nothing
As I said I don't really understand, just lots of reading and trying to adjust example codes and other helpful sources. Could someone please explain a few basics for me?
In particular: Why does some of my code work with "with" and using "objXL." as a precursor and other parts work as just .column.select or .name for example?
With that in mind how do you specifically navigate to certain cells or columns?
Having got this far I would like to improve my programming so that it is a bit less trial and error and a bit neater!!
Thanks in advance!!
I am trying to export and then format a series of queries into worksheets in excel.
I have managed so far to get this to work and I have managed to achieve some formatting but I don't really understand why it is doing what it is doing and some things that I think should work, don't!!
Here is a selection of the code that I have working, I have managed so far to change the worksheet names, autofit the columns, and apply some font changes:
Set objXL = CreateObject("Excel.Application")
objXL.Visible = True
Set objWB = objXL.WorkBooks.Open(strPathFile)
objXL.UserControl = True
Set xlWB = objXL.WorkBooks.Open(strPathFile)
Dim intCountofSheets, intCurrentSheet As Integer
intCountofSheets = xlWB.Sheets.Count
intCurrentSheet = 1
xlWB.WorkSheets(1).Activate
objXL.ActiveSheet.Name = "Income"
xlWB.WorkSheets(2).Activate
objXL.ActiveSheet.Name = "Expenditure"
xlWB.WorkSheets(3).Activate
objXL.ActiveSheet.Name = "Recharges"
xlWB.WorkSheets(4).Activate
objXL.ActiveSheet.Name = "Interest & Tax"
Do While intCurrentSheet <= intCountofSheets
xlWB.WorkSheets(intCurrentSheet).Activate
objXL.ActiveSheet.Cells.Select
With objXL.Selection.Font
.Name = "Tahoma"
.Size = 10
End With
With objXL.ActiveSheet
.Columns.Select
.Columns.EntireColumn.Autofit
.Range("A1").Select 'Selects first cell to deselect the rest of the sheet.
End With
intCurrentSheet = intCurrentSheet + 1
Loop
xlWB.WorkSheets(1).Activate 'Returns the selection to the first worksheet.
xlWB.Save
Set xlWB = Nothing
Set objXL = Nothing
As I said I don't really understand, just lots of reading and trying to adjust example codes and other helpful sources. Could someone please explain a few basics for me?
In particular: Why does some of my code work with "with" and using "objXL." as a precursor and other parts work as just .column.select or .name for example?
With that in mind how do you specifically navigate to certain cells or columns?
Having got this far I would like to improve my programming so that it is a bit less trial and error and a bit neater!!
Thanks in advance!!