PDA

View Full Version : what's wrong with this code?



s.schwantes
01-12-2015, 07:12 PM
It keeps blowing up on line 2 ... If. Range

Also posted here:

http://www.mrexcel.com/forum/newthread.php?do=newthread&f=10

http://answers.microsoft.com/en-us/office/forum/office_2010-customize/whats-wrong-with-this-code/334e54af-f676-48f8-bb11-38f62ca4a464?tm=1421114858685


thanks in advance!

Steve


Sub Reporting_If_No()

ActiveSheet.Select
If .Range("$B$14") = "No" Then
'PART 1
Application.Goto Reference:="RR_Table_Copy"
Selection.Copy

Sheets("Rankin Report 1 - Summary").Select
Columns("E:F").Select
Selection.Insert Shift:=xlToRight

Range("E1:F28").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveSheet.Paste Link:=True
Application.CutCopyMode = False

'cut two lines - see notepad
Range("F1,F5,F8,F9").Select
Selection.ClearContents

Range("E26:F26").Select
Application.CutCopyMode = False
Selection.Style = "Percent"


'PART 2
Sheets("Rankin Report 2 - Detail").Select
Columns("C").Select
Selection.Insert Shift:=xlToRight

Range("C128").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveSheet.Paste Link:=True
Application.CutCopyMode = False

'cut two lines - see notepad
Range("D1,D5,D8,F9").Select
Selection.ClearContents
Range("C2626").Select
Application.CutCopyMode = False
Selection.Style = "Percent"

' End Test
' auto fit all columns
Cells.Select
Cells.EntireColumn.AutoFit
End If
' return user to Calculator sheet
End Sub

SamT
01-12-2015, 11:56 PM
Delete the dot in front of "Range"

Steve,

I formatted your macro to make it easier to read. Look around the forum especially at threads with lots of replies, and see how other people format their code.

The next thing to do is clean out all the "Select_Selection" pairs that the Macro recorder puts in

When you see

blahblah.Select
Selection.dostuff
Note the two dots? One in front of "Select" and on the nest line one after "Selection"

Delete only one of the dots, and both of the "Select(ion)"s so the code looks like

blahblah.dostuffall on one line.

You can usually edit a three liner with only one Object in the second line from this
Sheets("Rankin Report 2 - Detail").Select
Columns("C").Select
Selection.Insert Shift:=xlToRightTo this
Sheets("Rankin Report 2 - Detail").Columns("C").Insert Shift:=xlToRight

Wonder how I got those nice boxes to put the code in? Click the # button on the Post Formatting Menu.