With Within With

ERLoft

Board Regular
Joined
Feb 24, 2006
Messages
193
For some reason the following code isn't working. It's still in the Yield Forecast Sheet when it assigns the value of CurrentMonthPickup to a Range.

This particular macro is called by another macro, so there's lots of public variables at work, and all runs as expected, save for the fact that despite the With Workbooks(YieldSheet).Sheets(Currentsht) the data is staying in the workbook that it's being pulled from.

Code:
Sub PullForecastPickup()
'This macro pulls the forecast transient pickup from the Transient Booking Pace spreadsheet.

Dim DayOfWeek As String
Dim MonthNumber As Integer
Dim Today As Date
Dim RowNumber As Long

Today = Date
DayOfWeek = CStr(Format(Today, "DDDD"))
MonthNumber = CStr(Format(Today, "M"))

Call FindTransientBookingPaceWorkbook

With Sheets("Yield Forecast Sheet")
    For RowNumber = .UsedRange.Rows.Count To 1 Step -1
        If (Range("A" & RowNumber) Like DayOfWeek) Then
            If MonthNumber = CurrentMonthToUpdate Then
                Cells(RowNumber, 2).Activate
                CurrentMonthPickup = Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(0, (LastDayOfMonth - EndHistoryDateToGrab))).Value
                With Workbooks(YieldSheet).Sheets(Currentsht)
                    Range(ActiveCell.Offset(11, (EndHistoryDateToGrab)), ActiveCell.Offset(11, (LastDayOfMonth - 1))).Value = CurrentMonthPickup
                End With
            ElseIf (MonthNumber + 1) = CurrentMonthToUpdate Then
                Cells(RowNumber, 3).Activate
            ElseIf (MonthNumber + 2) = CurrentMonthToUpdate Then
                Cells(RowNumber, 4).Activate
            Else
                Sheets("Input Sheet").Activate
                Exit Sub
            End If
        End If
    Next RowNumber
End With

End Sub

Not that it matters, but the ElseIf Then code isn't complete. I need to get the first If-Then working properly, then I'll worry about the ElseIf's.

Thanks in advance for any help!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I haven't looked at all your code, but a guess would be that Workbooks(YieldSheet) is not the active workbook and/or Sheets(Currentsht) is not the active sheet when you refer to Activecell. There can only be one active cell and that resides in the active sheet of the active workbook. Try making Workbooks(YieldSheet) and Sheets(Currentsht) active first.
 
Upvote 0
Thanks Joe, that was indeed it.

I changed it to Workbooks(YieldSheet).Sheets(Currentsht).Activate and got rid of the with. Works like a charm now...
 
Upvote 0

Forum statistics

Threads
1,216,095
Messages
6,128,790
Members
449,468
Latest member
AGreen17

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 MrExcel.com.
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 "mrexcel.com".
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
Back
Top