Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
For-each to copy rows to another workbook
I received help on this from Tom Ogilvy (which I appreciate, of
course), but I need additional help. I thought I would try to lay out what I needed differently, as I tend to be too wordy... This macro should go down a list, and copy rows based on 2 criteria. 1) to determine what the next workday is, like Monday, Tues, etc (a formula is in place to do this) 2) based on the next workday, it should read a certain table that contains the warehouse row numbers to look for in the source workbook. If tomorrow is a Tuesday, then it should look at a table with a range name of CycleCount_Tuesday, and during the For-each section, copy all rows from the source list that contain these warehouse row numbers, and paste them to the target workbook. Please look at the coding so far. Currently there are no errors given when I run this. The cursor ends up at the bottom of the source list like it should, but nothing at all is copied. My guess is that the assignment is not made during the If-Then-Else section, so it finds nothing that matches the criteria. Thanks to all for your help. J.O. Sub CycleCount() Dim RowsToCount As Range Dim CycleCount_Monday As Object Dim CycleCount_Tuesday As Object Dim CycleCount_Wednesday As Object Dim CycleCount_Thursday As Object Dim CycleCount_Friday As Object Dim DayOfWeek As Integer 'Opens up the Blank inventory workbook (target) ChDir "S:\Furniture Staging List\Staging List Inventories" Workbooks.Open Filename:= _ "S:\Furniture Staging List\Staging List Inventories\Inventory Wk of BLANK.xls" 'Switch back to original (source) file ThisWorkbook.Activate Worksheets("Cycle Count").Activate 'DayOfWeek is the numeric value of the next weekday based on a formula. 'CycleCount_Monday (Tues, Wed, etc) are ranges containing the warehouse rows. '*****I think this is where the problem is. Assignment value does not seem ' to be given to RowsToCount.***** If Range("DayOfWeek") = 2 Then Set RowsToCount = CycleCount_Monday ElseIf Range("DayOfWeek") = 3 Then Set RowsToCount = CycleCount_Tuesday ElseIf Range("DayOfWeek") = 4 Then Set RowsToCount = CycleCount_Wednesday ElseIf Range("DayOfWeek") = 5 Then Set RowsToCount = CycleCount_Thursday ElseIf Range("DayOfWeek") = 6 Then Set RowsToCount = CycleCount_Friday End If 'This sets the range name TempColumnName to the column on the source list 'to be read by the macro. Worksheets("Official List").Activate Application.Goto Reference:="FirstRowOfficialList" ActiveCell.Offset(1, 0).Range("A1").Select Range(Selection, Selection.End(xlDown)).Select ActiveWorkbook.Names.Add Name:="TempColumnName", RefersToR1C1:= _ "='Official List'!R6C3:R416C3" 'Makes sure cursor starts at the top of the source list. Range("FirstRowOfficialList").Select ActiveCell.Offset(1, 0).Select 'Looks at each row for matching criteria based on above IF-Then-Else. For Each cell In Range("TempColumnName") If Not RowsToCount Is Nothing Then If cell.Value = RowsToCount Then CopyRows 'sub below that will perform copy routine Else MsgBox "Nothing copied because " & vbNewLine & _ "Value of Range DayofWeek is " & Range("DayofWeek").Value End If End If ActiveCell.Offset(1, 0).Select Next cell ThisWorkbook.Activate ActiveWorkbook.Names("TempColumnName").Delete End Sub Sub CopyRows() 'Copies rows to target workbook. Rows(ActiveCell.Row).Select Selection.Copy Windows("Inventory Wk of BLANK.xls").Activate Sheets("Data Dump").Select Range("B65536").End(xlUp).Offset(1, -1).Select ActiveSheet.Paste ThisWorkbook.Activate End Sub |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Copy Autofiltered rows to another workbook using macro | Excel Discussion (Misc queries) | |||
copy rows and paste in another workbook | Excel Programming | |||
Copy Range with Additional Rows to New Workbook | Excel Programming | |||
copy rows to another workbook | Excel Discussion (Misc queries) | |||
how to copy succeeding rows from source workbook to destination w. | Excel Programming |