Hello, I have a code to import data fron text file and I want to add progress bar to monitor it. My problem is when I run the code with progress bar there is some error occures. Anyhelp would be appreciated. Thanks,
Sub import_mgf() s Progressbar.Show vbModeless Progressbar.Caption = "Process status" Progressbar.ProgressBar1.Min = 1 Progressbar.ProgressBar1.Max = 2000 Progressbar.ProgressBar1.Value = 1 Dim O_Fso As Object Dim Txt_File As Object Dim File_Path As String Dim T_Str As String, Str_Ma4 As String Dim T_Bool As Boolean Dim Hld_Data As Variant Dim Ma_H_Boo As Boolean Dim Ctr As Long Dim i As Long Const LL_Cha As String = "CHARGE=" Const LL_End As String = "END IONS" Const LL_Pep As String = "PEPMASS=" T_Bool = False Ma_H_Boo = False Ctr = 0 i = 1 File_Path = Application.GetOpenFilename( _ FileFilter:="Mascot generic format (*.mgf), *.mgf, Comma Separated Files (*.csv), *.csv, All Files (*.*), *.*", _ FilterIndex:=1, _ Title:="Select a File") If File_Path = "False" Then Exit Sub Set O_Fso = CreateObject("Scripting.FileSystemObject") Set Txt_File = O_Fso.OpenTextFile(File_Path) With Txt_File Do Until .AtEndOfStream = True T_Str = .ReadLine If T_Str = LL_End Then T_Bool = False Ctr = 0 End If If InStr(T_Str, LL_Pep) <> 0 Then Ma_H_Boo = True If Ma_H_Boo Then Hld_Data = Split(T_Str, Chr$(9)) i = i + 1 Cells(i, 1).Value = Replace(CStr(Hld_Data(0)), LL_Pep, "") Cells(i, 2).Value = 0 Cells(i, 3).Value = 0 i = i + 1 Ma_H_Boo = False End If If T_Bool Then Ctr = Ctr + 1 Hld_Data = Split(T_Str, Chr$(9)) Cells(i, 1).Value = Hld_Data(0) Cells(i, 2).Value = Hld_Data(1) Cells(i, 3).Value = Hld_Data(2) If Ctr = 1 Then Cells(i - 1, 4).Value = Str_Ma4 End If i = i + 1 End If If InStr(T_Str, LL_Cha) <> 0 Then T_Bool = True Hld_Data = Split(T_Str, "=") Str_Ma4 = CStr(Hld_Data(1)) Str_Ma4 = Replace(Str_Ma4, "+", "") End If Progressbar.ProgressBar1.Value = i Loop End With Txt_File.Close Set Txt_File = Nothing Set O_Fso = Nothing Progressbar.Caption = "Ready" Unload Progressbar Call clear_contents_ref02 End Sub
Hi,
Could you upload a workbook with the progress bar and at least one file that you would normally import? It doesn't have to contain any real data, but just any mocked up data to reflect the true workings of the process.
Also what error message are you getting and which line does it stop at?
abousetta
Please consider:
Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.
What sort of errors?
You really need to post example workbook so we can see how you have setup the progress userform. Also an example of the import file would help if that is related to the error.
Thanks for your anwers,
Please find attached the problem I have. I get "invalid property value" after running a code.
thanks,
First problem is you have set the max to 2000 but the i variable exceeds this.
I'm also getting lots of Out of Memory warnings but I'm not sure what is causing that.
I was thinking the same thing as Andy (I am honored to be on the same wavelength with such a well-know Guru). Your largest i is 5822. So you might want to make the maximum a bit a higher. For example:
abousettaProgressbar.ProgressBar1.Max = 6000
P.S. Not getting any Out of Memory Errors, so can't guess as to why this is happening also.
Please consider:
Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.
If it's possible to set "Progressbar.ProgressBar1.Max = " to finish at the same time with the code. I need to import data from different text files so the time to upload data is not the same.
thanks,
It is possible. The problem seems to be that the last part of the file is irrelevant to your import. For example the full length of the document is about 7500 lines but your "i" is only around 5900. What happens if I use the length of the file is that the progress bar progresses normally but then ends early because the last quarter is run extremely quickly. On the other hand, I tried to calculate the final number of "i" but now this is less than the length of the text file.
So I am out of ideas... sorry.
abousetta
Please consider:
Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.
This approach, using standard I/O, will allow you to determine size and position within the file being processed.
Sub import_mgf() Progressbar.Show vbModeless Progressbar.Caption = "Process status" Progressbar.ProgressBar1.Min = 1 Progressbar.ProgressBar1.Max = 2000 Progressbar.ProgressBar1.Value = 1 Dim intUnit As Integer Dim lngLOF As Long Dim lngPIF As Long Dim O_Fso As Object Dim Txt_File As Object Dim File_Path As String Dim T_Str As String, Str_Ma4 As String Dim T_Bool As Boolean Dim Hld_Data As Variant Dim Ma_H_Boo As Boolean Dim Ctr As Long Dim i As Long Const LL_Cha As String = "CHARGE=" Const LL_End As String = "END IONS" Const LL_Pep As String = "PEPMASS=" T_Bool = False Ma_H_Boo = False Ctr = 0 i = 1 File_Path = Application.GetOpenFilename( _ FileFilter:="Mascot generic format (*.mgf), *.mgf, Comma Separated Files (*.csv), *.csv, All Files (*.*), *.*", _ FilterIndex:=1, _ Title:="Select a File") If File_Path = "False" Then Exit Sub 'Set O_Fso = CreateObject("Scripting.FileSystemObject") 'Set Txt_File = O_Fso.OpenTextFile(File_Path) intUnit = FreeFile Open File_Path For Input As intUnit lngLOF = LOF(intUnit) Progressbar.ProgressBar1.Max = lngLOF Do While Not EOF(intUnit) lngPIF = Seek(intUnit) Progressbar.ProgressBar1.Value = lngPIF Line Input #intUnit, T_Str If T_Str = LL_End Then T_Bool = False Ctr = 0 End If If InStr(T_Str, LL_Pep) <> 0 Then Ma_H_Boo = True If Ma_H_Boo Then Hld_Data = Split(T_Str, Chr$(9)) i = i + 1 Cells(i, 1).Value = Replace(CStr(Hld_Data(0)), LL_Pep, "") Cells(i, 2).Value = 0 Cells(i, 3).Value = 0 i = i + 1 Ma_H_Boo = False End If If T_Bool Then Ctr = Ctr + 1 Hld_Data = Split(T_Str, Chr$(9)) Cells(i, 1).Value = Hld_Data(0) Cells(i, 2).Value = Hld_Data(1) Cells(i, 3).Value = Hld_Data(2) If Ctr = 1 Then Cells(i - 1, 4).Value = Str_Ma4 End If i = i + 1 End If If InStr(T_Str, LL_Cha) <> 0 Then T_Bool = True Hld_Data = Split(T_Str, "=") Str_Ma4 = CStr(Hld_Data(1)) Str_Ma4 = Replace(Str_Ma4, "+", "") End If Loop Close intUnit ''End With ''Txt_File.Close Set Txt_File = Nothing Set O_Fso = Nothing Progressbar.Caption = "Ready" Unload Progressbar End Sub
Thank you all for kind help and valuable time.
@abousetta, thank you very much for your answer
@Andy Pope, thank you very much for the nice code. It works like a charm.
@copythat, if you are satisfied with the responses then please mark the thread as Solved.
Good luck.
abousetta
Please consider:
Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks