2 ways of doing it:
1. Put the code in the Excel workbook - if it's a static file you're using, or a template, should be easy enough. The only issue I've found with this method (beginning with Excel 2007) is that you'll end up getting prompts from Excel about saving the workbook as macro-enabled or not... if you're also saving the workbook in code.
2. Use the Excel Application object within Access. This should be easy enough to accomplish, and this way, you keep the code out of Excel altogether.
Here's an example of how I would do with your above code if I were to run it from Access:
1. In the Access VB Editor window, go to Tools - References, and make sure you've a reference to
Microsoft Excel ##.# Object Library where the ##.# part will depend upon your version of Office.
2. Put the below code into a module:
Code:
Sub DeleteEmptyRowsAndColumns()
Dim xlApp As Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim lngLastRow As Long [GREEN]'Didn't see your row variable dimmed, so added that[/GREEN]
Dim r as Long [GREEN]'Another one not declared.[/GREEN]
Dim intLastColumn As Integer [GREEN]'Not declared in your example[/GREEN]
Dim c As Integer [GREEN]'Not previously declared[/GREEN]
Set xlApp = New Excel.Application
Set wb = xlApp.Workbooks.Open("C:\YourWorkBookNamePath\YourWorkBookName.xlsx")
[GREEN]'Important that all Excel objects are created under your main xlApp object,
'else you'll wind up with leftover Excel processes in memory after closing out the program.[/GREEN]
Set ws = wb.ActiveSheet
xlApp.DisplayAlerts = False [GREEN]'If there are any messages in Excel you want to hide during this process[/GREEN]
lngLastRow = ws.UsedRange.Row - 1 + _
ws.UsedRange.Rows.Count
xlApp.ScreenUpdating = False [GREEN]'Here again - have to use the Excel application, since code will be in Access
'I would probably move the ScreenUpdating line up to where the DisplayAlerts line is if it were me.[/GREEN]
For r = lngLastRow To 1 Step -1
If xlApp.CountA(ws.Rows(r)) = 0 Then ws.Rows(r).Delete
Next r
intLastColumn = ws.UsedRange.Column - 1 + _
ws.UsedRange.Columns.Count
[GREEN]'xlApp.ScreenUpdating = False 'You already set this to False, so this is a wasted line[/GREEN]
For c = intLastColumn To 1 Step -1
If xlApp.CountA(ws.Columns(c)) = 1 Then ws.Columns(c).Delete
Next c
[GREEN]'CleanUp[/GREEN]
Set ws = Nothing
Set wb.Close [GREEN]'If you want to close the workbook - and you'll want to specify whether or not to save..[/GREEN]
Set wb = Nothing [GREEN]'Whether leaving the workbook open or not[/GREEN]
xlApp.Quit [GREEN]'If you want to quit the workbook and exit Excel after code is run[/GREEN]
Set xlApp = Nothing [GREEN]'Whether leaving Excel open or not[/GREEN]
[GREEN]'This next bit of cleanup is probably unnecessary, but I've just gotten into the habit of clearing out all variables as best I can[/GREEN]
lngLastRow = 0
r = 0
intLastColumn = 0
c = 0
End Sub
3. Call the code now in whatever action that was opening the workbook or doing whatever before you wanted this code run, like this:
Code:
Private Sub MyButton_Click()
DeleteEmptyRowsAndColumns
End Sub
By the way, as documented in the code, I noticed none of your variables are declared. When you do this, you'll end up using more memory than is necessary for the small macro, b/c the variables are set to Variant which uses more memory than Integer or Long, b/c it's much more flexible. You shouldn't allow that for something like Row and Column numbers.
I didn't test the code, but I've been using the Excel Object in Access VBA code a decent amount, myself, of late.
![[smile] [smile] [smile]](/data/assets/smilies/smile.gif)