Solved

Select worksheet in VB for Excel not working

Posted on 2014-09-03
7
195 Views
Last Modified: 2014-09-03
Experts,

I am tryhing to selet a worksheet so I can delete rows when the values of all of the columns is 0. I am getting an error on the select statement.  I am passing the string in the call to the routine.  Doesanyone see what the problem is?


****Call
Call HospitalCleanup("MEDICAL_VOID_REBUILD")



***routine

Sub HospitalCleanup(Product As String)
Dim lastrow As Long, i As Long, j As Long 'define variables
Application.ScreenUpdating = False 'disable ScreenUpdating to avoid screen flashes
ActiveSheet.Unprotect
Sheets(Product).Select
lastrow = Cells(Rows.Count, "A").End(xlUp).Row 'get last row

For i = lastrow To 9 Step -1
    If Len(Trim(Cells(i, 1))) > 0 Then
        For j = 1 To 12
            If Cells(i + 2, j * 2 + 1) <> 0 And Len(Cells(i + 2, j * 2 + 1)) > 0 Then GoTo notDelete
        Next
           
        Rows(i).Resize(4).Delete
notDelete:
    End If
Next i
    With Worksheets(Product)
       .Activate
       .Range("B5") = Product
       .Range("G3") = ""
    End With

ActiveSheet.Protect
Sheets(Product).Visible = False
Application.ScreenUpdating = True 'reenable ScreenUpdating
End Sub
0
Comment
Question by:morinia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40301365
Should be:
Sheets("Product").Select

Open in new window

0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40301371
Are you certain that MEDICAL_VOID_REBUILD is the name of the sheet?

Also what error are you getting?
0
 
LVL 27

Assisted Solution

by:MacroShadow
MacroShadow earned 100 total points
ID: 40301383
Oops I was too quick to jump the gun...
The code looks ok (workable...) if the sheet MEDICAL_VOID_REBUILD exists I don't see a problem.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:morinia
ID: 40301406
Experts,

The sheet is "MEDICAL_VOID_REBUILD ".  I think the problem may have been the sheet was hidden. Does that sound correct. I made the sheet visible and I did not see the error.
0
 
LVL 47

Accepted Solution

by:
Martin Liss earned 400 total points
ID: 40301417
Yes it does and I should have thought of that since you can't Activate or Select a hidden sheet.
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40301483
Thanks for the points but there's no need to assign them when you self-resolve a problem. If you 'Request Attention' I won't have any objection if you ask the moderator to help you remove the point assignment.
0
 
LVL 33

Expert Comment

by:Norie
ID: 40301503
I know this is solved but you should be able to do what you want without selecting/activating, or making the sheet visible.
Sub HospitalCleanup(Product As String)
Dim lastrow As Long, i As Long, j As Long    'define variables

    Application.ScreenUpdating = False    'disable ScreenUpdating to avoid screen flashes

    With Sheets(Product)
        .Unprotect
        lastrow = .Cells(Rows.Count, "A").End(xlUp).Row    'get last row

        For i = lastrow To 9 Step -1
            If Len(Trim(.Cells(i, 1))) > 0 Then
                For j = 1 To 12
                    If .Cells(i + 2, j * 2 + 1) <> 0 And Len(.Cells(i + 2, j * 2 + 1)) > 0 Then GoTo notDelete
                Next j

                .Rows(i).Resize(4).Delete
notDelete:
            End If
        Next i

        .Range("B5") = Product
        .Range("G3") = ""

        .Protect
    End With
    
    Sheets(Product).Visible = False
    Application.ScreenUpdating = True    'reenable ScreenUpdating
End Sub

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question