Hi I have the following code getting data from text files and then performs calculations in my workbook. However, this code takes like 35 minutes to run. Is there any way to decrease the time? As you can see, I have the same dropbox directory but with different users, is this something that can be simplified? the first code for Private Sub 0() looks equal as the rest of them but with different textfiles. The code works but is really long...
Code:
Private Sub noll()
Dim Resultat As Worksheet
Dim Indata As Worksheet
Dim noder As Worksheet
Set Resultat = Sheets("Resultat")
Set Indata = Sheets("Indata")
Set noder = Sheets("Alla noder")
Range("A:I").Delete
If ComboBox2.Value = "Wal" Then
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\andre111\Dropbox\" a lot of text...."\noder 0.txt" _
, Destination:=Range("$A$1"))
.Name = "noder"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = True
.TextFileOtherDelimiter = ""
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
ElseIf ComboBox2.Value = "Bar" Then
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\andre222\Dropbox\" a lot of text...."\noder 0.txt" _
, Destination:=Range("$A$1"))
.Name = "noder"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = True
.TextFileOtherDelimiter = ""
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
ElseIf ComboBox2.Value = "Wal Hemma" Then
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\andre333\Dropbox\" a lot of text...."\noder 0.txt" _
, Destination:=Range("$A$1"))
.Name = "noder"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = True
.TextFileOtherDelimiter = ""
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
ElseIf ComboBox2.Value = "Bar Hemma" Then
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\andre444\Dropbox\" a lot of text...."\noder 0.txt" _
, Destination:=Range("$A$1"))
.Name = "noder"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = True
.TextFileOtherDelimiter = ""
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End If
Range("A:A").Delete
Range("B:I").Delete
Range("A1").EntireRow.Insert
Columns("A:A").Select
ActiveWorkbook.Worksheets("Indata").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Indata").Sort.SortFields.Add Key:=Range("A:A"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Indata").Sort
.SetRange Range("A:A")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
With Application.WorksheetFunction
For i = 2 To Range("A" & Rows.Count).End(xlUp).Row Step 1
Indata.Cells(i, 2).Value = .Index(noder.Range("B1:B300000"), .Match(Indata.Cells(i, 1), noder.Range("A1:A300000"), 0))
Indata.Cells(i, 3).Value = .Index(noder.Range("C1:C300000"), .Match(Indata.Cells(i, 1), noder.Range("A1:A300000"), 0))
Indata.Cells(i, 4).Value = .Index(noder.Range("D1:D300000"), .Match(Indata.Cells(i, 1), noder.Range("A1:A300000"), 0))
Indata.Cells(i, 5).Value = .Index(noder.Range("E1:E300000"), .Match(Indata.Cells(i, 1), noder.Range("A1:A300000"), 0))
Next i
End With
With Application.WorksheetFunction
Resultat.Cells(4, 7).Value = .Max(Range("E:E"))
Resultat.Cells(4, 6).Value = .Index(Indata.Range("D:D"), .Match(Resultat.Range("G4"), Indata.Range("E:E"), 0))
Resultat.Cells(4, 5).Value = .Index(Indata.Range("C:C"), .Match(Resultat.Range("G4"), Indata.Range("E:E"), 0))
Resultat.Cells(4, 4).Value = .Index(Indata.Range("B:B"), .Match(Resultat.Range("G4"), Indata.Range("E:E"), 0))
Resultat.Cells(4, 3).Value = .Index(Indata.Range("A:A"), .Match(Resultat.Range("G4"), Indata.Range("E:E"), 0))
End With
End Sub
Private Sub CommandButton2_Click()
Dim Resultat As Worksheet
Set Resultat = Sheets("Resultat")
Sheets("Indata").Select
Call noll
Call ett
Call två
Call tre
Call fyra
Call fem
Call sex
Call sju
Call åtta
Call nio
Call tio
Call elva
Call tolv
Call tretton
Call fjorton
Call femton
Call sexton
Call sjutton
Call arton
Call nitton
Call tjugo
Call tjugoett
Call tjugotvå
Call tjugotre
Call tjugofyra
Call tjugofem
Call tjugosex
Call tjugosju
Call tjugoåtta
Call tjugonio
Call trettio
Call trettioett
Call trettiotvå
Call trettiotre
Call trettiofyra
Call trettiofem
Call trettiosex
Call Roof
Call TopLevel
Call CraneLevel
Resultat.Select
Unload Me
End Sub