Vba 从Excel中的单元格打开文件夹,如果文件夹不存在,则创建文件夹
我正在尝试在电子表格中创建一个单元格,该单元格将执行以下操作:Vba 从Excel中的单元格打开文件夹,如果文件夹不存在,则创建文件夹,vba,excel,batch-file,Vba,Excel,Batch File,我正在尝试在电子表格中创建一个单元格,该单元格将执行以下操作: 根据行中两个单元格的值打开文件夹 如果文件夹不存在,则从以上两个单元格创建该文件夹 仅当我单击该行时才有效 目前,我正在使用超链接公式链接到手动创建的文件夹。我有一个聪明的想法,就是链接到一个批处理文件,该批处理文件通过解析数据打开/创建文件夹。我尝试了很长一段时间,但未能将数据从excel获取到批处理文件 不管怎样,你想这么做吗?用我描述的方式还是用VBA 我实际的电子表格有更多的列和行,但希望下面的图片能说明我希望文件夹链接的布
=HYPERLINK("C:\New folder\new.bat "&B2&" "&C2,"Open")
我收到“无法打开指定文件”错误。如果我删除单元格数据,它将打开程序,但没有数据,我无法创建必要的文件夹
下面是我为打开/创建文件夹而编写的批处理文件:
@echo off
set dir="C:\New folder\%1\%2"
if not exist %dir% mkdir %dir%
start "" %dir%
当使用以下命令行运行时,它本身可以正常工作:
new.bat 2015 Folder 0001
任何指导或帮助都将不胜感激。提前谢谢。我不知道你的牢房里有什么。但是您可以从如下值构建路径
Dim ws As Excel.Worksheet
Set ws = ActiveWorkbook.Sheets("Sheet1")
Dim szPath as string
szPath = ws.Range("B1").Value & "\" & ws.Range("C1").Value
Dim fs As FileSystemObject
Set fs = New FileSystemObject
'Create a text file
Set ts = fs.CreateTextFile("C:\Temp\test.txt", True, False)
'or using the path from the cells
Set ts = fs.CreateTextFile(szPath & "\text.txt", True, False)
打开文件夹要打开文件夹,可以使用shellexecute。在所有代码的顶部声明这一点。首先是子系统和功能
Private Declare Function ShellExecute Lib "Shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
然后你可以给它发送一条路径
ShellExecute 0, "open", "C:\Temp", 0, 0, 1
'or send it the value build from the cells
ShellExecute 0, "open", szPath , 0, 0, 1
您也可以通过这种方式打开文件
创建文件如果你想创建文件,可以这样做
Dim ws As Excel.Worksheet
Set ws = ActiveWorkbook.Sheets("Sheet1")
Dim szPath as string
szPath = ws.Range("B1").Value & "\" & ws.Range("C1").Value
Dim fs As FileSystemObject
Set fs = New FileSystemObject
'Create a text file
Set ts = fs.CreateTextFile("C:\Temp\test.txt", True, False)
'or using the path from the cells
Set ts = fs.CreateTextFile(szPath & "\text.txt", True, False)
创建文件夹最后,您可以创建文件夹
fs.CreateFolder ("C:\Temp\SomeNewDir")
'or using the path from the cells
fs.CreateFolder (szPath & "\SomeNewDir")
我不知道你的牢房里有什么。但是您可以从如下值构建路径
Dim ws As Excel.Worksheet
Set ws = ActiveWorkbook.Sheets("Sheet1")
Dim szPath as string
szPath = ws.Range("B1").Value & "\" & ws.Range("C1").Value
Dim fs As FileSystemObject
Set fs = New FileSystemObject
'Create a text file
Set ts = fs.CreateTextFile("C:\Temp\test.txt", True, False)
'or using the path from the cells
Set ts = fs.CreateTextFile(szPath & "\text.txt", True, False)
打开文件夹要打开文件夹,可以使用shellexecute。在所有代码的顶部声明这一点。首先是子系统和功能
Private Declare Function ShellExecute Lib "Shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
然后你可以给它发送一条路径
ShellExecute 0, "open", "C:\Temp", 0, 0, 1
'or send it the value build from the cells
ShellExecute 0, "open", szPath , 0, 0, 1
您也可以通过这种方式打开文件
创建文件如果你想创建文件,可以这样做
Dim ws As Excel.Worksheet
Set ws = ActiveWorkbook.Sheets("Sheet1")
Dim szPath as string
szPath = ws.Range("B1").Value & "\" & ws.Range("C1").Value
Dim fs As FileSystemObject
Set fs = New FileSystemObject
'Create a text file
Set ts = fs.CreateTextFile("C:\Temp\test.txt", True, False)
'or using the path from the cells
Set ts = fs.CreateTextFile(szPath & "\text.txt", True, False)
创建文件夹最后,您可以创建文件夹
fs.CreateFolder ("C:\Temp\SomeNewDir")
'or using the path from the cells
fs.CreateFolder (szPath & "\SomeNewDir")
最好的方法可能是使用VBA,而不使用shell脚本。 假设你的三个单元格是A1、A2和A3。您可以向工作表中添加按钮并指定如下宏:
Sub btn1_click()
Dim dir as String
Dim fso As Object
dir = Range("A1").Value & "\" & Range("A2").Value & Range("A3").Value
Set fso = CreateObject("scripting.filesystemobject")
If Not fso.folderexists(dir) Then
fso.createfolder (dir)
End If
Call Shell("explorer.exe" & " " & dir, vbNormalFocus)
End Sub
最好的方法可能是使用VBA,而不使用shell脚本。 假设你的三个单元格是A1、A2和A3。您可以向工作表中添加按钮并指定如下宏:
Sub btn1_click()
Dim dir as String
Dim fso As Object
dir = Range("A1").Value & "\" & Range("A2").Value & Range("A3").Value
Set fso = CreateObject("scripting.filesystemobject")
If Not fso.folderexists(dir) Then
fso.createfolder (dir)
End If
Call Shell("explorer.exe" & " " & dir, vbNormalFocus)
End Sub
我猜
C:\New folder\New.bat中的空格会带来麻烦;对于不包含任何空间的文件夹,请尝试相同操作…新文件夹中的空间不是问题。我可以按您键入的方式运行它,但在尝试从单元格添加信息(即new.bat 2015 folder 001)时无法运行。谢谢。我猜C:\New folder\New.bat中的空格会带来麻烦;对于不包含任何空间的文件夹,请尝试相同操作…新文件夹中的空间不是问题。我可以按您键入的方式运行它,但在尝试从单元格添加信息(即new.bat 2015 folder 001)时无法运行。谢谢