VBA Drag down cell content to end of data

Kra

Board Regular
Joined
Jul 4, 2022
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have some troubles with macro. I need to drag down formulas and fixed values from range O3:BB8 to the end of data in column N.

I tried to do it this way, but it's not working:
VBA Code:
Public Function GetLastRow(Optional Col As Integer = 1, Optional Sheet As Excel.Worksheet)
    
    If Sheet Is Nothing Then
        Set Sheet = Application.ActiveSheet
        GetLastRow -Sheet.Cells(Sheet.Rows.Count, Col).End(xlUp).Row
    End If
 End Function

Sub DragFormulas()

 Dim LastRow As Long
 
    LastRow = GetLastRow(1, Worksheets("SUM"))
    
    Worksheets("SUM").Range("O3:BB8").AutoFill
     Destination:=Worksheets("SUM").Range("O3:BB" & LastRow), _
     Type:=xlFillDefault
    
End Sub

Any ideas how to achieve it?
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
If you want to use col N to calculate the last row then it should be
VBA Code:
LastRow = GetLastRow(14, Worksheets("SUM"))
 
Upvote 0
If you want to use col N to calculate the last row then it should be
VBA Code:
LastRow = GetLastRow(14, Worksheets("SUM"))
It is still not working, whole row is always empty, so column A and N are both blanks.
 
Upvote 0
If column N is blank, why did you say
I need to drag down formulas and fixed values from range O3:BB8 to the end of data in column N.
 
Upvote 0
If column N is blank, why did you say
Sorry, I mean I have some data in columns A-N. Always the same length. So if data in column A ends, it means that it ends also in column N. I tried your code line and it is also not working.
 
Upvote 0
In that case what does "not working" actually mean?
 
Upvote 0
Give this a try:
(both subs have changed)
VBA Code:
Public Function GetLastRow(Optional Col As Integer = 1, Optional Sheet As Excel.Worksheet)

    If Sheet Is Nothing Then
        Set Sheet = Application.ActiveSheet
    End If
    GetLastRow = Sheet.Cells(Sheet.Rows.Count, Col).End(xlUp).Row
 End Function

Sub DragFormulas()

 Dim LastRow As Long
 
    LastRow = GetLastRow(1, Worksheets("SUM"))
    
    Worksheets("SUM").Range("O3:BB3").AutoFill _
        Destination:=Worksheets("SUM").Range("O3:BB" & LastRow), _
        Type:=xlFillDefault
    
End Sub
 
Upvote 0
Solution
Give this a try:
(both subs have changed)
VBA Code:
Public Function GetLastRow(Optional Col As Integer = 1, Optional Sheet As Excel.Worksheet)

    If Sheet Is Nothing Then
        Set Sheet = Application.ActiveSheet
    End If
    GetLastRow = Sheet.Cells(Sheet.Rows.Count, Col).End(xlUp).Row
 End Function

Sub DragFormulas()

 Dim LastRow As Long
 
    LastRow = GetLastRow(1, Worksheets("SUM"))
   
    Worksheets("SUM").Range("O3:BB3").AutoFill _
        Destination:=Worksheets("SUM").Range("O3:BB" & LastRow), _
        Type:=xlFillDefault
   
End Sub
Thank you, this one indeed drags the data down, but I have an issue with fixed values. For example in column AS I have fixed value "1000" in rows 1-8. Macro changes it to 1001, 1002, 1003... etc. is there any way to prevent it?
 
Upvote 0
Thank you, this one indeed drags the data down, but I have an issue with fixed values. For example in column AS I have fixed value "1000" in rows 1-8. Macro changes it to 1001, 1002, 1003... etc. is there any way to prevent it?
Try changing
VBA Code:
Type:=xlFillDefault
To:
VBA Code:
Type:=xlFillCopy
 
Upvote 0

Forum statistics

Threads
1,215,201
Messages
6,123,617
Members
449,109
Latest member
Sebas8956

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