Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

vba - Issue looping through worksheets in a workbook

I have a debugging problem for homework in class and I made the changes I saw that were necessary but I am getting an error. It says method or data member not found for my code that is shtCurrent.name that is in the message box.

I have researched as much as possible but I cannot find an answer. Can anyone help me out?

Thank you.

Public Sub PrintWorksheets1()
    'declare variables and assign address to object variable
    Dim strPrint As String, wkbHours As Workbook, shtCurrent As Worksheets
    Set wkbHours = Application.Workbooks("auco6215_HW10_Ex9.xlsm")
    wkbHours.Activate
    'ask user if he or she wants to print the worksheet
    For Each shtCurrent In wkbHours.Worksheets
        strPrint = _
            MsgBox(prompt:="Print " & shtCurrent.Name & "?", Buttons:=vbYesNo + vbExclamation)
        If strPrint = vbYes Then        'if user wants to print
            shtCurrent.PrintPreview
        End If
    Next shtCurrent
End Sub
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The shtCurrent As Worksheets should not be the worksheetS collection, just the worksheet object.

Dim strPrint As String, wkbHours As Workbook, shtCurrent As Worksheet

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...