从输入提示中动态选择工作表并打印到文件

时间:2014-12-08 17:10:40

标签: excel vba excel-vba excel-formula

我的工作簿中有3张表,即sheet1,sheet3和sheet2,当我输入inputpromt中的值时说(例如:1,2)我拆分它并将其存储在数组中它必须动态选择sheet1和sheet2并将值打印到文件。 我已经做了一个伪下面可以任何一个

    Sub example()
Dim Ran As Range
Dim cnt as String

Open "D:\temp\test.txt" For Output As #1
Dim myarray() As String
Dim holder As String
dim ab as Strig
ab="this is sample data"
holder = InputBox("Enter your choice eg:1,2")
myarray = Split(holder, ",")
Dim name, country,birth As String
For i = 1 To UBound(myarray)
If i = 1 Or i = 2 Then
name = Sheets(i).Range("Z12").Value
country= Sheets(i).Range("PQ26").Value
 birth = Sheets(i).Range("ab24").Value
  ab=ab & name & country & birth
    Print #1, ab
End If
Next i
end Sub
输入框中的

****如果我将值设为1,2则必须从工作表1和工作表2中选择值****

1 个答案:

答案 0 :(得分:0)

我猜你正在尝试理解InputBox以及如何拆分字符串如“1,2”并将单独的值作为工作表编号处理。

工作表编号对用户没有任何意义,所以我不相信这是一个好方法。您需要为用户提供一个工作表标签列表,供他们选择。

InputBoxApplication.InputBox。这些是不同的,我也不喜欢。它们来自最早的VB版本,可能最初与MS-DOS的控制台输入直接匹配。除了一个例外,有更好的方法。唯一的例外是输入Application.InputBox范围的能力,我不知道这是一个方便的选择。

在下面的代码中,我使用InputBox。您可能认为我已经过度验证但是如果用户输入“A”怎么办?您的代码将假定“A”是一个工作表编号,但Excel会认为它是一个工作表名称并停止执行下标错误。您必须检查是否有任何错误导致代码因运行时错误而停止。

您应该使用用户表单而不是InputBox。有一些在线教程可以帮助您入门。使用用户表单,您有多种选择,包括:

  • 一个列表框,启用了多个选择,包含每个允许的工作表的名称。
  • 一列单选按钮,每个允许的工作表一个。这些将取消链接,以便用户可以选择几个。
  • 每个允许的工作表的命令按钮,用户可以依次单击该按钮。

所有这些选择都比InputBox用户更友好。

使用 F8 逐步执行以下代码并研究它的作用。如有必要,请回答问题,但是你自己了解的越多,你的发展就越快。

Option Explicit

' Please indent your macros consistently.  This makes them much easier ri read

Sub example()
  Dim Ran As Range
  Dim cnt As String

  Dim myarray() As String
  Dim ab As String

  ' Variables I have added or which are replacements for yours.
  Dim Answer As String
  Dim AllValuesGood As Boolean
  Dim InxMA As Long
  Dim InxSht As Long
  Dim Question As String

  ' I like to keep all my Dim statements together at the top.
  ' This makes them easier to find.
  Dim name, country, birth As String

  ' I have output to the Immediate window which is simpler when
  ' experimenting.
  'Open "D:\temp\test.txt" For Output As #1

  Question = "Enter your choice eg: ""1"" or ""1,2"""

  ' You omitted the code to check the Answer

  Do While True

    Answer = InputBox(Question, "Experiment with Input box")
    ' Have a string but user could have typed anything
    If Answer = "" Then
      ' Cannot tell if user has clicked Cancel or if user clicked Return
      ' before entering value.  Have to assume Cancel
      Debug.Print "User clicked Cancel"
      Exit Sub
    Else
      AllValuesGood = True     ' Assume good answer until find otherwise
      myarray = Split(Answer, ",")
      For InxMA = 0 To UBound(myarray)
        If IsNumeric(myarray(InxMA)) Then
          InxSht = Val(myarray(InxMA))
          If InxSht < 1 Or InxSht > 2 Then
            ' Sheet number outside permitted range
            AllValuesGood = False
            Exit For
          End If
        Else
          ' Non-numeric sheet number
          AllValuesGood = False
          'Debug.Assert False
          Exit For
        End If
      Next
    End If
    If AllValuesGood Then
      ' Have good answer.  Exit Do-Loop to print sheets
      Exit Do
    Else
      ' An invalid sheet number has been found
      Question = "Click cancel to exit or enter ""1"" or ""2"" or ""1, 2"""
      ' Loop to repeat Input
    End If

  Loop  ' Until have good answer

  ' If get here Answer is a list of good sheet number

  For InxMA = 0 To UBound(myarray)
    InxSht = Val(myarray(InxMA))
    ' I have not created sample worksheets with data in Z12, PQ26 and ab24
    ' but this shows the code you need
    'With Worksheets(InxSht)
    '  name = .Range("Z12").Value
    '  country = .Range("PQ26").Value
    '  birth = .Range("ab24").Value
    'Next
    name = "Name" & InxSht
    country = "Country" & InxSht
    birth = "Birth" & InxSht
    ' Have to initialise ab here because need new value per sheet
    ab = "this is sample data"
    ab = ab & name & country & birth
    Debug.Print ab
    ' The value of ab will be messy because you have no spaces between words.
    'Print #1, ab
  Next InxMA

End Sub