VBA Autofill from active cell

Hansulet

Board Regular
Joined
Jan 24, 2013
Messages
173
Office Version
  1. 2021
Platform
  1. Windows
I need a VBA to make an autofill from active cell.
I mention that I don't know the column letter.
It can be column A or column E or another column. It depends on running another macro.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
How do we know how far down to Autofill?
Should we be looking at the column to the left or to the right?
(Obviously, if you are trying to autofill column A, you won't be able to look to the left).
 
Upvote 0
How do we know how far down to Autofill?
Should we be looking at the column to the left or to the right?
(Obviously, if you are trying to autofill column A, you won't be able to look to the left).

Yes, you are right, if it is column A, it is not possible.
I apologize for exemplifying wrongly.
But the idea is that in my database, the column is dynamic because it expands by several columns every month.
 
Upvote 0
OK, so can you answer my question of how you would like us to determine how far down to copy the formula?
And am I safe to assume that the "ActiveCell" will already have a formula in it to copy down?

Perhaps showing us some sample data and expected results would be helpful here.

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
VALUENAMESURNAMESUM
214NAME 1SURNAME 1212
NAME 2SURNAME 22121
NAME 3SURNAME 366
NAME 4SURNAME 4221
 
Upvote 0
test.xls
ABCD
1VALUENAMESURNAMESUM
2214NAME 1SURNAME 1212
3NAME 2SURNAME 22121
4NAME 3SURNAME 366
5NAME 4SURNAME 4221
Sheet1
Cell Formulas
RangeFormula
A2A2=D2+2
 
Upvote 0
I want to make autofill in the VALUE Column. Mention that this column is dynamic because it expands by several columns every month
It can be column E or column M or another column. It depends on running another macro.
 
Upvote 0
Would something like this work for you ?

VBA Code:
    Dim rngFill As Range
    Dim rowLast As Long, colFill As Long
    
    Set rngFill = ActiveCell
    colFill = rngFill.Column
    
    If colFill = 1 Then
        rowLast = Cells(Rows.Count, colFill + 1).End(xlUp).Row   ' Look to the right if column A
    Else
         rowLast = Cells(Rows.Count, colFill - 1).End(xlUp).Row  ' Look to the left
    End If
    
    If rowLast <= rngFill.Row Then Exit Sub
    
    Set rngFill = Range(rngFill, Cells(rowLast, colFill))
    rngFill.FillDown

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,896
Messages
6,175,260
Members
452,627
Latest member
KitkatToby

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