Code Error - will not go to sheet at the end of the code ???

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,342
Office Version
  1. 365
Platform
  1. Windows
This code is giving me an error on:

Sheets("3 Source Selection").Range("B16").Activate

Code:
Private Sub CommandButton1_Click()

'**********************
'First Clear table

With Sheets("Selected Tasks").ListObjects("Selected_Tasks")
        
        'Check If any data exists in the table
        If Not .DataBodyRange Is Nothing Then
            'Clear Content from the table
            .DataBodyRange.ClearContents
        End If
        
    End With
    
'***********************

ListBox3.MultiSelect = fmMultiSelectMulti

'Loop through every item in the ListBox
For i = 0 To ListBox3.ListCount - 1

    'Check if the item was selected.
    If ListBox3.Selected(i) Then

        'If here, means this item has been selected.

        'Put all selected items in Column B
 With Sheets("Selected Tasks")
        If Len(.Range("A2").Value) = 0 Then
            .Range("A2").Value = ListBox3.List(i, 11)
        Else
            .Range("A" & .Rows.Count).End(xlUp).Offset(1).Value = ListBox3.List(i, 11)
        End If
    End With


    End If

Next i


UserForm3.Hide



MsgBox "Select the row that has the Cost Source that you would like to use for this part and its identified task(s)"

Sheets("3 Source Selection").Range("B16").Activate

End Sub

Thanks for the help!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
What is the error number and error description? There is not much that can go wrong on that line. The most likely thing is that you do not have a sheet called "3 Source Selection". Or maybe the sheet is protected and B16 is locked.
 
Upvote 0
... or if Sheets("3 Source Selection") isn't the ActiveSheet, you need two steps:
- activate the sheet
- select the range
 
Upvote 0
Solution
When I took out the Range part of the line it worked. I left it as that, It really didnt need to go to a cell. What was important was opening to the sheet.

Thank you both for the help - very much appreciated. Thanks!
 
Upvote 0
So @StephenCrump correctly identified the issue. You cannot Activate a Range on a sheet that is not the ActiveSheet. If you wanted to you, you could still add a line of code to activate/select a specific cell.
 
Upvote 0

Forum statistics

Threads
1,215,443
Messages
6,124,890
Members
449,194
Latest member
JayEggleton

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