Solved

Select worksheet in VB for Excel not working

Posted on 2014-09-03
7
194 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
7 Comments
 
LVL 27

Expert Comment

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

Open in new window

0
 
LVL 46

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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 

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 46

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 46

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

828 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