Button - Macro to auto-populate data

D

dcarter

I am trying to create two buttons that, when either is selected, will run a
macro to populate all fields in that column with a "Yes" or "No" depending on
what button they clicked on. Let's say the "Yes" button is placed in cell
A1, and, if selected, all fields from A2 on have "Yes" inputted. I would
also like the macro to look at Column B. If the contents of cell B2 are
blank, then A2 would remain blank. If cell B3 has something in it, then A3
would populate with a "Yes" after the "Yes" button is selected.

I know how to create the buttons, but I am having trouble completing the
macro. Any help you could provide would be greatly appreciated. Thanks!
 
D

DomThePom

Here is a sub that does what you need - copy the code into a standard module
and run the sub fom the click event of your buttons, using "Yes" abd "No" as
parameters, as appropriate
Note that I have assumed that there is nothing else on your sheet apart from
what you have described in columns A and B - if this is not the case then you
will have to redefine the range rng below.
There are clearly many ways that your requirement could be met but I think
using the autofilter provides the most elegant solution

****************************************
Start code
****************************************
Sub DoFill(ByVal txtType As String)
Dim rng As Range
'turn off screen
Application.ScreenUpdating = False
'define the right hand column of the range (B1 to the last used cell)
Set rng = Range(ActiveSheet.Cells(1, 2), _
ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell))
With rng
'filter the range to non-blanks
.AutoFilter Field:=1, Criteria1:="<>"
'fill the visible cells 1 column to the left with the parameter
.Offset(1, -1).Resize(rng.Rows.Count - 1). _
SpecialCells(xlCellTypeVisible).Value = txtType
'clear the autofilter
.AutoFilter
End With
Application.ScreenUpdating = True
Set rng = Nothing
End Sub

**************************************
End of Code
**************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top