0

在这里我只想要它,以便如果单击另一个复选框,它将运行 Else 块中的代码。Duplicate Declaration in Current Scope代码运行得很好,只有 If Choice1 is True 块,但是当我添加 Else 块时会出现错误:

Private Sub CommandButton1_Click()

    If Choice1.Value = True Then
    

        Dim filename As String
        filename = ThisWorkbook.Path & Application.PathSeparator & "somefile.xlsm"
    
        Dim wk As Workbook
        Set wk = Workbooks.Open(filename, ReadOnly:=True)
    
        Dim rgSource As Range, rgDestination As Range
    
        ' set rgsource = [workbook].[worksheet].[range]
        Set rgSource = ThisWorkbook**strong text**.Worksheets("Source").**Range("A1:B7")**
        Set rgDestination = ThisWorkbook.Worksheets("Destination").Range("A1")
    
        rgSource.Copy
        rgDestination.PasteSpecial xlPasteValues
    
        wk.Close saveChanges:=False
        
    ElseIf Choice2.Value = True Then

        Dim filename As String
        filename = ThisWorkbook.Path & Application.PathSeparator & "Otherfile.xlsm"

        Dim wk As Workbook
        Set wk = Workbooks.Open(filename, ReadOnly:=True)

        Dim rgSource As Range, rgDestination As Range

        ' set rgsource = [workbook].[worksheet].[range]
        Set rgSource = wk.Worksheets("Sheet1").**Range("E1:F7")**
        Set rgDestination = ThisWorkbook.Worksheets("Destination").Range("A1")

        rgSource.Copy
        rgDestination.PasteSpecial xlPasteValues

        wk.Close saveChanges:=False

    ElseIf Choice3.Value = True Then ...etc.

    
    End If

End Sub
4

3 回答 3

1

在代码开始时声明所有变量。您可以在任何地方声明,但最好先声明变量。试试下面的子。

Private Sub CommandButton1_Click()
Dim filename As String
Dim wk As Workbook
Dim rgSource As Range, rgDestination As Range
    
    If Choice1.Value = True Then
            filename = ThisWorkbook.Path & Application.PathSeparator & "somefile.xlsm"
        Set wk = Workbooks.Open(filename, ReadOnly:=True)
        ' set rgsource = [workbook].[worksheet].[range]
        Set rgSource = ThisWorkboo.Worksheets("Source").Range("A1:B7")
        Set rgDestination = ThisWorkbook.Worksheets("Destination").Range("A1")
            rgSource.Copy
            rgDestination.PasteSpecial xlPasteValues
            wk.Close saveChanges:=False
    ElseIf Choice2.Value = True Then
            filename = ThisWorkbook.Path & Application.PathSeparator & "Otherfile.xlsm"
        Set wk = Workbooks.Open(filename, ReadOnly:=True)
        ' set rgsource = [workbook].[worksheet].[range]
        Set rgSource = wk.Worksheets("Sheet1").Range("E1:F7")
        Set rgDestination = ThisWorkbook.Worksheets("Destination").Range("A1")
            rgSource.Copy
            rgDestination.PasteSpecial xlPasteValues
            wk.Close saveChanges:=False
    ElseIf Choice3.Value = True Then
        DoEvents
    End If

End Sub
于 2021-04-16T06:23:14.610 回答
0

您只需要声明一次变量,因此您可以像这样将它放在上面:

Private Sub CommandButton1_Click()
    
    Dim filename As String
    Dim wk As Workbook
    Dim rgSource As Range, rgDestination As Range
    
    If Choice1.Value = True Then
            
        filename = ThisWorkbook.Path & Application.PathSeparator & "somefile.xlsm"
        
        Set wk = Workbooks.Open(filename, ReadOnly:=True)
    
        ' set rgsource = [workbook].[worksheet].[range]
        Set rgSource = ThisWorkbook.Worksheets("Source").Range("A1:B7")
        Set rgDestination = ThisWorkbook.Worksheets("Destination").Range("A1")
    
        rgSource.Copy
        rgDestination.PasteSpecial xlPasteValues
    
        wk.Close saveChanges:=False
        
    ElseIf Choice2.Value = True Then
            
        filename = ThisWorkbook.Path & Application.PathSeparator & "Otherfile.xlsm"
    
        Set wk = Workbooks.Open(filename, ReadOnly:=True)

        ' set rgsource = [workbook].[worksheet].[range]
        Set rgSource = wk.Worksheets("Sheet1").Range("E1:F7")
        Set rgDestination = ThisWorkbook.Worksheets("Destination").Range("A1")

        rgSource.Copy
        rgDestination.PasteSpecial xlPasteValues

        wk.Close saveChanges:=False

    ElseIf Choice3.Value = True Then ...etc.

    
    End If

End Sub
于 2021-04-16T06:19:10.837 回答
0

如果您不使用Option Explicit,您可以安全地省略声明并摆脱问题:)

但这不是好的做法,只是其他答案的替代品。

于 2021-04-16T06:26:02.220 回答