我的代码错误来自
If Dir(Pth, vbArchive) <> vbNullString Then
我找不到错误 - 有人可以帮我解决代码有什么问题吗?是应该说USERPROFILE
,还是我应该写别的东西?
Sub Opgave8()
Dim sh As Worksheet
Dim Pth As String
Application.ScreenUpdating = False
' Create default desktop path using windows user id
user_id = Environ$("USERPROFILE")
' Create full path
file_name$ = "\AdminExport.csv"
Pth = Environ$("USERPROFILE") & "\Desktop\" & FileName
Set sh = Sheets.Add
For i = 2 To 18288
If Left(Worksheets("Base").Cells(i, 12), 6) = "262015" Then
sh.Cells(i, 2) = Worksheets("Base").Cells(i, 4)
End If
Next i
sh.Move
If Dir(Pth, vbArchive) <> vbNullString Then
overwrite_question = MsgBox("File already exist, do you want to overwrite it?", vbYesNo)
End If
If overwrite_question = vbYes Then
With ActiveWorkbook
.SaveAs FileName:=Pth, FileFormat:=xlCSV
.Close False
End With
End If
Application.ScreenUpdating = True
End Sub
Function UniqueRandDigits(x As Long) As String
Dim i As Long
Dim n As Integer
Dim s As String
Do
n = Int(Rnd() * 10)
If InStr(s, n) = 0 Then
s = s & n
i = i + 1
End If
Loop Until i = x + 1
UniqueRandDigits = s
End Function
答案 0 :(得分:0)
您的代码中存在一些问题。我不明白为什么你会收到错误信息,但是如果你解决了问题,你就可以更好地找到主要问题了。
Option Explicit
置于顶部。如果你这样做,你不会犯诸如设置变量file_name$
但是从变量FileName
读取错误。Debug.Print Pth
之前添加If
。按 Ctrl - G 以显示调试窗格并研究输出。打印的文件路径是否存在? vbNullString
。请改为使用abc <> ""
进行测试。