View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.misc
OssieMac OssieMac is offline
external usenet poster
 
Posts: 2,510
Default Cannot use autofill after filtering

Are you interested in using macro code to autofill the required column? If so
then the following should do what you want.

If using the code then first make a backup of your workbook in case it does
not return the expected results.

To install the macro, right click the worksheet tab name.
Select view code.
Copy the code into the VBA editor.
Click the red X top right of VBA editor to close the editor.
Save the workbook.

To run the code, set the required Autofilter and then double click the
column header of Column B. (If not column B where you want the autofill then
you will need to edit the macro. See the green comment. Right click worksheet
tab name and then View code to re-open the VBA editor at the correct place.)

You will also have to set the macro security to allow macros to run and if
you are using xl2007 then you will need to save as an Excel Enabled workbook.
See help under macro security for more info on this.

The code will pop up a message if AutoFilter is not set or if actual filter
is not set and then terminate the processing.

Feel free to get back to me with any questions.


Private Sub Worksheet_BeforeDoubleClick _
(ByVal Target As Range, Cancel As Boolean)

'Edit "$B$1" in the following line to match your
'column header cell reference.
If Target.Address = "$B$1" Then
Dim rngToAutoFill As Range
Dim lngColumn As Long
Dim lngAutoFill As Long
Dim c As Range

Cancel = True

'Test if AutoFilter turned on.
With ActiveSheet
If .AutoFilterMode = False Then
MsgBox "AutoFilter is not turned on." _
& vbCrLf & "Set AutoFilter and re-run the code."
Exit Sub
End If

'Test if any filters are set.
If .FilterMode = False Then
MsgBox "No filters have actually been set." & vbCrLf & _
"Set filters and re-run the code."
Exit Sub
End If
End With

lngColumn = Target.Column

With ActiveSheet.AutoFilter.Range
Set rngToAutoFill = Columns(lngColumn) _
.Resize(.Rows.Count - 1, 1) _
.Offset(1, 0).SpecialCells(xlVisible)
End With

rngToAutoFill.Select

rngToAutoFill.NumberFormat = "000"

lngAutoFill = 1

For Each c In rngToAutoFill
c = lngAutoFill
lngAutoFill = lngAutoFill + 1
Next c
End If
End Sub



--
Regards,

OssieMac