Autofill Cells with Drop-down List Until Last Row using VBA?

kchuphone

New Member
Joined
Jun 6, 2022
Messages
7
Office Version
  1. 2021
Platform
  1. Windows
Looking for some help with autofilling using VBA

I have a table and would like to add a drop-down list (using data validation) in column B until the last row using VBA.

I tried the VBA code below but unfortunately couldn't get it to work. Any insight or pointers will be super appreciated!

AB
1Sample 1
2Sample 2
3Sample 3


VBA Code:
Sub listCreator()

Dim last_row As Long

last_row = Cells(Rows.Count, 2).End(xlUp).Row

Range("B1").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="YES,NO"

Range("B1").AutoFill Destination:=Range("B1:B" & last_row)

End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Your data is in column A so the last cell has to be calculated on that column, change to:
VBA Code:
last_row = Cells(Rows.Count, 1).End(xlUp).Row
 
Upvote 0
Welcome to the MrExcel board!

You can also put them all in directly at once rather that just the top one and then filling down.

VBA Code:
Sub listCreator_v2()
  Dim last_row As Long
  
  last_row = Cells(Rows.Count, 1).End(xlUp).Row
  Range("B1:B" & last_row).Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="YES,NO"
End Sub

BTW,
For the future, it makes it even easier for helpers if you can give your sample data with XL2BB
 
Upvote 0
Welcome to the MrExcel board!

You can also put them all in directly at once rather that just the top one and then filling down.

VBA Code:
Sub listCreator_v2()
  Dim last_row As Long
 
  last_row = Cells(Rows.Count, 1).End(xlUp).Row
  Range("B1:B" & last_row).Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="YES,NO"
End Sub

BTW,
For the future, it makes it even easier for helpers if you can give your sample data with XL2BB
Thank you! I'm new to VBA and this is super helpful!
 
Upvote 0
You're welcome. Thanks for the follow-up. :)

BTW, you could also do it in a single code line

VBA Code:
Sub listCreator_v3()
  Intersect(Columns("B"), ActiveSheet.UsedRange.EntireRow).Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="YES,NO"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,177
Members
452,615
Latest member
bogeys2birdies

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