Excel VBA autofill by "x" number of rows.

stereoqt

New Member
Joined
Jan 27, 2020
Messages
10
Office Version
  1. 2019
Platform
  1. Windows
Hello! I want to automate some changes I have to make to these tables regularly.

Basically, I need to copy down (autofill) the formulas in columns E4, F4 and G4 (same as M4, N4, O4) the number of times (value) wrote in B1 (J1).

For example, if I write 1 in B1, there should only be 1 column. If I write 2, two columns, etc.

The formula in D4 works differently, it is copied according to the number of columns in the table on the left. (Check L4:L14)

I managed to do it using the following code, but I have no idea about the other formulas.

VBA Code:
Sub test()

    Dim lastRow As Long
    lastRow = Range("C" & Rows.Count).End(xlUp).Row
    Range("D2").AutoFill Destination:=Range("D2:D" & lastRow)

End Sub


Test1.xlsx
ABCDEFGHIJKLMNOP
1Number1Number2
2
3IndexCountIndexCount
41510950250.2511CatItem254319474532390.211010CatItem21230
5226978165569984590.151212CatItem12987
6313148406657274550.07
7753316430.87
8838214810.22
9926105620.63
101024273880.01
111122681170.91
121220564620.28
131318024510.19
141410459240.41
15
Filtered1
Cell Formulas
RangeFormula
D4,L4:L14D4=RAND()
E4E4=INDEX($A$4:$A$6,RANK.EQ(D4,$D$4:$D$6)+COUNTIF($D$4:D4,D4)-1,1)
F4F4=SMALL($E$4:$E$6,ROW(A1))
G4,O4:O5G4=VLOOKUP(F4,Database,2)
M4:M5M4=INDEX($I$4:$I$14,RANK.EQ(L4,$L$4:$L$14)+COUNTIF($L$4:L4,L4)-1,1)
N4:N5N4=SMALL($M$4:$M$14,ROW(A1))
 

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)
Maybe
VBA Code:
'...
Range("E4:G4").AutoFill Range("E4").Resize(1 + Range("B1").Value, 3)
'...
Range("M4:O4").AutoFill Range("M4").Resize(1 + Range("J1").Value, 3)
'...

Bye
 
Upvote 0
Maybe
VBA Code:
'...
Range("E4:G4").AutoFill Range("E4").Resize(1 + Range("B1").Value, 3)
'...
Range("M4:O4").AutoFill Range("M4").Resize(1 + Range("J1").Value, 3)
'...

Bye

Hello Anthony, first of all, thank you for responding. I couldn't make it work, however, I achieved the result I wanted using the following code:

VBA Code:
Sub autofill()

    Dim Numbert1 As Long
    Dim Numbert2 As Long
   
    Dim lrC As Long
    Dim lrD As Long
    Dim lrE As Long
    Dim lrF As Long
   
    Dim lrJ As Long
    Dim lrK As Long
    Dim lrL As Long
    Dim lrM As Long
   
   
    Numbert1 = Range("A1").Value
    Numbert2 = Range("H1").Value

   
    lrC = Range("A" & Rows.Count).End(xlUp).Row
    lrD = Range("D" & (3 + Numbert1)).Row
    lrE = Range("E" & (3 + Numbert1)).Row
    lrF = Range("F" & (3 + Numbert1)).Row
   
    lrJ = Range("H" & Rows.Count).End(xlUp).Row
    lrK = Range("K" & (3 + Numbert2)).Row
    lrL = Range("L" & (3 + Numbert2)).Row
    lrM = Range("M" & (3 + Numbert2)).Row

   
    Range("C3").autofill Range("C3:C" & lrC)
    Range("D3").autofill Range("D3:D" & lrD)
    Range("E3").autofill Range("E3:E" & lrE)
    Range("F3").autofill Range("F3:F" & lrF)
   
    Range("J3").autofill Range("J3:J" & lrJ)
    Range("K3").autofill Range("K3:K" & lrK)
    Range("L3").autofill Range("L3:L" & lrL)
    Range("M3").autofill Range("M3:M" & lrM)
   
     
End Sub

In cells A1 and H1 I write the number of rows I want to add, but every time I write 0, I get an error (I can't autofill the same row I guess).

I think that implementing an If block in the code would do the trick

VBA Code:
If Numbert1 = 0
Then (Do nothing)

But I have no idea how to do it since my VBA skills are quite limited. Can anyone help me?

test.xlsm
ABCDEFGHIJKLMN
104
2NumberCountRandIndexMinVlookupNumberCountRandIndexMinVlookup
31510950250.611CatItem254319474532390.7794CatItem12402
42269781650.1433CatItem87440569984590.9745CatItem 43341
53131484060.17657274550.8777CatItem90134
6753316430.21139CatItem20981
7838214810.93513CatItem19620
8926105620.93
91024273880.2
101122681170.62
111220564620.84
121318024510.57
131410459240.69
14
Sheet1
Cell Formulas
RangeFormula
D3:D4D3=INDEX([Number],RANK.EQ(C3,[Rand])+COUNTIF($C$3:C3,C3)-1,1)
E3:E4E3=SMALL([Index],ROW(A1))
M3:M7,F3:F4F3=VLOOKUP(E3,Database,2)
K3:K7K3=INDEX([Number],RANK.EQ(J3,[Rand])+COUNTIF($J$3:J3,J3)-1,1)
L3:L7L3=SMALL([Index],ROW(A1))
J3:J13,C3:C5C3=RAND()
 
Upvote 0
Try this version:
VBA Code:
Sub Autofill()
Dim Numbert1 As Long
Dim Numbert2 As Long
Dim lrC As Long
Dim lrJ As Long
'   
Numbert1 = Range("A1").Value
Numbert2 = Range("H1").Value
'
lrC = Range("A" & Rows.Count).End(xlUp).Row
lrJ = Range("H" & Rows.Count).End(xlUp).Row
If lrC > 3 Then
    Range("C3").Autofill Range("C3:C" & lrC)
End If
If Numbert1 > 1 Then
    Range("D3").Autofill Range("D3:D" & 3 + Numbert1)
    Range("E3").Autofill Range("E3:E" & 3 + Numbert1)
    Range("F3").Autofill Range("F3:F" & 3 + Numbert1)
End If
If lrJ > 3 Then
    Range("J3").Autofill Range("J3:J" & lrJ)
End If
If Numbert2 > 1 Then
    Range("K3").Autofill Range("K3:K" & 3 + Numbert2)
    Range("L3").Autofill Range("L3:L" & 3 + Numbert2)
    Range("M3").Autofill Range("M3:M" & 3 + Numbert2)
End If
End Sub
Bye
 
Upvote 0

Forum statistics

Threads
1,215,707
Messages
6,126,353
Members
449,311
Latest member
accessbob

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