Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50158

Tricky. Upon deactivating a ws, trying to launch a form.. can't pass ws name into form

$
0
0
Trying to create a TaskManager: create some tasks, each of which has its own worksheet. Each worksheet has a series of steps, with a Y/N drop down to check if each step is complete.

When the user leaves the page, (deactivate sheet), I want to launch a form which is populated with each of the steps with a N completion status, or if all the steps are complete, archive and delete the task.
That's all done.

The problem is that upon deactivating a sheet, the next sheet becomes the ActiveSheet, so I can't use that in my code (I want to check the values in the deactivating sheet). I've resolved that by dimming shP (sheetPrevious) as the sheet that is being deactivated, but I can't seem to pass that sheet into the form, so the form keeps collecting from the new sheet.

Here's the code for the Deactivate, which only has an error upon calling the form. (shP is a global variable, Dim on Workbook page public)


Code:

Public Sub Workbook_SheetDeactivate(ByVal Sh As Object)

Dim i As Integer
Dim strResult
Dim irows As Integer
Dim j As Integer
Set shP = Sh

irows = shP.UsedRange.Rows.Count

j = 5

If shP.Range("A5") = "Y" Or shP.Range("A5") = "N" Then
    For i = 5 To irows
       
        If shP.Range("A" & i) = "N" Then
            Exit For
        End If
        j = j + 1
    Next i

    If j < irows + 1 Then
        TaskCheckupForm.Show
        Else
            strResult = MsgBox("You have completed all the steps for " & shP.Name & ". Would you like to archive and delete it?", vbYesNo)
            If strResult = vbYes Then
                Call Archive
            End If
    End If
End If


End Sub

And here is the Form Initialize Code, which populates the form (I tried making it public too, that hasn't changed anything), ws is a public variable as well:confused:

Code:

Public Sub UserForm_Initialize()
    'Often people complete a task, but forget to mark it off as completed. This prompts them with incomplete tasks and asks which they have completed!

Dim i As Integer
Dim strStep As String


Set ws = shP

lblTitle.Caption = "For the task " & shP.Name & ", did you complete any of these steps?"

    For i = 5 To 50 'collecting all incomplete tasks
        If ws.Range("A" & i) = "N" Then
            strStep = ws.Range("B" & i)
            TaskCheckupForm.lbxTasks.AddItem strStep
            TaskCheckupForm.Height = Height + 10
                'making the form size accomodate the number of entries.
        End If
    Next i


End Sub

Any idea how to pass in the value for shP into the form????

Viewing all articles
Browse latest Browse all 50158

Trending Articles