问题
Ok so best way to explain this, I have a spreadsheet that is produced monthly. It needs to be automated. From the report that gets shipped into excel I need to take all the data from the project number column and move it to another workbook. In this workbook are worksheets for 2012 and 2013. I need all of the projects starting 300, then 400, then 500 etc to move into the new workbook and then the 2012 projects to go into one worksheet and 2013 into another. How would I go about automating such a thing?
Thanks for any help
300873 Company Name 10/09/2012
300874 Company Name 10/09/2012
412774 Company Name 30-01-12
412972 Company Name 23-05-12
412986 Company Name 17-07-12
413001 Company Name 08/06/13
413048 Company Name 14-08-12
413049 Company Name 14-08-12
413062 Company Name 20-08-13
413068 Company Name 23-08-12
413169 Company Name 21-09-13
510003 Company Name 27-12-12
600161 Company Name 28-02-12
So with these I would want all of the projects with 2012 to go into one sheet and all the others to go into a 2013 sheet in a sperate workbook, I would also need the 300--- projects to go into the spreadsheet and fill rows underneath the number 300 so this might include adding rows.
回答1:
you can design a macro for this pretty easily. I'm going to give you some tools to use, give it a try, when and if there is something you're running into come back, post your code, and ask more questions on this question you've already made and I'll help you along with the code so you can learn.
I need to take all the data from the project number column and move it to another workbook.
First, code for Opening a file from your macro:
ChDir "[pathway to the folder]" 'loads file
Workbooks.Open Filename:= "[pathwayToFolder including filename]"
Workbooks("[workbook name]").Activate
Something else that is useful is the ability to specifically refer to different sheets and ranges without actually have to select or go to those sheets. This code, for example, writes something specific in a specific workbook, sheet, and range without going to them
Workbooks("Book1").Sheets("Sheet1").Range("A5") = "Test"
Second, Copying and Pasting:
This code copies all cells and pastes them all on another sheet, you can obviously copy and paste more specific ranges (google it if you need)
Cells.Copy
Sheets("[sheetname]").Select
ActiveSheet.Range("A1").Select
ActiveSheet.Paste
Third, Determining the Year:
If the date is in Column "C" I would just do this
Dim i As Integer, rowNumber As Integer
i = 1 'whatever row your data begins on
rowNumber = ActiveSheet.UsedRange.Rows.Count 'gets used number of rows
Do 'Loops through all data on the sheet
If Right(Range("C" & i), 2) = "12" Then 'checks the last 2 characters in date column
'copy data from that row and paste it wherever applicable
ElseIf Right(Range("C" & i), 2) = "13" Then 'checks for 2013
'move 2013 data wherever applicable
End If
i = i + 1
Loop Until i = rowNumber
Try to start putting this code together into something and get back to us on this question.
Also, in the future try to do some of the research and work on this before asking, the question is actually multiple questions and very broad, making it hard to answer. That is why someone downvoted it.
Hope this helps!
来源:https://stackoverflow.com/questions/12853800/moving-data-based-on-information-in-one-column-and-then-sorting-by-data-in-anoth