简体   繁体   English

运行时错误9-下标超出范围

[英]Runtime Error 9 - Subscript Out Of Range

I'm trying to create a form based interface using the VBA. 我正在尝试使用VBA创建基于表单的界面。 I ran into a problem which I cannot figure it out. 我遇到了一个无法解决的问题。 So in my code, I try to create a new workbook then save it. 因此,在我的代码中,我尝试创建一个新的工作簿,然后保存它。 After that it will scan through the checkbox and see which one is getting selected. 之后,它将扫描复选框,并查看正在选择的复选框。 If it get selected, it will create a new sheet and copy the pre-made template. 如果选择它,它将创建一个新的图纸并复制预制的模板。 The user will open another workbook that they want and it will copy the information from that workbook to the workbook that just made. 用户将打开他们想要的另一个工作簿,它将信息从该工作簿复制到刚创建的工作簿。 Here is the code for retrieving the data: 这是用于检索数据的代码:

Sub mil10_data()

Dim NewWB As Workbook
Dim thisWB As Workbook
Dim wb As Workbook
Dim Ret

Set thisWB = ThisWorkbook
Set NewWB = ActiveWorkbook
With NewWB
    'Copy the pre-made template to new workbook
    thisWB.Sheets("Data 10").Range("A1:AZ3000").Copy NewWB.Sheets(ActiveSheet.Name).Range("A1:AZ3000")

    'Retriving the data
    Ret = Application.GetOpenFilename("Excel Files (*.CSV), *.CSV", Title:="Select File To Be Opened")
    If Ret = False Then Exit Sub
    Set wb = Workbooks.Open(Ret)
    **'This is where the error is show up
    wb.Sheets(ActiveSheet.Name).Range("E21:E2136").Copy NewWB.Sheets(ActiveSheet.Name).Range("C2:C2117")** 
    wb.Close SaveChanges:=False

    Set wb = Nothing
    Set NewWB = Nothing
    End With
End Sub

I figure that maybe because there are three workbooks open, it didn't know which one is the active workbook, but that is not the case. 我认为可能是因为有三个工作簿处于打开状态,所以它不知道哪个是活动工作簿,但事实并非如此。 I tested by using the MsgBox and it display the right workbook and worksheeet name that I want. 我使用MsgBox进行了测试,它显示了所需的正确工作簿和workheeet名称。 If I change the ActiveSheet.Name to the actual sheet name, it works, but I don't want to use that method. 如果我将ActiveSheet.Name更改为实际的工作表名称,则可以,但是我不想使用该方法。 I have different worksheets that need to be created, so I prefer using the ActiveSheet.Name. 我需要创建不同的工作表,因此我更喜欢使用ActiveSheet.Name。 Anyone know why it didn't work? 有人知道为什么它不起作用吗? I would really appreciate the help. 我非常感谢您的帮助。 Thank you! 谢谢!

ActiveWorkbook and ActiveSheet are really clumsy ways of getting user input. ActiveWorkbookActiveSheet是获取用户输入的笨拙方法。 You should save in variables the properties that are you interested in (as soon as possible), and then stop referring them directly. 您应该(尽快)将您感兴趣的属性保存在变量中,然后停止直接引用它们。

In your case, the code may look like 就您而言,代码可能看起来像

Sub mil10_data()

    Dim NewWB As Workbook
    Dim thisWB As Workbook
    Dim wb As Workbook
    Dim Ret

    Dim active_sheet_name  As String

    Set thisWB = ThisWorkbook
    Set NewWB = ActiveWorkbook
    Let active_sheet_name = Application.ActiveSheet.Name

    With NewWB
        'Copy the pre-made template to new workbook
        thisWB.Sheets("Data 10").Range("A1:AZ3000").Copy NewWB.Sheets(active_sheet_name).Range("A1:AZ3000")

        'Retrieving the data
        Ret = Application.GetOpenFilename("Excel Files (*.CSV), *.CSV", Title:="Select File To Be Opened")
        If Ret = False Then Exit Sub
        Set wb = Workbooks.Open(Ret)
        wb.Sheets(active_sheet_name).Range("E21:E2136").Copy NewWB.Sheets(active_sheet_name).Range("C2:C2117") 
        wb.Close SaveChanges:=False

        Set wb = Nothing
        Set NewWB = Nothing
    End With
End Sub

If you still get "Subscript out of range" error, that means that you either don't select correctly the ActiveSheet before running the script, or that sheet does not exist in the workbook wb . 如果仍然出现“下标超出范围”错误,则意味着您或者在运行脚本之前未正确选择ActiveSheet ,或者工作表wb中不存在该工作表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM