Auto Fill macro

Alvaroro84

Board Regular
Joined
May 13, 2022
Messages
65
Office Version
  1. 2016
Platform
  1. Windows
VBA Code:
Selection.AutoFill Destination:=Sheets("Sheet1").Range("E2:E" & LastRow).SpecialCells(xlCellTypeVisible), Type:=xlFillDefault

I try to use this to code to auto fill all visible cells in column E. The reason it has to be visible rows is because it runs a filter before the code. However, the code above seem to be wrong can some one help me fix the above code in a similar format if possible
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
I don't think you can do what you're trying to do with auto fill. A loop should do the trick though:
VBA Code:
For i = 3 To lastrow
    If Range("E" & i).EntireRow.Hidden = False Then
        Range("E2").Copy Range("E" & i)
    End If
Next i
 
Upvote 0
If you are not trying to copy formatting then this might work for you.
VBA Code:
Sheets("Sheet1").Range("E2:E" & LastRow).SpecialCells(xlCellTypeVisible).FormulaR1C1 = Sheets("Sheet1").Range("E2").FormulaR1C1
 
Upvote 0
Solution
If you are not trying to copy formatting then this might work for you.
VBA Code:
Sheets("Sheet1").Range("E2:E" & LastRow).SpecialCells(xlCellTypeVisible).FormulaR1C1 = Sheets("Sheet1").Range("E2").FormulaR1C1
VBA Code:
ActiveSheet.Range("A1").AutoFilter field:=4, Criteria1:="0"
    ActiveSheet.Range("D2:D" & Rows.count).SpecialCells(xlCellTypeVisible).Range("A1").Select
ActiveCell.FormulaR1C1 = _
      "=VLOOKUP(RC[-3],'[ccc]Cos'!R2C3:R400000C4,2,False)"
      
Sheets("Sheet1").Range("D2:D" & LastRow).SpecialCells(xlCellTypeVisible).FormulaR1C1 = Sheets("Sheet1").Range("D2").FormulaR1C1
The code above is what I'm working with and it does not seem to be doing the job when it fills the data i fills it with numbers and its suppose to fill it with word
 
Upvote 0
I don't think you can do what you're trying to do with auto fill. A loop should do the trick though:
VBA Code:
For i = 3 To lastrow
    If Range("E" & i).EntireRow.Hidden = False Then
        Range("E2").Copy Range("E" & i)
    End If
Next i
VBA Code:
ActiveSheet.Range("A1").AutoFilter field:=4, Criteria1:="0"
    ActiveSheet.Range("D2:D" & Rows.count).SpecialCells(xlCellTypeVisible).Range("A1").Select
ActiveCell.FormulaR1C1 = _
      "=VLOOKUP(RC[-3],'[ccc]Cos'!R2C3:R400000C4,2,False)"

For i = 3 To lastrow
    If Range("E" & i).EntireRow.Hidden = False Then
        Range("E2").Copy Range("E" & i)
    End If
Next i
The code above is what I'm working with and it does not seem to be doing the job when it fills the data i fills it with numbers and its suppose to fill it with word
 
Upvote 0
Is the loop copying the formula down correctly? If so, then perhaps the issue is with your vlookup formula?
 
Upvote 0
Is the loop copying the formula down correctly? If so, then perhaps the issue is with your vlookup formula?
No the Loop doenst seem to be copying down correctly I've tested the code without the loop and the formula seems to work
 
Upvote 0
Is the variable "Lastrow" defined in your macro? Is the sheet this is running on, the active sheet in the workbook? These are the only things I can think of stopping the macro from running.
VBA Code:
lastrow = Cells(Rows.Count, 4).End(xlUp).Row

For i = 3 To lastrow
    If Range("E" & i).EntireRow.Hidden = False Then
        Range("E2").Copy Range("E" & i)
    End If
Next i
 
Upvote 0
Is the variable "Lastrow" defined in your macro? Is the sheet this is running on, the active sheet in the workbook? These are the only things I can think of stopping the macro from running.
VBA Code:
lastrow = Cells(Rows.Count, 4).End(xlUp).Row

For i = 3 To lastrow
    If Range("E" & i).EntireRow.Hidden = False Then
        Range("E2").Copy Range("E" & i)
    End If
Next i
I have it as this
VBA Code:
Dim LastRow As Long
 
Upvote 0

Forum statistics

Threads
1,214,559
Messages
6,120,208
Members
448,951
Latest member
jennlynn

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top