VBA代码挂起Excel

时间:2012-11-15 00:24:08

标签: excel vba excel-vba

我编写了一个代码来获取文件中的原始数据,并根据报告的“日期”汇总数据,并根据“日期”值将此汇总数据复制到目标工作簿中。

当我尝试运行此代码时。它适用于一个文件,但挂起在另一个文件中。当我尝试调试它时,我无法遵循代码流程。它突然崩溃了。你能帮我解决这个问题吗?

Option Explicit
Sub file_select()
Dim RequiredFileName As Variant, i As Integer
Dim targetWorkbook As Workbook
' making weak assumption that active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook
'RequiredFileName = "c:\myfiles\test.xls"
On Error GoTo EndNow
RequiredFileName = Application.GetOpenFilename(FileFilter:="ALL Files (*.*), *.*", Title:="Get File", MultiSelect:=True)
For i = 1 To UBound(RequiredFileName)
    MsgBox RequiredFileName(i), , GetFileName(CStr(RequiredFileName(i)))
Next i
For i = 1 To UBound(RequiredFileName)
    Call ProcessOpenFile(RequiredFileName(i), targetWorkbook)
Next i
EndNow: End Sub

Function GetFileName(filespec As String)
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
GetFileName = fso.GetFileName(filespec)
End Function

Sub ProcessOpenFile(RequiredFileName, targetWorkbook As Workbook)
Dim RequiredWorkbook As Workbook
'Dim targetWorkbook As Workbook
' get the required workbook
Set RequiredWorkbook = Application.Workbooks.Open(RequiredFileName)

Dim targetSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets("Summary_NV")
Dim RequiredSheet As Worksheet
Set RequiredSheet = RequiredWorkbook.Sheets(1)        'here assumed that source workbook consists only of one sheet i.e., is the required sheet.
RequiredWorkbook.Sheets.Add After:=Sheets(Sheets.Count)
RequiredWorkbook.Sheets(Sheets.Count).Select
RequiredWorkbook.Sheets(Sheets.Count).Name = "SUMMARY" & Sheets.Count
Call Sort_Before(RequiredWorkbook)        'sorting the required file data according to date.

If RequiredSheet.Name = "EVDO_SC_Summary" Then
    Call ProcessEVDO(RequiredSheet)        'get the summary of report
    Call Sort_After(RequiredWorkbook)        ' sort the summary according to date
    Call DateChange(RequiredWorkbook)        'changing date format
ElseIf RequiredSheet.Name = "CDMAVoice_SC_Summary" Then
    Call ProcessVoice(RequiredSheet)
    Call Sort_After(RequiredWorkbook)
    Call DateChange(RequiredWorkbook)
ElseIf RequiredSheet.Name = "CDMAData_SC_Summary" Then
    Call ProcessData(RequiredSheet)
    Call Sort_After(RequiredWorkbook)
    Call DateChange(RequiredWorkbook)
End If

Dim iRow As Integer
Dim LastRow_Req As Integer
Dim LastRow_Tar As Integer
Dim LastCol_Req As Integer
LastRow_Req = RequiredWorkbook.Sheets(Sheets.Count).Cells(Rows.Count, 1).End(xlUp).Row        'last row summary data
LastCol_Req = RequiredWorkbook.Sheets(Sheets.Count).Cells(1, Columns.Count).End(xlToLeft).Column        'last column of summary data
LastRow_Tar = targetSheet.Cells(Rows.Count, 1).End(xlUp).Row        'last row of target sheet used
RequiredWorkbook.Sheets(Sheets.Count).Range("B1").Resize(LastRow_Req, LastCol_Req - 1).Select        'selecting summary data for copying
Selection.Copy

If targetSheet.Cells(LastRow_Tar, 1).Value < RequiredWorkbook.Sheets(Sheets.Count).Range("A1").Value Then        'if date entered in target sheet last cell is less
    If RequiredWorkbook.Sheets(1).Name = "EVDO_SC_Summary" Then        'then the summary report date
        targetSheet.Activate
        Cells(LastRow_Tar + 1, 16).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                                                      :=False, Transpose:=False
        Cells(LastRow_Tar + 1, 1).Select
        Call Date_update(RequiredWorkbook, targetWorkbook, LastRow_Tar + 1, 1)
    ElseIf RequiredWorkbook.Sheets(1).Name = "CDMAVoice_SC_Summary" Then
        targetSheet.Activate
        Cells(LastRow_Tar + 1, 2).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                                                      :=False, Transpose:=False
        Cells(LastRow_Tar + 1, 1).Select
        Call Date_update(RequiredWorkbook, targetWorkbook, LastRow_Tar + 1, 1)
    ElseIf RequiredWorkbook.Sheets(1).Name = "CDMAData_SC_Summary" Then
        targetSheet.Activate
        Cells(LastRow_Tar + 1, 9).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                                                      :=False, Transpose:=False
        Cells(LastRow_Tar + 1, 1).Select
        Call Date_update(RequiredWorkbook, targetWorkbook, LastRow_Tar + 1, 1)
    End If
End If

For iRow = targetSheet.Range("A12").Row To LastRow_Tar
    RequiredWorkbook.Activate
    If targetSheet.Cells(iRow, 1).Value < RequiredWorkbook.Sheets(Sheets.Count).Range("A1").Value Then
        GoTo A
    ElseIf targetSheet.Cells(iRow, 1).Value = RequiredWorkbook.Sheets(Sheets.Count).Range("A1").Value Then
        If RequiredWorkbook.Sheets(1).Name = "EVDO_SC_Summary" Then
            targetSheet.Activate
            Cells(iRow, 16).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                                                          :=False, Transpose:=False
            Cells(iRow, 1).Select
            Call Date_update(RequiredWorkbook, targetWorkbook, iRow, 1)
            Exit For
        ElseIf RequiredWorkbook.Sheets(1).Name = "CDMAVoice_SC_Summary" Then
            targetSheet.Activate
            Cells(iRow, 2).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                                                          :=False, Transpose:=False
            Cells(iRow, 1).Select
            Call Date_update(RequiredWorkbook, targetWorkbook, iRow, 1)
            Exit For
        ElseIf RequiredWorkbook.Sheets(1).Name = "CDMAData_SC_Summary" Then
            targetSheet.Activate
            Cells(iRow, 9).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                                                          :=False, Transpose:=False
            Cells(iRow, 1).Select
            Call Date_update(RequiredWorkbook, targetWorkbook, iRow, 1)
            Exit For
        End If
    ElseIf targetSheet.Cells(iRow, 1).Value > RequiredWorkbook.Sheets(Sheets.Count).Range("A1").Value Then
        If RequiredWorkbook.Sheets(1).Name = "EVDO_SC_Summary" Then
            targetSheet.Activate
            Cells(iRow, 16).Select
            Selection.Insert Shift:=xlDown
            Exit For
            Cells(iRow, 1).Select
            Call Date_update(RequiredWorkbook, targetWorkbook, iRow, 1)
        ElseIf RequiredWorkbook.Sheets(1).Name = "CDMAVoice_SC_Summary" Then
            targetSheet.Activate
            Cells(iRow, 2).Select
            Selection.Insert Shift:=xlDown
            Cells(iRow, 1).Select
            Call Date_update(RequiredWorkbook, targetWorkbook, iRow, 1)
            Exit For
        ElseIf RequiredWorkbook.Sheets(1).Name = "CDMAData_SC_Summary" Then
            targetSheet.Activate
            Cells(iRow, 9).Select
            Selection.Insert Shift:=xlDown
            Cells(iRow, 1).Select
            Call Date_update(RequiredWorkbook, targetWorkbook, iRow, 1)
            Exit For
        End If
    End If
A:     Next
RequiredWorkbook.Close savechanges:=False
End Sub

1 个答案:

答案 0 :(得分:0)

在评论中回答&#34;代码进入无限循环&#34; - user1806794