单个文件使程序不稳定

时间:2018-11-28 14:25:15

标签: excel vba excel-vba excel-2016

我正在从“每日价格数据”文件中为股票行情制作单独的每日价格数据文件。以下附加的程序代码无法正常工作。有时它会运行并生成60个文件,然后无法保存文件,随后尽管创建了文件,但仍无法粘贴所需的范围数据。有时甚至在第5个文件之后或第30个文件之后它也可能无法保存。我什至介绍了暂停时间。

     Application.Wait (Now + #12:00:03 AM#)

但是它不能解决问题。程序在以下代码行上任意失败。

 .SaveAs Filename:="C:\Users\kakka\Desktop\SymbolData\" & cell & "-v1.xls", FileFormat:=56

程序代码附在下面。

Sub number()

    Dim wbI As Workbook, wbO As Workbook
    Dim wsI As Worksheet, wsO As Worksheet
    Dim cell, rng As Range
    Dim stRw As Long
    Set rng = Range("A2:A72")
    stRw = 2
    For Each cell In rng
        If cell.Value <> cell.Offset(1, 0).Value Then

        Set wbI = ActiveWorkbook
        Set wsI = wbI.Worksheets("Sheet1")
        Set wbO = Workbooks.Add

            With wbO
                 Set wsO = wbO.Sheets("Sheet1")
                 .SaveAs Filename:="C:\Users\kakka\Desktop\SymbolData\" & cell & "-v1.xls", FileFormat:=56
                 wsI.Range("A1:H1").Copy
                 wsO.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                 wsI.Rows(stRw & ":" & cell.Row).Copy
                 wsO.Range("A2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                .Close SaveChanges:=True
                stRw = cell.Row + 1
            End With
        Set wbI = Nothing
        Set wsI = Nothing
        Set wbO = Nothing
        Set wsO = Nothing



        End If
    Application.Wait (Now + #12:00:03 AM#)
    Next cell


End Sub

Data File Snapshot

Unsaved File Snapshot

Saved File Snapshot 请求解决此问题的帮助。

1 个答案:

答案 0 :(得分:0)

您正在使用范围变体来连接您的String FilePath

使用cell.Value2将单元格值与字符串FilePath连接起来。

.SaveAs Filename:="C:\Users\kakka\Desktop\SymbolData\" & cell.Value2 & "-v1.xls", FileFormat:=56