masud8956
Board Regular
- Joined
- Oct 22, 2016
- Messages
- 163
- Office Version
- 2016
- 2011
- 2007
- Platform
- Windows
Hi everyone!
I am trying to import a large array of data from multiple files located in a folder named "Aircrew_Flying_Hour" in D:\ using VBA.
The code runs alright in my pc. But it does not run in other pc. I need to be able to run it from a different pc. Do I need any modification of the code or it has something to do with the settings of the pc in which I am trying to run it?
Thanks in advance!
The VBA I am trying is below:
I am trying to import a large array of data from multiple files located in a folder named "Aircrew_Flying_Hour" in D:\ using VBA.
The code runs alright in my pc. But it does not run in other pc. I need to be able to run it from a different pc. Do I need any modification of the code or it has something to do with the settings of the pc in which I am trying to run it?
Thanks in advance!
The VBA I am trying is below:
Code:
Sub test() Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Sheets("DATA").Cells.ClearContents
Dim lastRow As Long
Dim firstRow As Long
Dim myDir As String, fn As String, n As Long, t As Long, Cell As String
Const wsName As String = "Summary of the Year"
Const myRng As String = "G77:U796"
myDir = "D:\Aircrew_Flying_Hour"
fn = Dir(myDir & "\*.xlsx")
If fn = "" Then MsgBox "No files in the folder": Exit Sub
With Range(myRng)
n = .Rows.Count: t = .Columns.Count
Cell = .Cells(1).Address(0, 0)
End With
Do While fn <> ""
With Sheets("Data").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Resize(n, t)
.Formula = "=if('" & myDir & "\[" & fn & "]" & wsName & "'!" & Cell & "<>""""," & _
"'" & myDir & "\[" & fn & "]" & wsName & "'!" & Cell & ","""")"
.Value = .Value
End With
fn = Dir
Loop
firstRow = Sheets("DATA").Range("A1:A" & Sheets("DATA").Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
lastRow = Sheets("DATA").Cells(Rows.Count, "A").End(xlUp).Row
Sheets("DATA").Range("A" & firstRow & ":A" & lastRow).AutoFilter Field:=1, Criteria1:="="
Sheets("DATA").Range("A" & firstRow & ":A" & lastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
If Sheets("DATA").AutoFilterMode Then Sheets("DATA").AutoFilterMode = False
Sheets("DATA COPY").Cells.ClearContents
Sheets("DATA").Range("A1:O" & Sheets("DATA").Range("A" & Sheets("DATA").Rows.Count).End(xlUp).Row).Copy Sheets("DATA COPY").Range("A1")
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub