I have created main file which imports data from other (closed) excel files. There is x-ten of files from which I need to import data. I made a code in UserForm so that mine boss can choose where to import (sheet = wariant) file. It is not completed because I need to add options button (for choosing which file to import), but main core will look like beneath.
But there is a problem, in our company we have a medium class laptops, so that code (beneath) in executin in 5-7 minutes for each file (wariant). I need it to run as fast as possible. Can you make something with it?
Private Sub CommandButton1_Click()
StartTime = Timer
Dim p As String
Dim f As String
Dim s As String
Dim a As String
Dim r As Long
Dim c As Long
Dim Warinat As String
If UserForm1.War1 = True Then Wariant = "Wariant 1"
If UserForm1.War2 = True Then Wariant = "Wariant 2"
If UserForm1.War3 = True Then Wariant = "Wariant 3"
If UserForm1.War4 = True Then Wariant = "Wariant 4"
p = ThisWorkbook.path
f = "files.xlsx"
s = "Sheet1"
Application.ScreenUpdating = False
For r = 7 To 137
For c = 2 To 96
a = Cells(r, c).Address
If IsNumeric(Cells(r, c)) = True And ThisWorkbook.Sheets(Wariant).Cells(r, c) <> "" _
Then ThisWorkbook.Sheets(Wariant).Cells(r, c) = _
ThisWorkbook.Sheets(Wariant).Cells(r, c).Value + GetValue(p, f, s, a)
Else
ThisWorkbook.Sheets(Wariant).Cells(r, c) = GetValue(p, f, s, a)
End If
Next c
Next r
EndTime = Timer
MsgBox Format(EndTime - StartTime, ssss)
Unload Me
End Sub
Private Function GetValue(path, file, sheet, ref)
Dim arg As String
If Right(path, 1) <> "\" Then path = path & "\"
If Dir(path & file) = "" Then
GetValue = "Files is missing"
Exit Function
End If
arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
Range(ref).Range("A1").Address(, , xlR1C1)
GetValue = ExecuteExcel4Macro(arg)
End Function
Private Sub CommandButton2_Click()
Unload Me
End Sub
Private Sub UserForm_Click()
End Sub