我對VBA相當陌生,因此不太瞭解如何正確使用數組。在添加值時向數組添加
我想爲我的文檔刮新值添加到一個數組,但不知道如何做到這一點..
- 我也正在從275個文件刮下的值。
- 我試圖將值寫入即時窗口,它運行良好,但最多隻有200行。
- 我想在每次通過一個文件運行時間,
- 一行每個變量
rfr
,chief
等追加4行....
的代碼:
Sub DeleteNotOpsSheet()
Dim fPath As String
Dim fName As String
Dim wb As Workbook
Dim ws As Worksheet
Dim xWs As Worksheet
Dim rfr As String, chief As String, yard As String, tp As String
Dim Output As ThisWorkbook
Dim i As Long
Dim spath As String
'Which folder?
fPath = "\\hofiler1\fileserver\users\AChan\Documents\Scrape\manning\SEP"
'Check if slash included
If Right(fPath, 1) <> "\'" Then
fPath = fPath & "\"
End If
'Check for xlsm files
fName = Dir(fPath & "*.XLS")
'Turn of the screen
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'Loop until we run out of files
Do While fName <> ""
'Open the workbook
Set wb = Workbooks.Open(fPath & fName)
For Each xWs In wb.Worksheets
If xWs.Name = "ops sheet" Then '--> Getting an Object required error here
rfr = Left(ActiveWorkbook.Name, 11) & " - Reefer Foreman: " & WorksheetFunction.CountA(Range("P42"))
chief = Left(ActiveWorkbook.Name, 11) & " - Chief Foreman: " & WorksheetFunction.CountA(Range("V78"))
yard = Left(ActiveWorkbook.Name, 11) & " - Yard Foreman: " & WorksheetFunction.CountA(Range("AB74:AB81"))
tp = Left(ActiveWorkbook.Name, 11) & " - TPC Foreman: " & WorksheetFunction.CountA(Range("AB68"))
'NEED HELP HERE: I would like to append these values to sheet1 on ThisWorkbook
'Debug.Print rfr
'Debug.Print chief
'Debug.Print yard
'Debug.Print tp
End If
wb.Save
wb.Close True
Next
Application.DisplayAlerts = True
'delete all the others
'SaveChanges:=True, Filename:=newName
'Increment count for feedback
i = i + 1
'Get next file name
fName = Dir()
Loop
'turn screen back on
Application.ScreenUpdating = True
'Give feedback
MsgBox "All done." & vbNewLine & "Number of files changed: " & i, vbOKOnly, "Run complete"
End Sub
如果你是認真學習VBA你應該看這個系列:Excel的VBA簡介】(https://www.youtube.com/playlist?list=PLNIs- AWhQzckr8Dgmgb3akx_gFMnpxTN5。下面是一個相關的視頻:[Excel VBA簡介第25部分 - 數組](https://www.youtube.com/watch?v=h9FTX7TgkpM&index=28&list=PLNIs-AWhQzckr8Dgmgb3akx_gFMnpxTN5) – 2016-11-10 21:43:19