vba 取消选择或取消选择工作表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21642911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:49:27  来源:igfitidea点击:

Unselect or Deselect Worksheet

excelexcel-vbavba

提问by user3286479

I have a workbook with x worksheets. And a button (commandbutton1) in Sheet1 which opens a userform with two checkboxes and a Preview Button to choose between 2 sheets to preview in PDF.

我有一个包含 x 个工作表的工作簿。以及 Sheet1 中的一个按钮 (commandbutton1),它打开一个带有两个复选框的用户窗体和一个预览按钮,以在 2 张工作表之间进行选择以在 PDF 中进行预览。

Here's my Preview button code.

这是我的预览按钮代码。

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub CommandButton1_Click()
    Dim i As Integer
    Dim CheckBoxName As String

    For i = 1 To 2
        CheckBoxName = "CheckBox" & i
        If Me.Controls(CheckBoxName).Value = True Then
            Sheets(Me.Controls(CheckBoxName).Caption).Select (False)
        End If
    Next i

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:="D:\Preview.pdf"

    Unload Me

    Dim File As String
    File = "D:\Preview.pdf"
    ShellExecute 0, "Open", File, "", "", vbNormalNoFocus
End Sub

Now, the problem is that Sheet1 is always included in the preview file since it's the sheet containing commandbutton1.

现在,问题是 Sheet1 总是包含在预览文件中,因为它是包含 commandbutton1 的工作表。

So is there a way to deselect sheet1 before exporting sheets 6 & 21 ??

那么有没有办法在导出工作表 6 和 21 之前取消选择工作表 1 ?

I also tried another way but it always stuck at this line (Sheets(Array(SheetNames)).Select)

我也尝试了另一种方式,但它总是停留在这一行 (Sheets(Array(SheetNames)).Select)

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub CommandButton1_Click()
    Dim i As Integer
    Dim CheckBoxName As String
    Dim SheetNames As String

    For i = 1 To 2
        CheckBoxName = "CheckBox" & i
        If Me.Controls(CheckBoxName).Value = True Then
            SheetNames = SheetNames & Me.Controls(CheckBoxName).Caption & ","
        End If
    Next i

    SheetNames = Mid(SheetNames, 1, Len(SheetNames) - 1)

    Sheets(Array(SheetNames)).Select

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:="D:\Preview.pdf"

    Unload Me

    Dim File As String
    File = "D:\Preview.pdf"

    ShellExecute 0, "Open", File, "", "", vbNormalNoFocus
End Sub

Any help with any of these please ??

请问有什么帮助吗??

Thank u in advance.

提前谢谢你。

采纳答案by Siddharth Rout

The trick is how you pass the parameter Falsein

关键是如何传递的参数False

Sheets(Me.Controls(CheckBoxName).Caption).Select (False)

Is this what you are trying?

这是你正在尝试的吗?

Private Sub CommandButton1_Click()
    Dim i As Integer, j As Integer

    For i = 1 To 6
        CheckBoxName = "CheckBox" & i
        If Me.Controls(CheckBoxName).Value = True Then
            j = j + 1
            Worksheets(Me.Controls(CheckBoxName).Caption).Select (j = 1)
        End If
    Next
    If j = 0 Then Exit Sub

    '
    '~~> Rest of your code
    '
End Sub

Before:

前:

enter image description here

在此处输入图片说明

After:

后:

enter image description here

在此处输入图片说明

OR

或者

enter image description here

在此处输入图片说明