简体   繁体   中英

Excel 2010 - Copy data to a specified sheet based on drop-down value.

new to this forum and hoping someone can help!

I'm creating a database of resources for a project. I have created a sheet called 'Summary' to enter details of the resource (Organisation, type, location, contact details etc). The list is organised vertically and has a few drop-down data validation fields. see summary

What I'm trying to do is attach a macro to the 'Add to database' button that will copy the data from the 'Summary' sheet to one of the other worksheets based on the value in the drop-down (cell: E4) and transpose this horizontally and then clear the field.

I've created individual Macro's to perform this (9 altogether) such as:

Sub EMPTADD()
Application.ScreenUpdating = False
Range("E2:E19").Select
Selection.Copy
Sheets("Employability & Training").Activate
Range("A750").End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("Summary").Activate
Range("E2:E19").Select
Application.CutCopyMode = False
Selection.ClearContents
Range("E2").Select
Application.ScreenUpdating = True
End Sub

I just can't work out where to go from there. Can I use an IF function to determin if E4 hold a specific value and call the relevant Macro? Any help much appreciated :)

You don't need to utilize the activate/selection type commands. VBA is capable of working much more dynamically than that. Using activate/selection type commands can also open your coding up to a number of unintended consequences. Sometimes what you think is active, and what excel thinks is active, can be two different things. It's best practice to fully qualify your ranges so there is never any doubt.

As far as coding goes, it looks like you're trying to manually replicate how you would go about clicking on specific ranges and manipulating the data (maybe basing it off of recording a macro?). We can use VBA more dynamically than that! I would use something like this:

Sub CopyPasteData()

'Declare your variables
Dim wb As Workbook
Dim criteria As String
Dim i As Long

Application.ScreenUpdating = False

    'Set values for your variables.
    Set wb = ThisWorkbook
    criteria = wb.Sheets("Summary").Range("E4")
    i = wb.Sheets(criteria).Range("A" & Rows.Count).End(xlUp).Row

        'Tell excel where to copy and paste your data
        wb.Sheets("Summary").Range("E1").Copy
        wb.Sheets(criteria).Range("A" & i + 1).PasteSpecial xlPasteValues
        wb.Sheets("Summary").Range("E2").Copy
        wb.Sheets(criteria).Range("B" & i + 1).PasteSpecial xlPasteValues
        wb.Sheets("Summary").Range("E3").Copy
        wb.Sheets(criteria).Range("C" & i + 1).PasteSpecial xlPasteValues
        wb.Sheets("Summary").Range("E4").Copy
        wb.Sheets(criteria).Range("D" & i + 1).PasteSpecial xlPasteValues

Application.ScreenUpdating = True


End Sub

If you use variables to set some of your values, you'll be able to use one macro for all of your Workbooks. No If statement will be necessary! The wb variable creates a shortcut for declaring which workbook the macro should operate in. The criteria value is set to whatever you have entered into cell E4 on the SUMMARY worksheet. As long as your Worksheet names match your dropdown list in E4, this will tell excel which Worksheet to put your data in. The i variable counts the number of rows that contain data (and that way we can paste new data at the bottom of those rows).

Then the last bit of code just tells Excel where to copy data, and where to paste it. Adjust the ranges as needed to do what you need it to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM