디렉터리의 내용을 검사하고 해당 검사 결과로 테이블을 업데이트하는 vba 스크립트를 작성했습니다. 결과는 파일이 Unchanged, New 또는 Missing이고 테이블의 File Status 열로 업데이트됩니다. 새로운 파일이면 파일 이름을 테이블의 Filename 열에 넣습니다.Excel VBA를 사용하여 디렉터리 내용을 검사하고 테이블을 업데이트하십시오.
코드가 상당히 비슷하지만 두 개의 배열을 통과하는 논리에 문제가 있습니다. 나는 코드에서 더 많은 문제를 일으키지 않고 이것을 보면서 내 능력을 다 써 버렸다. 더 많은 변화가 코드에서 회귀를 야기하고있다.
누구나 코드를 살펴볼 수 있습니까? 내가 올바른 길을 가고 있는지 또는 간단한 실수가 있는지 확인하십시오. 일부 파일이 누락되었거나 잘못 표시되는 것으로 잘못 표시되는 경우가 있습니다. 그러나 잘못 되었으면 먼저 변경되지 않은 파일을 올바르게 표시하고 있다고 생각합니다.
Sub FolderContents()
Dim objFSO, objFolder, objFile As Object
Dim g, h, i, j, k, l As Integer
Dim myTable As ListObject
Dim myArray As Variant
Dim FileArray(), FileStatusArray() As String
Dim wsName, tbName, fnName, fsName, Path As String
Dim colNumFile, colNumStatus As Long
Dim newRow As ListRow
h = 1
j = 1
l = 1
' Change only these values if name of table or worksheets change
wsName = "Signage List" 'Worksheet name that contains the signage table
tbName = "Signage" 'Table name for the signage file data
fnName = "Filename" 'Column name that contains the file names
fsName = "File Status" 'Column name that contains the file statuses
' ! DO NOT EDIT ANYTHING BELOW THIS LINE !
Application.FileDialog(msoFileDialogFolderPicker).ButtonName = "Select Folder"
Set objFSO = CreateObject("Scripting.FileSystemObject")
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Title = "Select destination folder"
If .Show = -1 And .SelectedItems.Count = 1 Then
Path = .SelectedItems(1)
Else: Exit Sub
End If
End With
Set objFolder = objFSO.GetFolder(Path)
Set myTable = Worksheets(wsName).ListObjects(tbName)
colNumFile = myTable.ListColumns(fnName).Index
colNumStatus = myTable.ListColumns(fsName).Index
If Not myTable.ListColumns(colNumFile).DataBodyRange Is Nothing Then
myArray = myTable.ListColumns(colNumFile).DataBodyRange
End If
If Not IsEmpty(myArray) Then
For Each objFile In objFolder.Files
If objFile.Type = "PNG image" Then
For i = LBound(myArray) To UBound(myArray)
ReDim Preserve FileArray(1 To j)
ReDim Preserve FileStatusArray(1 To j)
If myArray(i, 1) = objFile.Name Then
FileArray(j) = objFile.Name
Cells(i + 1, colNumStatus) = "Unchanged"
FileStatusArray(j) = "Unchanged"
GoTo NextFile
Else
FileArray(j) = objFile.Name
FileStatusArray(j) = "New"
End If
Next i
NextFile:
j = j + 1
End If
Next objFile
For k = LBound(FileArray) To UBound(FileArray)
For l = LBound(myArray) To UBound(myArray)
If Not myArray(l, 1) = FileArray(k) Then
Cells(l + 1, colNumStatus) = "Missing"
GoTo AnotherFile
Else
Cells(l + 1, colNumStatus) = "Unchanged"
End If
Next l
AnotherFile:
If FileStatusArray(k) = "New" Then
Set newRow = myTable.ListRows.Add(AlwaysInsert:=True)
Set myTable = Worksheets(wsName).ListObjects(tbName)
newRow.Range.Cells(1, colNumStatus) = "New"
newRow.Range.Cells(1, colNumFile) = FileArray(k)
End If
Next k
Else
For Each objFile In objFolder.Files
If objFile.Type = "PNG image" Then
ReDim Preserve FileArray(1 To h)
ReDim Preserve FileStatusArray(1 To h)
FileArray(h) = objFile.Name
FileStatusArray(h) = "New"
h = h + 1
End If
Next objFile
For g = LBound(FileArray) To UBound(FileArray)
Set newRow = myTable.ListRows.Add(AlwaysInsert:=True)
Set myTable = Worksheets(wsName).ListObjects(tbName)
newRow.Range.Cells(1, colNumStatus) = "New"
newRow.Range.Cells(1, colNumFile) = FileArray(g)
Next g
End If
End Sub
고마워요!