I am currently in the process of re-purposing some older code to function as a loop rather then as 160 different macros (literally).
Anecdotally , I have committed the cardinal sin of selecting ranges and using active cell (partially due to my ignorance of how this would work otherwise), I have attempted to rebuild this using by assigning cell = cell etc, however I was unsure how this would work, I have returned to what I know.
The code is as follows:
Sub Refined_Code()
' Turn off the usual system hogs.
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
ActiveWorkbook.ForceFullCalculation = False
' Dim some ranges for use later on.
Dim BU As Range
Set BU = Sheets("Control").Range("C3")
Dim PorT As Range
Set PorT = Sheets("Control").Range("C8")
Dim BUStart As Range
Set BUStart = Sheets("Control").Range("D3")
Dim MasterData As Range
Set MasterData = Sheets("Parent and Child view").Range("Y6:Y180")
Dim MasterPaste As Range
Set MasterPaste = Sheets("Parent and Child view").Range("F6")
Dim BUlist As Range
Set BUlist = Sheets("Parent and Child view").Range("I106:AR116")
Dim PrevCell As Range
' Selects sheet "parent and child view" clears the data from the range, then selects the "Control' sheet.
Sheets("Parent and Child view").Select
Range("F6:R1000").Select
Selection.ClearContents
Sheets("Parent and Child view").Range("G6") = "G6"
Sheets("Control").Select
' Look up the datavalidated cell in D3 and find the right column
Range("G106:AR106").Select
Selection.Find(What:=BUStart.Value, After:=ActiveCell).Activate
ActiveCell.Select
Selection.Offset(1, 0).Select
' This will lookup the Range in D3, and commence running the macro chain.
Do While (Selection.Offset(1, 0) <> "")
Application.Calculation = xlCalculationManual
Sheets("Control").Select
BU.Value = ActiveCell.Value
Set PrevCell = ActiveCell
Application.Calculation = xlCalculationAutomatic
If InStr(1, ActiveCell, " ") = 1 Then
PorT.Value = "Target"
Else
PorT.Value = "Plan"
End If
Sheets("Parent and Child view").Select
MasterData.Copy
MasterPaste.Select
ActiveCell.End(xlToRight).Select
ActiveCell.Offset(0, 1).Select
ActiveCell.PasteSpecial xlPasteValues
PrevCell.Select
Selection.Offset(1, 0).Select
Loop
MsgBox "Finished"
End Sub
The code appears to crash on the line
PrevCell.Select
with a run-time error '1004' select method of range class failed.
I have tried following previous 'solved' guides for people who have used select, including placing the worksheet name and re-selecting this etc, but to no avail.
Any help or workaround would be GREATLY appreciated.
Warm regards,
RB.
[Update]
Thanks for the help guys, the solution was simple once the problem was identified, the range error was the result of trying to select the activecell on the incorrectly selected sheet.
Moral is, don't use select, but if you have to, atleast make your code tight.
The issue is here:
MasterPaste.Select
ActiveCell.End(xlToRight).Select
ActiveCell.Offset(0, 1).Select
ActiveCell.PasteSpecial xlPasteValues
PrevCell.Select
You've set MasterPaste as Sheets("Parent and Child view").Range("F6")
. But in this section of the loop...
Sheets("Control").Select
BU.Value = ActiveCell.Value
Set PrevCell = ActiveCell
...you set PrevCell to the ActiveCell
, which is on the "Control" Worksheet (see 2 lines prior). Then, 7 lines later, you do this:
Sheets("Parent and Child view").Select
Now, the Active Sheet is "Parent and Child view", which doesn't change before you call PrevCell.Select
. PrevCell
is (see above) a Range
on Worksheet "Control" which isn't the active sheet . Excel will throw a 1004 if you try to select a Range
that isn't on the active Worksheet.
Your instinct toward a top-down re-write is well warranted - it's close to impossible to decipher what you intend that this code does with all of the references to active objects and navigation through selection. I'd suggest a top down re-write without using Select, Active* or any of the other global objects.
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.