Method 'Range' of object '_Worksheet' failed in Excel VBA, halp?
up vote
1
down vote
favorite
Okay, let me start by saying I am a noob at this. I'm following this step-by-step video, and I still found a way to mess it up somehow. I've tried reading related questions, but I don't really know much about VBA to make sense of the answers.
Basically, I'm trying (and failing) to create an "Employee Manager." At around 9:44 of the video, the narrator said we'll need to create a macro to hide and show specific cells depending on what tab we click on.
Here is my code for Sheet1:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("E4:H4")) Is Nothing Then
Range("B2").Value = Target.Column
Range("F2").Select
SwitchHTabs
End If
End Sub
And I have this code under modules, with the macro named SwitchHTabs:
Option Explicit
Sub SwitchHTabs()
Dim SelCol As Long
Dim FirstRow As Long
SelCol = ActiveCell.Column
With Sheet1
.Range("5:84").EntireRow.Hidden = True
FirstRow = 5 + ((SelCol - 5) * 20)
.Range(FirstRow & "." & FirstRow + 19).EntireRow.Hidden = False 'This is the line with the error
End With
End Sub
This error pops up when I try to run the code:
And when I click Debug, it highlights this line of code:
Where'd I mess up? I'm using Excel 2016. Thank you!
excel vba
New contributor
add a comment |
up vote
1
down vote
favorite
Okay, let me start by saying I am a noob at this. I'm following this step-by-step video, and I still found a way to mess it up somehow. I've tried reading related questions, but I don't really know much about VBA to make sense of the answers.
Basically, I'm trying (and failing) to create an "Employee Manager." At around 9:44 of the video, the narrator said we'll need to create a macro to hide and show specific cells depending on what tab we click on.
Here is my code for Sheet1:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("E4:H4")) Is Nothing Then
Range("B2").Value = Target.Column
Range("F2").Select
SwitchHTabs
End If
End Sub
And I have this code under modules, with the macro named SwitchHTabs:
Option Explicit
Sub SwitchHTabs()
Dim SelCol As Long
Dim FirstRow As Long
SelCol = ActiveCell.Column
With Sheet1
.Range("5:84").EntireRow.Hidden = True
FirstRow = 5 + ((SelCol - 5) * 20)
.Range(FirstRow & "." & FirstRow + 19).EntireRow.Hidden = False 'This is the line with the error
End With
End Sub
This error pops up when I try to run the code:
And when I click Debug, it highlights this line of code:
Where'd I mess up? I'm using Excel 2016. Thank you!
excel vba
New contributor
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Okay, let me start by saying I am a noob at this. I'm following this step-by-step video, and I still found a way to mess it up somehow. I've tried reading related questions, but I don't really know much about VBA to make sense of the answers.
Basically, I'm trying (and failing) to create an "Employee Manager." At around 9:44 of the video, the narrator said we'll need to create a macro to hide and show specific cells depending on what tab we click on.
Here is my code for Sheet1:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("E4:H4")) Is Nothing Then
Range("B2").Value = Target.Column
Range("F2").Select
SwitchHTabs
End If
End Sub
And I have this code under modules, with the macro named SwitchHTabs:
Option Explicit
Sub SwitchHTabs()
Dim SelCol As Long
Dim FirstRow As Long
SelCol = ActiveCell.Column
With Sheet1
.Range("5:84").EntireRow.Hidden = True
FirstRow = 5 + ((SelCol - 5) * 20)
.Range(FirstRow & "." & FirstRow + 19).EntireRow.Hidden = False 'This is the line with the error
End With
End Sub
This error pops up when I try to run the code:
And when I click Debug, it highlights this line of code:
Where'd I mess up? I'm using Excel 2016. Thank you!
excel vba
New contributor
Okay, let me start by saying I am a noob at this. I'm following this step-by-step video, and I still found a way to mess it up somehow. I've tried reading related questions, but I don't really know much about VBA to make sense of the answers.
Basically, I'm trying (and failing) to create an "Employee Manager." At around 9:44 of the video, the narrator said we'll need to create a macro to hide and show specific cells depending on what tab we click on.
Here is my code for Sheet1:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("E4:H4")) Is Nothing Then
Range("B2").Value = Target.Column
Range("F2").Select
SwitchHTabs
End If
End Sub
And I have this code under modules, with the macro named SwitchHTabs:
Option Explicit
Sub SwitchHTabs()
Dim SelCol As Long
Dim FirstRow As Long
SelCol = ActiveCell.Column
With Sheet1
.Range("5:84").EntireRow.Hidden = True
FirstRow = 5 + ((SelCol - 5) * 20)
.Range(FirstRow & "." & FirstRow + 19).EntireRow.Hidden = False 'This is the line with the error
End With
End Sub
This error pops up when I try to run the code:
And when I click Debug, it highlights this line of code:
Where'd I mess up? I'm using Excel 2016. Thank you!
excel vba
excel vba
New contributor
New contributor
edited Nov 10 at 14:25
Chronocidal
2,5001216
2,5001216
New contributor
asked Nov 10 at 14:20
aiseaisebb
84
84
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Compare your two Range
lines in that sub:
.Range("5:84").EntireRow.Hidden = True 'Works
.Range(FirstRow & "." & FirstRow + 19).EntireRow.Hidden = False 'Doesn't Work
For the sake of argument, let's assume that FirstRow
is 1, and then process that second line of code:
.Range("5:84").EntireRow.Hidden = True 'Works
.Range("1.20").EntireRow.Hidden = False 'Doesn't Work
Hopefully it is now easier to see that you have used a Full-Stop in place of a Colon.
(As an aside - you can use .Rows("5:84").Hidden
instead to remove the need for .EntireRow
)
O. M. F. G. I can't believe I missed that, you're a lifesaver! Thank you, thank you, thank you!!!
– aiseaisebb
Nov 10 at 14:33
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Compare your two Range
lines in that sub:
.Range("5:84").EntireRow.Hidden = True 'Works
.Range(FirstRow & "." & FirstRow + 19).EntireRow.Hidden = False 'Doesn't Work
For the sake of argument, let's assume that FirstRow
is 1, and then process that second line of code:
.Range("5:84").EntireRow.Hidden = True 'Works
.Range("1.20").EntireRow.Hidden = False 'Doesn't Work
Hopefully it is now easier to see that you have used a Full-Stop in place of a Colon.
(As an aside - you can use .Rows("5:84").Hidden
instead to remove the need for .EntireRow
)
O. M. F. G. I can't believe I missed that, you're a lifesaver! Thank you, thank you, thank you!!!
– aiseaisebb
Nov 10 at 14:33
add a comment |
up vote
1
down vote
accepted
Compare your two Range
lines in that sub:
.Range("5:84").EntireRow.Hidden = True 'Works
.Range(FirstRow & "." & FirstRow + 19).EntireRow.Hidden = False 'Doesn't Work
For the sake of argument, let's assume that FirstRow
is 1, and then process that second line of code:
.Range("5:84").EntireRow.Hidden = True 'Works
.Range("1.20").EntireRow.Hidden = False 'Doesn't Work
Hopefully it is now easier to see that you have used a Full-Stop in place of a Colon.
(As an aside - you can use .Rows("5:84").Hidden
instead to remove the need for .EntireRow
)
O. M. F. G. I can't believe I missed that, you're a lifesaver! Thank you, thank you, thank you!!!
– aiseaisebb
Nov 10 at 14:33
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Compare your two Range
lines in that sub:
.Range("5:84").EntireRow.Hidden = True 'Works
.Range(FirstRow & "." & FirstRow + 19).EntireRow.Hidden = False 'Doesn't Work
For the sake of argument, let's assume that FirstRow
is 1, and then process that second line of code:
.Range("5:84").EntireRow.Hidden = True 'Works
.Range("1.20").EntireRow.Hidden = False 'Doesn't Work
Hopefully it is now easier to see that you have used a Full-Stop in place of a Colon.
(As an aside - you can use .Rows("5:84").Hidden
instead to remove the need for .EntireRow
)
Compare your two Range
lines in that sub:
.Range("5:84").EntireRow.Hidden = True 'Works
.Range(FirstRow & "." & FirstRow + 19).EntireRow.Hidden = False 'Doesn't Work
For the sake of argument, let's assume that FirstRow
is 1, and then process that second line of code:
.Range("5:84").EntireRow.Hidden = True 'Works
.Range("1.20").EntireRow.Hidden = False 'Doesn't Work
Hopefully it is now easier to see that you have used a Full-Stop in place of a Colon.
(As an aside - you can use .Rows("5:84").Hidden
instead to remove the need for .EntireRow
)
answered Nov 10 at 14:26
Chronocidal
2,5001216
2,5001216
O. M. F. G. I can't believe I missed that, you're a lifesaver! Thank you, thank you, thank you!!!
– aiseaisebb
Nov 10 at 14:33
add a comment |
O. M. F. G. I can't believe I missed that, you're a lifesaver! Thank you, thank you, thank you!!!
– aiseaisebb
Nov 10 at 14:33
O. M. F. G. I can't believe I missed that, you're a lifesaver! Thank you, thank you, thank you!!!
– aiseaisebb
Nov 10 at 14:33
O. M. F. G. I can't believe I missed that, you're a lifesaver! Thank you, thank you, thank you!!!
– aiseaisebb
Nov 10 at 14:33
add a comment |
aiseaisebb is a new contributor. Be nice, and check out our Code of Conduct.
aiseaisebb is a new contributor. Be nice, and check out our Code of Conduct.
aiseaisebb is a new contributor. Be nice, and check out our Code of Conduct.
aiseaisebb is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239858%2fmethod-range-of-object-worksheet-failed-in-excel-vba-halp%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown