excel - 创建文件夹并将文件保存到共享点站点的宏
问题描述
我有一个公司共享点站点,我和同事需要在该站点上加载年度文件夹、一个月子文件夹和一周结束子文件夹中的每日工作表。我正在寻找一种方法来根据需要自动创建文件夹,而无需连续创建它们。谢谢
我已经尝试如下,但它出现了运行时错误 52。
Private Sub CommandButton5_Click()
Dim sFFname As String
Dim sFname As String
Dim sName As String
Dim sFolder As String
Dim sSub As String
sFolder = Range("B69").Value
sSub = Range("B70").Value
sName = Range("B71").Value
sFname = Range("B72").Value
sFFname = Range("B73").Value
'Define Folder Path for year folder
sFolderPath = sFolder
'Check Specified Folder exists or not
If Dir(sFolderPath, vbDirectory) = "" Then
'If folder is not available
MkDir (sFolderPath)
'Display Message
MsgBox "Year folder created successfully!", vbInformation, "VBAF1"
Else
'If folder is available
MsgBox "Year Folder already exists!", vbInformation, "VBAF1"
End If
'Variable declaration
'Define Folder Path for Month folder
sFolderPath = sSub
'Check Specified Folder exists or not
If Dir(sFolderPath, vbDirectory) = "" Then
'If folder is not available
MkDir (sFolderPath)
'Display Message
MsgBox "Month folder created successfully!", vbInformation, "VBAF1"
Else
'If folder is available
MsgBox "Month Folder already exists!", vbInformation, "VBAF1"
End If
'Variable declaration
'Define Folder Path for Week Ending Folder
sFolderPath = sName
'Check Specified Folder exists or not
If Dir(sFolderPath, vbDirectory) = "" Then
'If folder is not available
MkDir (sFolderPath)
'Display Message
MsgBox "Week Ending folder created successfully!", vbInformation, "VBAF1"
Else
'If folder is available
MsgBox "Week Ending folder already exists!", vbInformation, "VBAF1"
End If
'Set up file to be saved
Dim filename As String ''''filename is the sharepoint site and file name
DirFile = sFFname
If Len(Dir(sFFname)) = 0 Then
'If File exists
MsgBox "File already exists! Open exizting file to edit", vbInformation, "VBAF1"
Else
'Insert commands to Save File below
Folder = Range("B72").Value
Name = Range("H73").Value
ActiveWorkbook.SaveAs filename:= _
"Folder & " / " & Name & """
FileFormat = xlOpenXMLWorkbookMacroEnabled
MsgBox "File saved", vbInformation, "VBAF1"
End If
'Display Message
MsgBox "Yipeeee!", vbInformation, "VBAF1"
End Sub
单元格引用的格式为: \mycompany.sharepoint.com\sites\mysite\folder\subfolder
开头有 2 个斜杠 - 由于某种原因未在问题中显示
我尝试了文件夹名称和 %20 中有空格的空格,以及将 \ 反转为 / 以及两者的组合。
错误从第一个 If Dir(sFolderPath, vbDirectory) = "" Then
将鼠标悬停在 sFolderPath 上会显示来自单元格的链接
是格式问题还是代码问题?任何帮助表示赞赏。
解决方案
推荐阅读
- google-apps-script - Google App Script - TypeError:无法读取未定义的属性“getResponse”
- signalr - SignalR 客户端断开连接
- reactjs - 选择上的 forwardRef 不会使用 react-hook-form 保存选择的值
- python - 如何将 python 包安装到 monorepo 内的 docker 服务中
- python - 如何以这种方式合并具有不相同列的两个数据框?Python
- ruby-on-rails - 通过隐藏字段或默认表单值从模型传递数据
- pyqt4 - PyQt4:找不到模块错误
- c# - 在 Powershell 脚本中将 args 数组传递给 C# Main
- powershell - PowerShell:如果 B 列值为“X”,则使用前一列 A 行数据中的数据填充 A 列空行
- javascript - 数据更改后停止执行函数(React、useEffect)