Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2184|回复: 2

下标越界的问题

[复制链接]
发表于 2013-8-5 11:27 | 显示全部楼层 |阅读模式
小弟对VBA完全不懂,在运行一个程序时,出现"运行错误9,下标越界”的提示,我点击“调试”之后,出现下面的东西,请各位行家看看,问题出现在那里,该如何解决。(下面标黄的那一行,是我点击“调试”之后出来的)。
我运行的这个程序需要从两个excel文件中读取数据,然后生成一个Result文件。读取数据的两个excel文件中,一个为一个矩阵,有211行,211列;另一个文件为两列数据,前两行为一些说明的文字(运行程序时用不到),第三行为数据的标题,共4个标题,从第四行开始为数据,共4列117行数据。
我对VBA丝毫不懂,从网上下载了这个程序来使用,好像数据量大了的时候,就出现这个问题,再次请大家帮忙看看,万分感激!
Public OuvrirFichierCou As String
Public OuvrirFichierGroupes As String
Public LaMatrice As MatriceCouple
Public NomFichierSortieCou As String
Public ReconstitutionCou As String
Public StructCouple As Structure
Public ChoixStat As String
Public ChoixStatbis As String
Private Reconstitution As String
Private Chemin As Variant
Private OuvertureFichier1 As Boolean
Private Resultat As Boolean
Private ControleurS1 As ControleSaisieTexte
Private Wb As Workbook
Private Feuille As Worksheet
Private CompteurSlash As Integer

Private Sub CommandButton1_Click()
'routine pour le premier bouton "parcourir"
ChDir ("C:\")
    OuvrirFichierCou = Application.GetOpenFilename(filefilter:="Classeur Microsoft Excel (*.xls),*.xls")
    TextBox1.Text = OuvrirFichierCou
    OuvertureFichier1 = True
    Set ControleurS1 = New ControleSaisieTexte
    If OuvrirFichierCou = "Faux" Then
        OuvertureFichier1 = False
        Resultat = True
        Resultat = ControleurS1.ControleS(Resultat, "un nom de fichier correct", TextBox1)
    End If
End Sub

Private Sub CommandButton5_Click()
'routine pour le second bouton "parcourir"
'recuperation du chemin du premier fichier pour que lors de l'ouverture du second
    'fichier, l'explorateur soit deja dans le bon dossier
    If OuvertureFichier1 = True Then
        CompteurSlash = 0
        Reconstitution = ""
        'decoupage du premier chemin au niveau des \
        Chemin = Split(OuvrirFichierCou, "\")
        For Each i In Chemin
            CompteurSlash = CompteurSlash + 1
        Next i
        For j = 0 To CompteurSlash - 2
            'recuperation du chemin precedent
            Reconstitution = Reconstitution + Chemin(j) + "\"
        Next j
        'ouverture au bon endroit
        ChDir (Reconstitution)
        OuvrirFichierGroupes = Application.GetOpenFilename(filefilter:="Classeur Microsoft Excel (*.xls),*.xls")
        TextBox4.Text = OuvrirFichierGroupes
        'instanciation de la classe ControleS pour verifier si la saisie du fichier est correcte
        If OuvrirFichierGroupes = "Faux" Then
            Resultat = True
            Resultat = ControleurS.ControleS(Resultat, "un nom de fichier correct", TextBox2)
        End If
    Else
        MsgBox "Please select a matrix file"
    End If

End Sub

Sub DemarrageU14()
    Call UserForm14_Initialize
End Sub

Sub UserForm14_Initialize()
    TextBox3.Value = "Results"
End Sub


Private Sub CommandButton3_Click()
    If TextBox1 = "Faux" Or TextBox1 = "" Then
        MsgBox "You must select a matrix file"
    ElseIf TextBox4.Text = "" Then
        MsgBox "You must select a groups file"
    ElseIf TextBox3.Text = "" Then
        MsgBox "You must choose a name for the output file"
    Else
        Set LaMatrice = New MatriceCouple
        LaMatrice.NomFichier1 = OuvrirFichierCou
        LaMatrice.CreationMatrice
        For Each Wb In Workbooks
            If Wb.Name = NomFichierSortieCou & ".xls" Then
                Application.DisplayAlerts = False
                Workbooks(NomFichierSortieCou & ".xls").Close
                Application.DisplayAlerts = True
            End If
        Next Wb
        Workbooks.Add
        ActiveWorkbook.SaveAs NomFichierSortieCou & ".xls"
        Sheets.Add
        ActiveSheet.Name = "Cache"
        For Each Feuille In Worksheets
            If Feuille.Name <> "Cache" Then
                Application.DisplayAlerts = False
                Feuille.Delete
                Application.DisplayAlerts = True
            End If
        Next Feuille
        Sheets.Add
        ActiveSheet.Name = "Graph"
        Sheets.Add
        ActiveSheet.Name = "P Value"
        Sheets.Add
        ActiveSheet.Name = "Parameters"
        Set cell = Range("a1")
        cell.Value = "Analysis of pairwise relationship Sxy among pairs"
        Range("a1").Select
        Selection.Font.Bold = True
        Set cell = Range("a3")
        cell.Value = "Matrix file : " & OuvrirFichierCou
        Set cell = Range("a4")
        cell.Value = "Pairs/Couples file : " & OuvrirFichierGroupes
        Workbooks.Open (OuvrirFichierCou)
        'cr閍tion de la structure pour le fichier
        Set StructCouple = New Structure
        StructCouple.TypeStructure = 3
        StructCouple.NomFichier2 = OuvrirFichierGroupes
       StructCouple.CreationStructure

        UserForm14.Hide
        Call UserForm16.DemarrageU16
        UserForm16.Show vbModeless
    End If

End Sub

Private Sub CommandButton4_Click()
    UserForm14.Hide
    Call UserForm.DemarrageU
    UserForm.Show
End Sub

Private Sub TextBox3_Change()
    'recuperation du nom de fichier saisi par l'utilisateur
    NomFichierSortieCou = TextBox3.Text
End Sub


excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2013-8-5 17:14 | 显示全部楼层
没见过Structure是引用的什么类对象,出现下标越界,就表示这个对象在集合内不存在,请核查声明对象的合法性

回复

使用道具 举报

 楼主| 发表于 2013-8-6 07:18 | 显示全部楼层
兰色幻想 发表于 2013-8-5 17:14
没见过Structure是引用的什么类对象,出现下标越界,就表示这个对象在集合内不存在,请核查声明对象的合法性 ...

抱歉俺VBA一窍不通,可否指点从哪里核查对象的合法性?感谢!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-6-6 10:34 , Processed in 0.207519 second(s), 12 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表