Consider this scenario: I have a client who has two network shares on a server. Each share contains an identical list of folders representing each project (one is for current data, and the other share for archived data). Each project folder contains a matching set of folders and subfolders which need individual permissions assigned. Of course projects are added and removed from time to time.
I'd like to maintain a list of the projects in Excel along with a list of the permissions that need to be set for each project folder as well as all their subfolders. My idea is to lay out the commands in Excel one per row with each row representing a subfolder. Then use for loops to duplicate these commands adding on the shared folder and project folder to the directory path. The output would be a batch file that I could run any time permissions needed to be changed or updated.
EDIT: I've never used VBA in Excel, and after searching I can't seem to find how to use variables in a for loop to change which cells in the sheet are referenced. It's been a while since I wrote any program, but when I was programming in C I remember being able to use the variables from the for loops to reference cells in a list/table. As the for loop went through iterations the variables would count up changing which cell in the list they were referencing. To put this in Excel terms I want the variable from each for loop to be the row number, and I will statically assign the column letter since that's not changing with each iteration.
Each time the for loop runs I want it to concatenate a series of cells into a text string that is output to a new line of a text file. The concatenation will tie together some cells that remain the same during each loop, and other cells that are changing as the variables are incremented. For example I want to concatenate cells A1, B1, Ci, Dj, E1, Fk where i, j, and k represent the numeric value of the integer used to count the iterations of each for loop.
Can you tell me what the proper syntax is to accomplish concatenation with variable cell assignments? Here's an excerpt from my code.
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
I got this all work. My code is below. Read the original question for more info about what this does.
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