Coding error I cant find code runs but no results

HEALTHSTAFF

New Member
Joined
Oct 5, 2017
Messages
12
hello,
I have a spreadsheet that is basically a roster that staff can choose to print just their own shifts.
The code below runs with no errors popping up but there is always no results shown.
The input is via userform list box with command button. (I have not used user forms before)
It seems to copy the list value and places in worksheet 1 OK but then doesn't seem to find the staff name. The font, size, spelling and no spaces are identical. so I don't know why it can find items.
The coding is a bit basic (Noob) and i have cobbled it together from old spreadsheets and Google. Could i trouble a guru to have a look and see if i am missing something obvious?

Many Thanks and please be gentle :)

'Private Sub CommandButton1_Click()

Step1
Step2

End Sub




Private Sub Step1()


Worksheets("Sheet3").Range("1:100").EntireRow.Hidden = False
Worksheets("Sheet3").Range("C4:P100").ClearContents
Application.ScreenUpdating = False
Worksheets("Sheet1").Select
Dim listItems As String, i As Long

With ListBox1
For i = 0 To .ListCount - 1
If .Selected(i) Then listItems = listItems & .List(i) & ""
Next i
End With
If Len(listItems) > 0 Then
Range("A1") = Left(listItems, Len(listItems) - 0)
Else
Range("A1") = ""
End If
End Sub


Private Sub Step2()
Dim c As Range
Dim ans As String
Dim anss As Variant
ans = Range("Sheet1!A1")
For Each c In Worksheets("Sheet1").Range("C4:P100")
If c.Value = ans Then
anss = c.Address
Sheets(3).Range(anss).Value = ans
End If
Next
Application.Calculation = xlCalculationManual


Set r = Worksheets("Sheet3").Range("C4:P100")
Application.ScreenUpdating = False
ActiveSheet.Rows.Hidden = False
r.EntireRow.Hidden = True
For Each c In r
If c <> Blank Then
c.EntireRow.Hidden = False
End If
Next c
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Worksheets("Sheet3").Activate
Application.Dialogs(xlDialogPrint).Show
Worksheets("Sheet3").Range("1:100").EntireRow.Hidden = False
Worksheets("Sheet3").Range("C4:P100").ClearContents
Worksheets("Sheet1").Activate
MsgBox ("STAFF PLEASE CHECK YOUR PRINTED ROSTER WITH ORIGINAL"), vbExclamation
Exit Sub
End Sub




Private Sub CommandButton2_Click()
Unload Me
End Sub'
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hello,

Thanks for your question. In future please do make use of the CODE tags to it formats correctly. Made a small change to how 'ans' is found. Does this fix the problem?
Code:
Private Sub CommandButton1_Click()
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With
    
    Call Step1
    Call Step2
    
    With Application
        .ScreenUpdating = True
        .Calculation = xlAutomatic
    End With
End Sub




Private Sub Step1()
    Dim listItems As String, i As Long


    With Worksheets("Sheet3")
        .Range("1:100").EntireRow.Hidden = False
        .Range("C4:P100").ClearContents
    End With
    Worksheets("Sheet1").Select
    
    With ListBox1
        For i = 0 To .ListCount - 1
            If .Selected(i) Then listItems = listItems & .List(i) & ""
        Next i
    End With
    
    If Len(listItems) > 0 Then
        Range("A1") = Left(listItems, Len(listItems) - 0)
    Else
        Range("A1") = ""
    End If
End Sub




Private Sub Step2()
    Dim sh1 As Worksheet, sh3 As Worksheet
    Dim c As Range
    Dim ans As String
    Dim anss As Variant
    
    Set sh1 = Sheets("Sheet1")
    Set sh3 = Sheets("Sheet3")
    
    ans = sh1.Range("A1").Value
    
    For Each c In sh1.Range("C4:P100")
            If c.Value = ans Then
            anss = c.Address
            sh3.Range(anss).Value = ans
        End If
    Next
    
    Set r = sh3.Range("C4:P100")
    'Which sheet is the next line meant to be for?
    ActiveSheet.Rows.Hidden = False


    r.EntireRow.Hidden = True
    For Each c In r
        If c <> Blank Then c.EntireRow.Hidden = False
    Next c


    With sh3
        .Activate
        Application.Dialogs(xlDialogPrint).Show
        .Range("1:100").EntireRow.Hidden = False
        .Range("C4:P100").ClearContents
    End With
    sh1.Activate
    MsgBox ("STAFF PLEASE CHECK YOUR PRINTED ROSTER WITH ORIGINAL"), vbExclamation
    Exit Sub
End Sub




Private Sub CommandButton2_Click()
    Unload Me
End Sub
 
Upvote 0
I did work out my issue yesterday, It was to do with referencing sheets. Worksheets ('Sheet1"). Range etc instead of Sheets (3).
Pretty much what you have done . Many Thanks for your reply
 
Upvote 0
Hello

Glad you got it working :)

Regards
Caleeco
 
Upvote 0

Forum statistics

Threads
1,216,096
Messages
6,128,807
Members
449,468
Latest member
AGreen17

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