Excel VBA中单行中的多个OFFSET函数不起作用 – 为什么?

简单地说,我试图得到什么看起来像一个相当简单的macros在Excel中工作。 它需要在Mac和PC上同样运行,但是我需要遍历列A中的所有行,然后为格式中的每一行创build一个文件夹:

我知道还有更多的事情可以做(检查文件夹是否已经存在,等等),但目前我只想让mkdir线路正常工作。 有人能帮忙吗? 谢谢。

A栏文本B栏文本 – C栏文本

Sub CreateDirs() Dim R As Range Dim RootFolder As String RootFolder = ThisWorkbook.path For Each R In Range("A7:A64000") If Len(R.Text) > 0 Then On Error Resume Next MkDir RootFolder & Application.PathSeparator & R.Text & " " & R.Offset(0, 1).Text & " - " & R.Offset(0, 2).Text On Error GoTo 0 End If Next R End Sub 

在这里testing时,你的代码在Windows下执行得很好。 但是,由于您还需要在Mac上执行,因此您可能要离开MkDir。 你也想检查文件夹是否存在。

Ron de Bruin在他的网站上提供了完整的问题答案: http : //www.rondebruin.nl/mac/mac010.htm

他的代码发表在上面的链接上:

 Sub MakeFolderTest1() 'Make folder on the Desktop MakeFolderIfNotExist (MacScript("return (path to desktop folder) as string") & "TestFolder1") End Sub Sub MakeFolderTest2() 'Add folder in the same path as your workbook with this code MakeFolderIfNotExist (ThisWorkbook.Path & Application.PathSeparator & "TestFolder2") End Sub 'Change the path of the two macro below before you test them Sub MakeFolderTest3() 'Enter the complete path MakeFolderIfNotExist ("YosemiteLacie256:Users:rondebruin:Desktop:TestFolder3") End Sub Sub MakeFolderTest4() 'Do not include the harddisk name if you use a posix path MakeFolderIfNotExist ("/Users/rondebruin/Desktop/TestFolder4") End Sub Function MakeFolderIfNotExist(Folderstring As String) 'Ron de Bruin, 22-June-2015 ' http://www.rondebruin.nl/mac/mac010.htm Dim ScriptToMakeFolder As String Dim str As String If Val(Application.Version) < 15 Then ScriptToMakeFolder = "tell application " & Chr(34) & _ "Finder" & Chr(34) & Chr(13) ScriptToMakeFolder = ScriptToMakeFolder & _ "do shell script ""mkdir -p "" & quoted form of posix path of (" & _ Chr(34) & Folderstring & Chr(34) & ")" & Chr(13) ScriptToMakeFolder = ScriptToMakeFolder & "end tell" On Error Resume Next MacScript (ScriptToMakeFolder) On Error GoTo 0 Else str = MacScript("return POSIX path of (" & _ Chr(34) & Folderstring & Chr(34) & ")") MkDir str End If End Function 

他的网站是Excel VBA开发的一个很好的资源。