简体   繁体   English

跨列的VBA循环宏

[英]VBA Loop macro across columns

Good Afternoon, 下午好,

I have a worksheet that runs multiple macros. 我有一个运行多个宏的工作表。

module #1 lists the sub folders of a master directory across columns on row 3. (Works Correctly) 模块#1在第3行的各列中列出了主目录的子文件夹。(正确工作)

module #2 lists a specific sub folder from the resulting folder from module #1 based on keywords, the result is printed to row 4. This module functions correctly for column A, though I have been unsuccesful in repeating the calculation across the columns based on a reletive cell reference across row 3. What the code is doing is returning the correct result to A4, and then prints the same result to B4,C4... i cant seem to modify this code to consider the row 3 result for each column. 模块#2根据关键字从模块#1的结果文件夹中列出了特定的子文件夹,结果打印到第4行。尽管我无法根据行3上的可重复单元格引用。代码正在执行的操作是将正确的结果返回到A4,然后将相同的结果打印到B4,C4 ...我似乎无法修改此代码以考虑每列的第3行结果。

Private Sub PrintFolders()
Dim objFSO As Object
Dim OBJFolder As Object
Dim objSubFolder As Object
Dim i As Integer

    Dim rCell As Range
    Dim rRng As Range

    Set rRng = Sheet1.Range("A4:BZ4")

    For Each rCell In rRng.Cells
Application.StatusBar = ""
'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set OBJFolder = objFSO.getfolder(Sheets("Sheet1").Range("A3").Value)
i = i + 1

'loops through each folder in the directory and prints their names and path
On Error GoTo handleCancel

For Each objSubFolder In OBJFolder.SubFolders
If InStr(1, objSubFolder.Name, "Plans", vbTextCompare) > 0 Or InStr(1, objSubFolder.Name, "Sketches", vbTextCompare) > 0 Then
Application.StatusBar = objSubFolder.Path & " " & objSubFolder.Name
    'print folder path
    Cells(1 + 3, i) = objSubFolder.Path
    i = i

Else
End If

Next objSubFolder
Next rCell
handleCancel:
If Err = 18 Then
 MsgBox "You cancelled"
End If

End Sub

any assistance is very appreciated. 非常感谢您的协助。

I have not tried this, but I think using the Offset function will give you the cell relative to the current cell you are calculating from. 我没有尝试过,但是我认为使用Offset函数将为您提供相对于您正在计算的当前像元的像元。

Private Sub PrintFolders()
    Dim objFSO As Object
    Dim OBJFolder As Object
    Dim objSubFolder As Object
    Dim i As Integer

        Dim rCell As Range
        Dim rRng As Range

        Set rRng = Sheet1.Range("A4:BZ4")

        For Each rCell In rRng.Cells
    Application.StatusBar = ""
    'Create an instance of the FileSystemObject
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'Get the folder object
    Set OBJFolder = objFSO.getfolder(rCell.Offset(-1, 0).Value)
    i = i + 1

    'loops through each folder in the directory and prints their names and path
    On Error GoTo handleCancel

    For Each objSubFolder In OBJFolder.SubFolders
    If InStr(1, objSubFolder.Name, "Plans", vbTextCompare) > 0 Or InStr(1, objSubFolder.Name, "Sketches", vbTextCompare) > 0 Then
    Application.StatusBar = objSubFolder.Path & " " & objSubFolder.Name
        'print folder path
        Cells(1 + 3, i) = objSubFolder.Path
        i = i
    Else

    End If

    Next objSubFolder
    Next rCell
    handleCancel:
    If Err = 18 Then
     MsgBox "You cancelled"
    End If

    End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM