Macro to copy & paste but with conditions


New Member
Aug 4, 2010
Hi all

I need some help creating a macro for our church database. Basically I have reports for different missionaries. To generate the report I usually copy and paste information from a database of givers to a specific missionaries report.

What I would like the macro to do is to open the database and copy the information that relates specifically to a specific missionary. So the macro will go into the database and copy only information relating to missionary Joe Soap and then paste it into his report.

If anybody can help that would be awesome

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.


Board Regular
Nov 23, 2005
I have recently taught myself to do this sort of thing, reading and writing to an Access database.

Its not something you'll be able to have someone knock up a couple of lines of code for and post on here so your job is done if i'm being honest.

I'd suggest you record a macro where you use the Excel functionality to grab data from your database and see if you understand how it all works, or at least the bits that you will need to edit.

For what its worth i have posted some code i ended up with, but it took me a day or so to get my head round it to be able to hack this together from recorded and googled code.

But essentially i got to this point by doing as i've advised above.

Sub Selector_Updater()
'Used to update the the Initiatives available for selection in the Initiative drop down

Dim adoCtn As ADODB.Connection
Dim adoRS As ADODB.Recordset

Dim wsSQL As String
Dim wsconn As String
Dim wsCount As Integer
Dim sOutput As String
Dim myCtl As Control
Dim SQL_Crit As String
Dim wsColumn As String
Dim x As Integer
Dim wsVarCou As Integer
Dim SQL_Cols As String
Dim wsInit_Concat As String
Dim wsList(1 To 50) As String


'Creates Required Columns

SQL_Cols = "ID, Title, Actioner"

'Creates Selection Criteria

SQL_Crit = ""

'Checks whether a control is a checkbox and whether it is true, if yes it picks up the value in the associated control
For Each myCtl In Me("Select_Criteria").Controls
        If TypeName(myCtl) = "CheckBox" And myCtl = True Then
            'picks up the field name
            wsColumn = Right(myCtl.Name, Len(myCtl.Name) - 6)
            If Me("Select_" & wsColumn) = "" Then
                x = MsgBox("YOU HAVE NOT ENTERED A CRITERIA FOR" & vbNewLine & vbNewLine & wsColumn, vbOKOnly)
                Exit Sub
            End If
            SQL_Crit = SQL_Crit & "[" & wsColumn & "] = '" & Me("Select_" & wsColumn) & "' AND "
        End If
Next myCtl

If SQL_Crit = "" Then

    Exit Sub

End If

SQL_Crit = Left(SQL_Crit, Len(SQL_Crit) - 5)

'Creates SQL String
wsSQL = "SELECT " & SQL_Cols & " FROM Gen_Info WHERE " & SQL_Crit

'Sets up wsConn with database path and connection details
wsconn = "DSN=MS Access Database;" & _
        "DBQ=\\\users$\wisimpson\Tracker\Initiative Tracker.accdb;" & _
        "DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;"

'Creates and opens ADODB connection
Set adoCtn = New ADODB.Connection

adoCtn.Open wsconn

'Creates and setups up a record set
Set adoRS = New ADODB.Recordset

adoRS.Open Source:=wsSQL, ActiveConnection:=adoCtn

    If Not (adoRS.BOF Or adoRS.EOF) Then 'If there are no records, this will be false
       adoRS.MoveFirst 'This probably isn't necessary, particularly with the default
                       'cursor type,but I always include it anyway
        wsCount = adoRS.Fields.Count
        wsVarCou = 1
        Do While Not adoRS.EOF Or adoRS.BOF 'Start looping through the records
            x = 0
            wsInit_Concat = ""
            Do While x < wsCount
                wsInit_Concat = wsInit_Concat & adoRS.Fields.Item(x).Value & " - "
                x = x + 1
            wsInit_Concat = Left(wsInit_Concat, Len(wsInit_Concat) - 3)
            wsList(wsVarCou) = wsInit_Concat
            wsVarCou = wsVarCou + 1
            adoRS.MoveNext  'When your code has been running for 1/2 an hour, you
                           'probably forgot this line.  I know I forget it all the time.
    'Close stuff
    'Get rid of the last comma
    ' sOutput = Left(sOutput, Len(sOutput) - 1)
        sOutput = "Empty Recordset"
    End If
    Debug.Print sOutput
    'Clean up
    Set adoRS = Nothing
    Set adoCtn = Nothing

wsVarCou = 1

Do While wsList(wsVarCou) <> ""

    ActiveCell.Value = wsList(wsVarCou)
    ActiveCell(2, 1).Select
    wsVarCou = wsVarCou + 1


Range(ActiveCell, Selection.End(xlDown)).Select
ActiveWorkbook.Names.Add Name:="Init_list", RefersTo:=Selection
Init_Pick.RowSource = "=Init_list"

End Sub
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Latest member

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
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 "".
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