[英]Determine if cell exists in a specified range w/ Powershell Excel COM
[英]Determine if a file exists within a folders file-path that is contained in a Cell on Excel for a range of cells?
我熱衷於向我正在構建的 Excel 電子表格添加功能以跟蹤合規性。
事實證明,我的想法很難實現,在閱讀了大量文章和帖子后,我感到非常困惑。
我有一個包含 5 列的電子表格,其中一列將包含大約 100 個文件夾的文件路徑。
其他列將是需要在這些文件夾中的文件 - 4 個標題中具有相同引用的文件。
我想按下一個按鈕,讓 Sub 在啟動時運行以瀏覽每個文件夾並確保存在合規性文件。 我試圖調整一些代碼,但我在一些事情上掙扎。
這是我到達的地方,這是我在網上找到的一些代碼。
Sub IsItThere()
Dim Cell As Range
Dim fileName As Variant
Dim i As Integer
Dim C As String
Cell = E3: E200 'The range of Filepaths
For i = 3 To 200
Do
C = Cell(i)
fileName = Dir(C)
While fileName <> ""
If fileName = Dir()(C + "January*") Then 'Does it contain the key phrase
B(i) = "1"
Else
B(i) = "0" 'Make the cell next door to the cell with the current filepath become 1
End If
i = i + 1
fileName = Dir
Wend
End Sub
這個簡單的代碼將從活動工作表上 A2:A200 范圍內的每個單元格中獲取文本,檢查此字符串中的最后一個符號是否為路徑分隔符,並在需要時添加它。
然后對於右側的四個單元格檢查文件是否存在並將結果(TRUE 或 FALSE)放入 F:I 列中的單元格
Sub IsItThere()
Dim rCell As Range
Dim sPath As String
Dim sFileName As String
Dim i As Integer
For Each rCell In [A2:A200].Cells()
sPath = rCell.Text
If Right(sPath, 1) <> Application.PathSeparator Then sPath = sPath & Application.PathSeparator
For i = 1 To 4
sFileName = Trim(rCell.Offset(0, i).Text)
rCell.Offset(0, i + 4) = (Dir(sPath & sFileName) <> vbNullString)
Next i
Next rCell
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.