2015-03-31 93 views
-4

需要帮助您不要这样做请:Excel的VBA编程范围内选择问题

  1. 打开一个输入框,提示用户可以选择从1工作表数
  2. 存储值的用户中的一个变量进入一个名为Index的整数。 您可能会假定用户将始终输入有效的输入(即1,2或3)。例如,如果用户输入文本, 程序将发生运行时错误 - 但您不需要编写代码来处理此情况。
  3. 使用用户输入的索引编号和工作表()集合对象 来激活选定的工作表。
  4. 查找列A中的项目数(从单元格A1开始),并将其存储在名为L1的变量中;将其存储在 中;在列B中找到列表中的项目数(单元格B1中的 开始),并将其存储在名为L2的变量中。
  5. 使用If语句确定要存储在名为 的变量中的字符串答案: 如果列A中的项目数较大,则“列表1更长”。 如果列B中的项目数量较大,则“列表2更长”,否则“ ”相同长度。
  6. 打开显示答案的消息框。
+0

**发表您当前的代码** – 2015-03-31 21:24:29

+0

哪你需要帮助的部分?你需要什么样的帮助? – 2015-03-31 21:25:46

+0

你一直在问太多。删除第2步到第6步并专注于第1步.VBA程序可以通过数字访问工作表,但用户不知道这些数字。一种方法是使用工作表名称填充一个列表框。另一种方法是要求用户在宏启动时确认所需的工作表处于活动状态。研究编码解决步骤1的宏。如果遇到困难,请发布错误的代码并解释发生了什么问题。一旦这个宏正在工作,研究步骤2. – 2015-03-31 21:49:35

回答

1

嗯......

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 
+1

我将假设这一切作品和upvote只是为了评论:-) – 2015-04-01 04:12:08

+1

谢谢你。这有所帮助。我用它来比较我的合作伙伴的代码实际上哈哈。谢谢。 – nmhomie13 2015-04-02 21:36:03