Excel VBA无法保存文件

时间:2019-04-05 10:29:19

标签: excel vba

这是我的代码,但是保存文件时遇到问题。

Private o As Integer

Public Sub Procedure1()
   o = 1
End Sub
Sub dural()
Location = Sheets("uitleg").Range("A2").Value
Totallocation = Location & "\"
Debug.Print (Totallocation)
checkNum = Sheets("FunBelgium").Range("A1").Value
Debug.Print (checkNum)
lRow = Range("A65536").End(xlUp).Row 'finds last row, assumes contiguous data
j = 1 'first row for your output

For i = 1 To lRow
    If Range("A" & i).Value = checkNum Then

        Sheets("Output").Range("A" & j & ":N" & j).Value = Sheets("FunBelgium").Range("A" & i & ":N" & i).Value

        Rows([i]).EntireRow.Delete

        j = j + 1 'advances output row counter if checkNum is found

    End If
Next
    o = o + 1
 Application.ScreenUpdating = True

        Sheets("Export").Activate
        ActiveSheet.Copy
        Thisfile = ActiveSheet.Range("J2").Value
        With ActiveSheet.UsedRange
            .Copy
            .PasteSpecial xlValues
            .PasteSpecial xlFormats
        End With
        Application.CutCopyMode = False

        ActiveWorkbook.SaveAs Filename:=Totallocation & Thisfile, FileFormat:=xlCSV, CreateBackup:=True
        'ActiveSheet.SaveAs Filename:="C:\Users\sgouman\Downloads\Bernd\" & Thisfile & ".csv"

        Application.ScreenUpdating = True

        ActiveWorkbook.Close
        Sheets("FunBelgium").Activate
        If Cells(1, 1) = "" Then
            Sheets("Output").Cells.ClearContents
            Exit Sub
        Else:
            Debug.Print ("niet leeg")
            Sheets("Output").Cells.ClearContents
            j = 0
            Call dural
        End If
End Sub

因此,用于总定位的调试是这样的: C:\ Users \ sgouman \ Downloads \ Bernd \

但是出现1004错误。如果我只是手动复制到activeworkbook.saveas中的该文件位置,则效果很好。

1 个答案:

答案 0 :(得分:0)

我相信这是问题所在

with open(file) as fo:
    lines = fo.readlines()
    for i, line in enumerate(lines):
        if r'[NON-UNIFORM LOADS]' in line:
            results['non_uniform_loads'] = {}
            count = 0
            subline = lines[count]
            while '[' not in subline:
                count=count+1
            """
            results['non_uniform_loads'] = {'X':[], 'Z':[]}
            coordinates_count = int(lines[i+4].strip().split()[0])
            for j in range(coordinates_count):
                X_coord = float(lines[i+5+j].strip().split()[0])
                Z_coord = float(lines[i+5+j].strip().split()[1])
                results['non_uniform_loads']['X'].append(X_coord)
                results['non_uniform_loads']['Z'].append(Z_coord)
            """

请替换为:

Totallocation = Location & "\"

原因:“ \”字符是一个特殊字符,为下一个赋予了另一种含义(例如,“ \ n”是换行符,“ \ t”是制表符,...)。

我在PC上试过了,一切正常,请调试您的VBA代码到“保存”行,然后在立即窗口中启动以下命令,让我们知道结果:

Totallocation = Location & "\\"

这应该显示文件名。

顺便问一下,您确定目录不是只读的吗?您甚至确定这是引起问题的线路吗?