Hi,
I'm trying to Set all the cells in a given range to a value from another cell. I'm using a string variable to change the range whenever I like. It works fine when I assign a value to variable "MyRange" and use it like
, except when it's inside an If..Then..Else Statement. When I try to use it here, the VBA editor/compiler says "Method 'Range' of Object_Global Failed"
Here's a sample of my code:
Can someone please help me by explaining why this doesn't work when inside the IF...Then.....Else directive?
I'm so confused.
PS:I'm not using Option Explicit as I don't know how to define LastCell if I do.
I'm trying to Set all the cells in a given range to a value from another cell. I'm using a string variable to change the range whenever I like. It works fine when I assign a value to variable "MyRange" and use it like
Code:
Range(MyRange).Select
Here's a sample of my code:
Code:
Public Sub SortAll()
Dim ws As Worksheet
Dim LastRow As Long
Set LastCell = Range("A1").SpecialCells(xlCellTypeLastCell) 'gets the last used cell
Dim MyRange As String
Range("J8").Select
'Set up DistID For Sheets with Handling Credit in K8
If ActiveCell.Value = "Handling Credit" Then
Range("K8").Select
ActiveCell.FormulaR1C1 = "DistID" 'Set Column heading
MyRange = "K9:K" & LastRow 'Set Range to end at the last row
Range(MyRange).Select 'IT FAILS HERE
Selection.Value = "=$H$4"
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Selection.NumberFormat = "@"
Else
Range("I7").Select
End If
Can someone please help me by explaining why this doesn't work when inside the IF...Then.....Else directive?
I'm so confused.
PS:I'm not using Option Explicit as I don't know how to define LastCell if I do.