SOLVED

Use VBA to Autofill a Row until the end of the number of data in another row

Copper Contributor

Hello,

 

I need some help with the following problem:

 

The Macro should select the first cell with the vlookup (AY2) and autofill the complete range in the column AY until the last row that contain data in the cell next to it (Column E). Column E is the cell that the vlookup refers to.

 

The situation looks like this:

 

2017-12-14_09h25_21.png

 The code that I have so far looks like this:


    Sheets(3).Select
    Range("AY2").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-41],DennisAR!C[-50],1,0)"
    Selection.AutoFill Destination:=Range("AY2:AY1662")
    Range("AY2:AY1662").Select

 

The problem with this is, that the number of rows with data always change every week. So I cannot use a static row number of 1662.

 

I am looking for a way to make Destination:=Range("AY:AY1662) dynamic. In fact it has to refer to the number of rows with data in column E.

 

Thank you very much in advance,

Kai
   

 

 

81 Replies

@Haytham Amairah

 

Thanks for your quick reply!

 

I have an Excel file in which I import various reports and in this file I have a sheet, where I use those data for my formulas.

The various reports I import have the same length but differ in length every day.

 

In my main sheet where I use the imported data the first row always stays the same with all kind of formulas. However, I want my Macro to autofill the other rows until there is no more data in one of the imported reports.

 

Right now I have to set the autofill range manually. So if the report is too long, it's either cut or if it's too short I get errors.

 

Hope that was understandable.

 

Kind regards

@To31416

 

It seems that this situation is different!

 

But I think you need to somehow get the total number of rows in the imported reports and then use that number in the macro.

 

If you can attach your file or a sample of it, this is would be helpful!

@Haytham Amairah 

 

Thank you for helping me out with this.

 

I removed all data and a settings sheet but the important data is still there.

The macro-button is in the sheet "Output" Cell "F1"

 

The length should be the same as I got days in the reports. The totals column excluded.

@To31416

 

Hi,

 

I've updated the code behind the Load button to fill down the rows based on the number of rows in (yield) sheet.

 

This is the code:

Sub FillRowsDown()
    'Get the last row in sheet (yield) and save it in the lastRow variable
    Dim lastRow As Long
    lastRow = Sheets("yield").Range("A" & Rows.Count).End(xlUp).Row
   
    'Subtract the first 6 rows from the lastRow
    lastRow = lastRow - 6
    
    'Subtract the Total row (if exists)
    If LCase(Sheets("yield").Range("A" & Rows.Count).End(xlUp).Value) = "total" Then
        lastRow = lastRow - 1
    End If
    
    With Sheets("output")
     .Range("A4:U4").AutoFill Destination:=Range("A4:U" & lastRow + 3)
     .Range("D4").Select
    End With
End Sub

 

Hope that helps

@Haytham Amairah 

thank you so much! :)

 

you really were of great help

@Haytham Amairah 

 

Hi Haytham,

 

If I use to populate Row H to length of Row E

 

 

Sub Test()

   Range("H2").Select
    
    Selection.AutoFill Destination:=Range("H2:H" & Range("E" & Rows.Count).End(xlUp).Row)
Range(Selection, Selection.End(xlDown)).Select
End Sub

 

 

 

What would I need to add/change to get content from single Cell A2 and fill down from next empty in H

 

(the content of A2  changes and would like to see all changes updated in H rather than just overwriting) 

 

@aimeelou

 

Hi,

 

If I understand you well, nothing has to change in the code!

You need to use the below formula in cell H2 to link this cell to cell A2.

=A2

 

After then, run the code so that you fill this formula down until the last row in column E and make the column H and A linked together.

 

Regards

@Haytham Amairah 

 

Thanks for your help Haytham,

unfortunately I am probably doing something wrong as when I link the Cells then run macro rather than

filling down with the text that is in A2 - cells in H fill down with a zero

@aimeelou

 

Could you attach a sample of the data you working on?

@Haytham Amairah 

 

Thank you so much for helping

@aimeelou

 

If you need to fill the column H with the content of only cell A2 so that each cell in column H will be filled with the same text in cell A2.

Then you just need to lock the formula in cell H2 as follows:

=$A$2

 

@Haytham Amairah 

 

Hi Amairah,

 

I have a question somewhat similar to the post above.

Currently, I want to create a column with a function and want to autofill the column with the function until the end of the row

However, I wish to put in the number to rows so that it works on any worksheet (maybe with different number of rows)

 

Screenshot below is the formula I want to put into the column and autofill until the end of the row for any worksheet.

 

clipboard_image_1.png

Also, this is what I have so far 

 

Sub MacroCode_Column fill

 

Dim myFirstColumn As Integer
Dim myLastColumn As Integer
Dim myFirstFieldRow As Integer
Dim myFirstDataRow As Integer
Dim myLastDataRow As Integer


myFirstFieldRow = 5
myFirstDataColumn = Cells(5, Columns.Count).End(xlToLeft).Column + 1
myLastDataRow = Cells(Rows.Count, 1).End(xlUp).Row

 

'Multiple Occurred

ActiveSheet.Cells(myFirstFieldRow, myFirstDataColumn + 6).Select
ActiveCell.Formula = "MCO Incurred"
ActiveCell.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=SUMIF(R6C5:R1321C5,RC5,R6C22:R1321C22)*RC31"
Selection.AutoFill Destination:=Range(Cells(myFirstFieldRow + 1, myFirstDataColumn + 6), Cells(myLastDataRow, myFirstDataColumn + 6))

 

Thank you in advance!!

 

@D_Jang3884

 

Hi,

 

Based on what you want to fill the formula down?

You see that the solutions here in this conversation focus on filling the rows in a column based on the number of rows of another column.

@Haytham Amairah 

 

Hi Amairah,

 

Sorry if I asked a non-related question to the post but regarding to your question - basically I want to update the number of rows to the formula below using the following variables (which I created to set first row to be 5 and the last row to be determined based on data I'm using)

 

Dim myFirstDataRow As Integer
Dim myLastDataRow As Integer

 

myFirstFieldRow = 5

myLastDataRow = Cells(Rows.Count, 1).End(xlUp).Row 

 

and the formula I want to update is below

 

ActiveCell.FormulaR1C1 = "=SUMIF(R6C5:R1321C5,RC5,R6C22:R1321C22)*RC31"

 

Currently, the rows in the formula is in numbers but I wish to replace it using dimensions.

 

I would greatly appreciate it if you could help or direct me to where I can ask.

 

Thanks!!

@D_Jang3884

 

Hi,

 

I suggest you post this question in a separate post by starting a new conversation in here.

If you didn't find the answer, you may ask this community instead under [vba] tag.

 

Regards

Hi @Haytham, I'm trying to do the same as above to autofill multiple columns to varying rows and I have tried a couple of different things I've seen above, but I fear I don't have the excel coding knowledge to know what I'm doing wrong. the following is my macro and I'd like the "E145040" to be variable. I also dont know if I need to change the "$A$915118", "$C$915118", "$E$915118", "$G$915118" and "$AC$145040" as well. Thank you so much in advance! Sub Macro5() ' ' Macro5 Macro ' ' Range("Y1").Select ActiveCell.FormulaR1C1 = "COMP" ActiveCell.Offset(1, 0).Range("A1").Select ActiveCell.FormulaR1C1 = _ "=IF(RC[-21]=""Closed"",1,IF(RC[-21]=""Cancelled"",1,0))" ActiveCell.Offset(-1, 1).Range("A1").Select ActiveCell.Select ActiveCell.FormulaR1C1 = "PROC" ActiveCell.Offset(1, 0).Range("A1").Select ActiveCell.FormulaR1C1 = _ "=IF(OR(RC[-22]=""Closed"",RC[-22]=""Verified"",RC[-22]=""Ready_To_Verify"",RC[-22]=""Cancelled""),1,IF(OR(RC[-22]=""Assigned"",RC[-22]=""Reassigned"",RC[-22]=""Pending""),IF(COUNTIF(RC[-11]:RC[-8],""Yes/Oui"")>0,1,0),0))" ActiveCell.Offset(-1, 1).Range("A1").Select ActiveCell.FormulaR1C1 = "PC" ActiveCell.Offset(1, 0).Range("A1").Select ActiveCell.FormulaR1C1 = _ "=IF(OR(RC[-23]=""Reassigned"",RC[-23]=""Assigned"",RC[-23]=""Pending""),IF(COUNTIF(RC[-12]:RC[-9],""Yes/Oui"")>0,1,0),0)" ActiveCell.Offset(-1, 1).Range("A1").Select ActiveCell.FormulaR1C1 = "VER" ActiveCell.Offset(1, 0).Range("A1").Select ActiveCell.FormulaR1C1 = _ "=IF(RC[-24]=""Verified"",1,IF(RC[-24]=""Ready_To_Verify"",1,0))" ActiveCell.Offset(-1, 1).Range("A1").Select ActiveCell.FormulaR1C1 = "UNPROC" ActiveCell.Offset(1, 0).Range("A1").Select ActiveCell.FormulaR1C1 = _ "=IF(OR(RC[-25]=""Closed"",RC[-25]=""Verified"",RC[-25]=""Ready_To_Verify"",RC[-25]=""Cancelled""),0,IF(OR(RC[-25]=""Assigned"",RC[-25]=""Pending""),IF(COUNTIF(RC[-14]:RC[-11],""Yes/Oui"")>0,0,1),1))" ActiveCell.Offset(0, -4).Range("A1:E1").Select Selection.AutoFill Destination:=ActiveCell.Range("A1:E145040") ActiveCell.Range("A1:E145040").Select Sheets.Add After:=ActiveSheet Sheets("Sheet1").Select Rows("1:1").Select Selection.AutoFilter ActiveSheet.Range("$A$1:$AC$145040").AutoFilter Field:=25, Criteria1:="0" Columns("J:J").Select Selection.Copy Sheets("Sheet2").Select Range("A1").Select ActiveSheet.Paste Sheets("Sheet1").Select Application.CutCopyMode = False ActiveSheet.ShowAllData Range("Z2").Select ActiveSheet.Range("$A$1:$AC$145040").AutoFilter Field:=26, Criteria1:="0" Columns("J:J").Select Selection.Copy Sheets("Sheet2").Select Range("C1").Select ActiveSheet.Paste Sheets("Sheet1").Select Application.CutCopyMode = False ActiveSheet.ShowAllData Range("M2").Select ActiveSheet.Range("$A$1:$AC$145040").AutoFilter Field:=27, Criteria1:="1" Columns("J:J").Select Selection.Copy Sheets("Sheet2").Select Range("E1").Select ActiveSheet.Paste Sheets("Sheet1").Select Application.CutCopyMode = False ActiveSheet.ShowAllData Range("M2").Select ActiveSheet.Range("$A$1:$AC$145040").AutoFilter Field:=28, Criteria1:="1" Columns("J:J").Select Selection.Copy Sheets("Sheet2").Select Range("G1").Select ActiveSheet.Paste Columns("A:A").Select Application.CutCopyMode = False ActiveSheet.Range("$A$1:$A$915118").RemoveDuplicates Columns:=1, Header:= _ xlNo Columns("C:C").Select ActiveSheet.Range("$C$1:$C$915118").RemoveDuplicates Columns:=1, Header:= _ xlNo Columns("E:E").Select ActiveSheet.Range("$E$1:$E$915118").RemoveDuplicates Columns:=1, Header:= _ xlNo Columns("G:G").Select ActiveSheet.Range("$G$1:$G$915118").RemoveDuplicates Columns:=1, Header:= _ xlNo Sheets("Sheet1").Select ActiveSheet.ShowAllData Sheets.Add After:=ActiveSheet Sheets("Sheet1").Select ActiveSheet.Range("$A$1:$AC$145040").AutoFilter Field:=9, Criteria1:= _ "PA Exceptions – Exception PA" Cells.Select Range("J1").Activate Selection.Copy Sheets("Sheet3").Select Range("A1").Select ActiveSheet.Paste Application.CutCopyMode = False Sheets.Add ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ "Sheet3!R1C1:R981068C29", Version:=xlPivotTableVersion15).CreatePivotTable _ TableDestination:="Sheet4!R3C1", TableName:="PivotTable3", DefaultVersion _ :=xlPivotTableVersion15 Sheets("Sheet4").Select Cells(3, 1).Select With ActiveSheet.PivotTables("PivotTable3").PivotFields("User ID") .Orientation = xlRowField .Position = 1 End With ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _ "PivotTable3").PivotFields("PROC"), "Sum of PROC", xlSum ActiveWindow.SmallScroll Down:=-84 Sheets("Sheet1").Select ActiveSheet.ShowAllData Sheets("Sheet1").Select Sheets("Sheet1").Name = "1.0 PA RAW" Sheets("Sheet4").Select Sheets("Sheet4").Name = "2.1 production" Sheets("Sheet3").Select Sheets("Sheet3").Name = "2.0 PA exceptions" Sheets("Sheet2").Select Sheets("Sheet2").Name = "1.1 Daily update" End Sub

@Haytham Amairah 

I tried your suggestion and it works fine, but not for cases where there is only 1 row of data or no data (only heading). I have a heading in row 1, formula in row 2 (cell E2) which I'm trying to copy to the end of the table using this code:

 

Selection.AutoFill Destination:=Range("E2:E" & Range("C" & Rows.Count).End(xlUp).Row)

 

Many thanks in advance for your advice.

Sarka

@jahoda

 

Hi,

 

Sorry for the late reply!

Please attach a sample of the data you're working to fit the code on it.

 

Regards

Hi Haitham

I am new to VBA and I am trying to amend the ActiveCell.End(xlDown) part so that the formula (in column A) stops at the last row that contains data in column A. and again starts to autofill from this column to the next contains data and goes on,

 

and thank you in advance.

 this is a screenshot:

clipboard_image_0.png

 

Sub autofill()

Dim Wad As String
Wad = Cells(Rows.Count, "A").End(xlUp).Row
Range("A30").Select
Selection.autofill Destination:=Range("A30:A" & Range("A" & Rows.Count).End(xlDown).Row)
Range(Selection, Selection.End(xlUp)).Select

Selection.autofill Destination:=Range("wad:A" & Range("A" & Rows.Count).End(xlDown).Row)
Range(Selection, Selection.End(xlUp)).Select
End Sub

 

Waddah

@Haytham Amairah 

@walshamasi

 

Hi Waddah,

 

Sorry for the late reply, but I need some clarifications as column A seems to have no formulas in it!

If you can attach the worksheet here, that would be better.

 

Regards