嗯......
Sub HomeworkForNmHomie13()
Dim Response, Index, L1, L2, Answer
Do
Response = InputBox("Enter a number from 1 to " & Worksheets.Count)
If Response = "" Then Exit Sub
'Your teacher said don't do error handling, but that's for failures.
On Error Resume Next
Index = Int(Response)
On Error GoTo 0
If Index > Worksheets.Count Or Index < 1 Then
MsgBox ("Your entry was invalid. Please enter a number between 1 and " & Worksheets.Count)
End If
Loop While Index > Worksheets.Count Or Index < 1
Sheets(Index).Activate
L1 = Cells(Rows.Count, "A").End(xlUp).Row 'Assuming an "item" includes blank cells
L2 = Cells(Rows.Count, "B").End(xlUp).Row 'Just grab the last row with data
'Use 2 IIF Statements to check the length using one line of code and look smart as hell
Answer = IIf(L1 > L2, "List 1 is longer", IIf(L2 > L1, "List 2 is Longer", "Same length"))
MsgBox (Answer)
End Sub
如果您希望通過滿足最低要求 「打動」 你的老師:
Sub LazyHomeworkForNmHomie13()
Index = Int(InputBox("Enter a number from 1 to " & Worksheets.Count))
Sheets(Index).Activate
L1 = Cells(Rows.Count, "A").End(xlUp).Row
L2 = Cells(Rows.Count, "B").End(xlUp).Row
Answer = IIf(L1 > L2, "List 1 is longer", IIf(L2 > L1, "List 2 is Longer", "Same length"))
MsgBox (Answer)
End Sub
**發表您當前的代碼** – 2015-03-31 21:24:29
哪你需要幫助的部分?你需要什麼樣的幫助? – 2015-03-31 21:25:46
你一直在問太多。刪除第2步到第6步並專注於第1步.VBA程序可以通過數字訪問工作表,但用戶不知道這些數字。一種方法是使用工作表名稱填充一個列表框。另一種方法是要求用戶在宏啓動時確認所需的工作表處於活動狀態。研究編碼解決步驟1的宏。如果遇到困難,請發佈錯誤的代碼並解釋發生了什麼問題。一旦這個宏正在工作,研究步驟2. – 2015-03-31 21:49:35