Autofill Range keeps giving me an error

Olio1225

New Member
Joined
May 30, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
So I've been trying to get my vba code to autofill a column. The 2nd cell in that column has a formula.

Dim nextEmptyColumn As Range

Set nextEmptyColumn = Worksheets("Sheet1").Range("A1").End(xlToRight).Offset(0, 1)

nextEmptyColumn.Value = "AlertKey"

nextEmptyColumn.Offset(1).Formula = "=(MID(Sheet1!D2,FIND(""AlertKey:"",Sheet1!D2)+LEN(""AlertKey:""),FIND(""AlertGroup"",Sheet1!D2)-FIND(""AlertKey:"",Sheet1!D2)-LEN(""AlertKey:"")))"

nextEmptyColumn.Offset(1).AutoFill Range()

I keep getting errors when I try to do range. Any know how to autofill from the second row in the column to the last row with data in the adjacent column.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
See if this helps:

VBA Code:
Sub test()

    Dim ws As Worksheet
    Dim nextEmptyColumn As Range

    
    Set ws = Worksheets("Sheet1")
    
    With ws
        Set nextEmptyColumn = .Range("A1").End(xlToRight).Offset(0, 1)
        nextEmptyColumn.Value = "AlertKey"
        nextEmptyColumn.Offset(1).Formula = "=(MID(Sheet1!D2,FIND(""AlertKey:"",Sheet1!D2)+LEN(""AlertKey:""),FIND(""AlertGroup"",Sheet1!D2)-FIND(""AlertKey:"",Sheet1!D2)-LEN(""AlertKey:"")))"
        
        'Autofill Lines
        Dim lastRowAdjacentCol As Long
        lastRowAdjacentCol = .Cells(Rows.Count, nextEmptyColumn.Offset(0, -1).Column).End(xlUp).Row
        nextEmptyColumn.Offset(1).AutoFill Destination:=.Range(nextEmptyColumn.Offset(1), .Cells(lastRowAdjacentCol, nextEmptyColumn.Column))
    End With
    
End Sub
 
Upvote 0
Solution
See if this helps:

VBA Code:
Sub test()

    Dim ws As Worksheet
    Dim nextEmptyColumn As Range

   
    Set ws = Worksheets("Sheet1")
   
    With ws
        Set nextEmptyColumn = .Range("A1").End(xlToRight).Offset(0, 1)
        nextEmptyColumn.Value = "AlertKey"
        nextEmptyColumn.Offset(1).Formula = "=(MID(Sheet1!D2,FIND(""AlertKey:"",Sheet1!D2)+LEN(""AlertKey:""),FIND(""AlertGroup"",Sheet1!D2)-FIND(""AlertKey:"",Sheet1!D2)-LEN(""AlertKey:"")))"
       
        'Autofill Lines
        Dim lastRowAdjacentCol As Long
        lastRowAdjacentCol = .Cells(Rows.Count, nextEmptyColumn.Offset(0, -1).Column).End(xlUp).Row
        nextEmptyColumn.Offset(1).AutoFill Destination:=.Range(nextEmptyColumn.Offset(1), .Cells(lastRowAdjacentCol, nextEmptyColumn.Column))
    End With
   
End Sub
Yup It worked perfectly. Thank you!
 
Upvote 0

Forum statistics

Threads
1,215,079
Messages
6,123,000
Members
449,092
Latest member
masterms

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