在Excel中连接使用Loop来创build命令的batch file

考虑这种情况:我有一个服务器上有两个networking共享的客户端。 每个共享包含代表每个项目的相同文件夹列表(一个用于当前数据,另一个用于归档数据)。 每个项目文件夹都包含一组匹配的文件夹和子文件夹,需要分配个人权限。 当然,项目是不时添加和删除的。

我想维护Excel中的项目列表以及需要为每个项目文件夹以及所有子文件夹设置的权限列表。 我的想法是在Excel中将每行命名为一个子文件夹。 然后使用for循环将共享文件夹和项目文件夹上添加的这些命令复制到目录path。 输出将是一个batch file,我可以运行任何时候需要更改或更新权限。

编辑:我从来没有在Excel中使用VBA,search后,我似乎无法find如何使用for循环中的variables来更改工作表中引用的单元格。 从我编写任何程序开始已经有一段时间了,但是当我在CI中进行编程时,请记住可以使用for循环中的variables来引用列表/表格中的单元格。 当for循环经过迭代时,variables会计数改变列表中引用的列表中的哪个单元格。 为了把它放在Excel中,我希望每个for循环的variables都是行号,我将静态地分配列字母,因为每次迭代都不会改变。

每次运行for循环时,我都希望它将一系列单元格连接成输出到文本文件的新行的文本string。 连接会将每个循环中保持不变的单元格连接在一起,而随着variables递增的其他单元格将会更改。 例如,我想连接单元格A1,B1,Ci,Dj,E1,Fk,其中i,j和k表示用于计算每个for循环的迭代的整数的数字值。

你能告诉我什么正确的语法是完成串联可变单元格分配? 这是我的代码摘录。

For i = 2 To numberOfSharedFolders 'Loops for every subfolder (project folder) in the shared folder that needs permissions set For j = 2 To numberOfSubfolders1 'Loops for every entry of permissions on final folders For k = 2 To numberOfSubfolders2 concatenatedDataString = ActiveSheet.Range("A1") & ActiveSheet.Range("B1") & ActiveSheet.Range("C"i) & ActiveSheet.Range("D"j) Write #1, concatenatedDataString Next k Next j Next i 

.B3不是ActiveSheet对象的方法或属性。

我怀疑你打算为ActiveSheet.Range("B3")

可能还有更多的错误,实际上接下来的两行可能会引起同样的错误。

可能还有更多,例如注意在这个语句中的错字将会引发一个424的对象要求的错误(因为Appleication不是一个对象,它被视为一个未定义/未声明的variables,其值为空):

 filePath = Appleication.DefaultFilePath & "icacls Commands.bat" 

您可以在每个模块的顶部使用Option Explicit来帮助规避打字错误(这会对任何未定义的variables产生编译错误,而错字被解释为未定义的variables。

对于其余的问题(实际上你是在问6个相当宽泛的问题),我会尽量简短地回答,但是在这里的评论中不会真正地回答长时间的问答和讨论。 如果您有具体的问题,每个人都应该被问到(在您完成search/故障排除等方面的尽职调查后)。

我应该如何在for循环中连接?

不知道你的意思。 通过将一些值附加到现有的string,在循环中连接。 使用&运算符而不是+运算符,因为后者可能被非string数据混淆(即"hello" & 1不会产生错误,但是"hello" +1会引起不匹配)。

 Dim i as Integer Dim s as String s = "some words" For i = 1 to 10 s = s & some_other_variable Next 

事实上,你的代码并不是连接任何string,而只是简单地添加整数:

 concatenatedDataSting = i + j + k 

ijk都是整型数据。 这不是创build一个string(或者如果是,它是一个强制/隐含的string表示该操作的数字总和)。

有没有更好的方法来设置for循环的variables? 目前我有一个单元格使用countif(D:D,“*”)总计每列的大小,并将其分配给一个variables。

什么variables? 在上面的代码中我没有看到对列D或CountIf函数的任何引用。

在工作表中引用数据的最佳方法是什么? 范围()? currentsheet.range()?

这取决于“表单中的数据”的含义。 不清楚,回答太宽泛。

我如何执行这个程序? 通过点击开发者控制台中的播放?

从开发人员function区,macros菜单中,从ThisWorkbook中selectmacros(或者它所在的任何一本书)。 然后运行。 或者从控制台/ VBE,按F5或“运行”button。

我怎么知道文本文件将在哪里结束? 是我的文档的默认位置?

一旦你改正了错字,它将会在这里结束:

 Application.DefaultFilePath 

你可以这样做,看看它是: MsgBox Application.DefaultFilePath

我得到了这一切工作。 我的代码如下。 阅读原始问题,了解更多信息。

Sub Sheet1Compile()

 'Create variables used in for loops Dim numberOfSharedFolders As Integer Dim numberOfSubfolders1 As Integer Dim numberOfSubfolders2 As Integer Dim i As Integer Dim j As Integer Dim k As Integer 'Set values of variables to the number of rows in each column. 'Note that the value includes the column titles in row 1 numberOfSharedFolders = ActiveSheet.Range("B2") numberOfSubfolders1 = ActiveSheet.Range("B3") numberOfSubfolders2 = ActiveSheet.Range("B4") 'Create text file to output of data from for loop Dim filePath As String filePath = Application.DefaultFilePath & "\icacls Commands.txt" Open filePath For Output As #1 'Loops for every row in the first mentioned column (except the first row which contains column labels) 'Initializing to 2 instead of 1 so that the loop will skip the first row which is just column labels. Not setting it to 0 since there is no row 0 For i = 2 To numberOfSharedFolders 'Loops for every row in the second mentioned column (except the first row which contains column labels) For j = 2 To numberOfSubfolders1 'Loops for every row in the third mentioned column (except the first row which contains column labels) For k = 2 To numberOfSubfolders2 'Print the concatendated text to the output file. Print is used instead of "write" so that it doesn't produce unwanted quotation marks. Print #1, ActiveSheet.Cells(2, 3) & ActiveSheet.Cells(i, 4) & ActiveSheet.Cells(j, 5) & ActiveSheet.Cells(1, 6) & ActiveSheet.Cells(k, 7) & ActiveSheet.Cells(1, 8) & ActiveSheet.Cells(k, 9) Next k Next j Next i Close #1 End Sub