Solved

Format excel when exporting from access

Posted on 2014-03-16
6
1,122 Views
Last Modified: 2014-03-18
I have a code to highlight a filed. below is the code.
I would like to enable filter, freeze the first row and autofit  when exporting.
How to accomplish this?

Your help appreciated.


Private Sub Command48_Click()
Dim outputFileName As String
If Me.Dirty Then Me.Dirty = False
outputFileName = "Export_" & Format(Date, "dd-MM-yyyy") & ".xls"
DoCmd.OutputTo acOutputQuery, "Payment-Details", acFormatXLS, outputFileName
Call excel_format(outputFileName, "Payment-Details")
End Sub


Public Function excel_format(xls As String, sheet As String) As Long
Dim xlf As Object, wbk As Object, wks As Object
Dim i As Long, Lr As Long
Set xlf = CreateObject("Excel.Application")
Set wbk = xlf.Workbooks.Open(fGetSpecialFolderLocation(CSIDL_PERSONAL) & "\" & xls)
Set wks = wbk.Sheets(sheet)
Lr = wks.UsedRange.Rows.Count
For i = 2 To Lr
    If wks.cells(i, 35).value = "Low" Then wks.cells(i, 11).Interior.Color = vbGreen
    If wks.cells(i, 35).value = "Medium" Then wks.cells(i, 11).Interior.Color = vbYellow
    If wks.cells(i, 35).value = "High" Then wks.cells(i, 11).Interior.Color = vbRed
Next i
wbk.Save
xlf.Application.Visible = True ' If you like to show workbook
'wbk.close
'xlf.Quit ' quit excel
Set xlf = Nothing
Set wbk = Nothing
Set wks = Nothing
End function
0
Comment
Question by:-MAS
  • 4
  • 2
6 Comments
 
LVL 48

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 500 total points
Comment Utility
Hi,

pls try

With ActiveWindow
    .SplitRow = 1
    .FreezePanes = True
End With
    
With ActiveSheet
    If .AutoFilterMode Then
        If .AutoFilter.FilterMode Then
            .AutoFilter.ShowAllData
        End If
    Else
        .Cells.AutoFilter
    End If
    .Cells.EntireColumn.AutoFit
End With

Open in new window

Regards
0
 
LVL 24

Author Comment

by:-MAS
Comment Utility
Where I have to apply this.

Within the function ?
if yes where? . I am not good in VB
0
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
Comment Utility
pls try

Public Function excel_format(xls As String, sheet As String) As Long
Dim xlf As Object, wbk As Object, wks As Object
Dim i As Long, Lr As Long
Set xlf = CreateObject("Excel.Application")
Set wbk = xlf.Workbooks.Open(fGetSpecialFolderLocation(CSIDL_PERSONAL) & "\" & xls)
Set wks = wbk.Sheets(sheet)
With xlf.ActiveWindow
    .SplitRow = 1
    .FreezePanes = True
End With
    
With wks
    If .AutoFilterMode Then
        If .AutoFilter.FilterMode Then
            .AutoFilter.ShowAllData
        End If
    Else
        .Cells.AutoFilter
    End If
    .Cells.EntireColumn.AutoFit
End With 

Lr = wks.UsedRange.Rows.Count
For i = 2 To Lr
    If wks.cells(i, 35).value = "Low" Then wks.cells(i, 11).Interior.Color = vbGreen
    If wks.cells(i, 35).value = "Medium" Then wks.cells(i, 11).Interior.Color = vbYellow
    If wks.cells(i, 35).value = "High" Then wks.cells(i, 11).Interior.Color = vbRed
Next i
wbk.Save
xlf.Application.Visible = True ' If you like to show workbook
'wbk.close
'xlf.Quit ' quit excel
Set xlf = Nothing
Set wbk = Nothing
Set wks = Nothing
End function 

Open in new window

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 24

Author Comment

by:-MAS
Comment Utility
Many thanks. It worked
0
 
LVL 24

Author Closing Comment

by:-MAS
Comment Utility
Many thanks. It worked
0
 
LVL 24

Author Comment

by:-MAS
Comment Utility
Appreciate if you can send the code to bold the heading. Not urgent reply when you are free
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now