Change Needed - VB Code in excel

I have a neat VB code that I use in Excel.  This code imports data from a 
text file into columns in an excel spreadsheet.

The excel file has two worksheets, one is to input the .txt filename with a 
button for the VB code to do it's magic and convert everything I have in the 
text file, which usually has the same layout (in each txt file) in terms of 
what data I want columnized in excel. The code goes as follows....

Sub ConvertFile()
Dim LCStart As Long
Dim IQStart As Long
Dim Lat1Start As Long
Dim Lon1Start As Long
Dim Lat2Start As Long
Dim Lon2Start As Long
Dim NbmesStart As Long
Dim Nbmes2Start As Long
Dim BestLevelStart As Long
Dim PassDurStart As Long
Dim NOPCStart As Long
Dim CalcFreqStart As Long
Dim AltStart As Long
Dim Num1Start As Long
Dim Num2Start As Long
Dim Num3Start As Long
Dim Num4Start As Long
Dim LineIn As String
Dim RecNo As Long
Dim RecLine As Long
Dim aryData As Variant
Dim maxRecords As Long
Dim recColumns As Long

maxRecords = 999
recColumns = 20
Worksheets("Raw Data").UsedRange.Offset(1, 0).Resize(maxRecords, 
aryData = Worksheets("Raw Data").UsedRange.Offset(0, 0).Resize(maxRecords, 
ChDrive Left(ActiveWorkbook.Path, 1)
ChDir ActiveWorkbook.Path
'FName = Range("FName").Value
Open Range("FName") For Input As #1
RecNo = 2
RecLine = 0
Do While Not EOF(1) ' Loop until end of file.
    Line Input #1, LineIn ' Read line into variable.
    RecLine = RecLine + 1
    Select Case RecLine
    Case 1
        LCStart = WorksheetFunction.Find("LC :", LineIn)
        IQStart = WorksheetFunction.Find("IQ :", LineIn)
        aryData(RecNo, 1) = Mid(LineIn, 2, 5)
        aryData(RecNo, 2) = WorksheetFunction.Substitute(Mid(LineIn, 15, 8), 
".", "/")
        aryData(RecNo, 3) = TimeValue(Mid(LineIn, 24, 8))
        aryData(RecNo, 4) = Trim(Mid(LineIn, LCStart + 4, IQStart - (LCStart 
+ 4)))
        aryData(RecNo, 5) = Trim(Mid(LineIn, IQStart + 4))
    Case 2
        Lat1Start = WorksheetFunction.Find("Lat1 :", LineIn)
        Lat2Start = WorksheetFunction.Find("Lat2 :", LineIn)
        Lon1Start = WorksheetFunction.Find("Lon1 :", LineIn)
        Lon2Start = WorksheetFunction.Find("Lon2 :", LineIn)
        aryData(RecNo, 6) = Trim(Mid(LineIn, Lat1Start + 6, Lon1Start - 
(Lat1Start + 6)))
        aryData(RecNo, 7) = Trim(Mid(LineIn, Lon1Start + 6, Lat2Start - 
(Lon1Start + 6)))
        aryData(RecNo, 8) = Trim(Mid(LineIn, Lat2Start + 6, Lon2Start - 
(Lat2Start + 6)))
        aryData(RecNo, 9) = Trim(Mid(LineIn, Lon2Start + 6))
    Case 3
        NbmesStart = WorksheetFunction.Find("Nb mes :", LineIn)
        Nbmes2Start = WorksheetFunction.Find("Nb mes>-120dB :", LineIn)
        BestLevelStart = WorksheetFunction.Find("Best level :", LineIn)
        aryData(RecNo, 10) = Trim(Mid(LineIn, NbmesStart + 8, Nbmes2Start - 
(NbmesStart + 8)))
        aryData(RecNo, 11) = Trim(Mid(LineIn, Nbmes2Start + 15, 
BestLevelStart - (Nbmes2Start + 15)))
        aryData(RecNo, 12) = Trim(Mid(LineIn, BestLevelStart + 12))
    Case 4
        PassDurStart = WorksheetFunction.Find("Pass duration :", LineIn)
        NOPCStart = WorksheetFunction.Find("NOPC :", LineIn)
        aryData(RecNo, 13) = Trim(Mid(LineIn, PassDurStart + 15, NOPCStart - 
(PassDurStart + 15)))
        aryData(RecNo, 14) = Trim(Mid(LineIn, NOPCStart + 6))
    Case 5
        CalcFreqStart = WorksheetFunction.Find("Calcul freq :", LineIn)
        AltStart = WorksheetFunction.Find("Altitude :", LineIn)
        aryData(RecNo, 15) = Trim(Mid(LineIn, CalcFreqStart + 13, AltStart - 
(CalcFreqStart + 13)))
        aryData(RecNo, 16) = Trim(Mid(LineIn, AltStart + 10))
    Case 6
        Num1Start = WorksheetFunction.Find(" ", LineIn, 2)
        Num2Start = WorksheetFunction.Find(" ", LineIn, Num1Start + 1)
        Num3Start = WorksheetFunction.Find(" ", LineIn, Num2Start + 1)
        Num4Start = WorksheetFunction.Find(" ", LineIn, Num3Start + 1)
        aryData(RecNo, 17) = Trim(Mid(LineIn, Num1Start + 1, Num2Start - 
(Num1Start + 1)))
        aryData(RecNo, 18) = Trim(Mid(LineIn, Num2Start + 1, Num3Start - 
(Num2Start + 1)))
        aryData(RecNo, 19) = Trim(Mid(LineIn, Num3Start + 1, Num4Start - 
(Num3Start + 1)))
        aryData(RecNo, 20) = Trim(Mid(LineIn, Num4Start + 1))
    Case Else
        If LineIn = " " Then
            RecLine = 0
            RecNo = RecNo + 1
            MsgBox "Record number " & RecNo & " (collar number " & 
aryData(RecNo, 1) & _
            ")" & Chr(10) & "has this additional line:" & _
                Chr(10) & Chr(10) & LineIn, vbInformation, "Please note"
        End If
    End Select
Worksheets("Raw Data").UsedRange.Offset(0, 0).Resize(maxRecords, recColumns) 
= aryData
Worksheets("Raw Data").UsedRange.Offset(RecNo, 0).Resize(maxRecords + 1 - 
End Sub

Now, the txt files normally looks like this...(as an example, I have pasted 
three data blocks, each data block would represent one row in excel when 
converted. There are normally 450-500 data blocks).....

 06979 Date : 18.08.04 13:30:35 LC : 3 IQ : 60
  Lat1 : 68.747N Lon1 : 93.642W Lat2 : 83.784N Lon2 : 178.549E
  Nb mes : 004 Nb mes>-120dB : 000 Best level : -130 dB
  Pass duration : 345s NOPC : 2
  Calcul freq : 401 649660.3 Hz Altitude : 0 m
  00 5866 00 06
 06979 Date : 18.08.04 13:31:10 LC : 2 IQ : 56
  Lat1 : 68.753N Lon1 : 93.656W Lat2 : 66.044N Lon2 : 78.199W
  Nb mes : 006 Nb mes>-120dB : 000 Best level : -128 dB
  Pass duration : 414s NOPC : 3
  Calcul freq : 401 649660.6 Hz Altitude : 0 m
  00 5853 00 06
 06979 Date : 18.08.04 15:09:30 LC : 3 IQ : 68
  Lat1 : 68.737N Lon1 : 93.627W Lat2 : 86.767N Lon2 : 25.778E
  Nb mes : 004 Nb mes>-120dB : 000 Best level : -133 dB
  Pass duration : 483s NOPC : 4
  Calcul freq : 401 649651.0 Hz Altitude : 0 m
  00 5822 00 06

Note above that there are spaces in certain areas. (one space before the 
first block and the first line, 2 spaces before the second line and so on, 

I can work with this data and it's layout fine and easily converts to 
columnized excel file. 

However, my problem is this...Once in a while, the data comes in a different 
layout and it is very time consuming to make changes to the text file and 
adding spaces where they should be in data that comes in which have this 

 06976 Date : 07.07.04 13:05:56 LC : 1 IQ : 50 
 Lat1 : 64.913N Lon1 : 90.657W Lat2 : 81.105N Lon2 : 167.394E 
 Nb mes : 004 Nb mes>-120dB : 000 Best level : -131 dB 
 Pass duration : 396s NOPC : 3 
 Calcul freq : 401 651625.3 Hz Altitude : 0 m 
 00 169 00 65 

 06976 Date : 07.07.04 13:08:08 LC : 2 IQ : 66 
 Lat1 : 64.897N Lon1 : 90.663W Lat2 : 70.048N Lon2 : 119.597W 
 Nb mes : 004 Nb mes>-120dB : 000 Best level : -129 dB 
 Pass duration : 263s NOPC : 3 
 Calcul freq : 401 651621.5 Hz Altitude : 0 m 
 00 170 01 65 

 06976 Date : 07.07.04 13:24:05 LC : 0 IQ : 56 
 Lat1 : 64.887N Lon1 : 90.551W Lat2 : 63.387N Lon2 : 82.438W 
 Nb mes : 005 Nb mes>-120dB : 000 Best level : -126 dB 
 Pass duration : 461s NOPC : 2 
 Calcul freq : 401 651611.8 Hz Altitude : 0 m 
 00 172 01 65 

Note above that there is the one space before the first line, which is fine, 
but there is only one space before the second and rest lines where there 

Can I make a simple change to the VB code to take into consideration the 
missing spaces in the second txt file example shown above? if so, I could 
just marry whoever provides an answer if your a girl, or take you in as a 
brother in law if your a guy. <sorry, it would just save so much time>.

Eskimo1 (9)
8/27/2004 9:19:07 PM
excel.misc 78881 articles. 5 followers. Follow

2 Replies

Similar Articles

[PageSpeed] 14

I got it!  I just deleted the space in the vb line " " below.

   Case Else
        If LineIn = " " Then
            RecLine = 0
            RecNo = RecNo + 1

and made in into

   Case Else
        If LineIn = "" Then
            RecLine = 0
            RecNo = RecNo + 1

Simple enough, even I can figure it out.

Eskimo1 (9)
8/27/2004 10:27:01 PM
You may want to trim LineIn, just in case:

	if trim(linein) = "" then

And take a look at VBA's help for Instr().

You may want to replace your worksheetfunction.find's.

Eskimo wrote:
> I got it!  I just deleted the space in the vb line " " below.
>    Case Else
>         If LineIn = " " Then
>             RecLine = 0
>             RecNo = RecNo + 1
> and made in into
>    Case Else
>         If LineIn = "" Then
>             RecLine = 0
>             RecNo = RecNo + 1
> Simple enough, even I can figure it out.
> Eskimo


Dave Peterson
ec35720 (10082)
8/27/2004 10:40:22 PM

Similar Artilces:

Steps in Exchange 2000 to change domain name?
We have an existing domain name ( We have decided to change our name to Right now the users are and we want it to be What do I need to do to change that in Exchange? I have made the necessary DNS changes (or so I believe). Thank you!!! Douglas Just add to your recipient policy & make it the not to remove your existing addresses as it may help during the transition to let users receive mail for both addresses. See for help. ; ...

Looking for Excel Help
I'm a very novice Excel user and am looking for a little help with creating a formula for a spreadsheet I'm creating for my personal use. I would appreciate some assistance if possible. Thanks in advance. Dan --- Message posted from Hi Dan! Post a sample of what you want to do. Your question is just a tad open ended <g> -- Regards Norman Harker MVP (Excel) Sydney, Australia Excel and Word Function Lists (Classifications, Syntax and Arguments) available free to good homes. "DanB4105" <DanB4105.ywtpa@excelfor...

New to excel
Hi All, I'm new to Excel ( and to this forum :) ) and so I hope somebody may b able to help me. I've got 2 questions.... QUESTION 1 I've got a spreadsheet which takes data from one worksheet and uses i to calculate data in a second worksheet using the following code formula: =IF('4th November 2005'!B19="","nothing here dude",IF(B19<'4th Novembe 2005'!B19,"UP",IF(B19='4th November 2005'!B19,"Same",IF(B19>'4t November 2005'!B19,"DOWN")))) The problem is, when I create a new worksheet I have...

Need Help, Task Start Date is wrong
I’m using MS Project 2007, have several task linked with finish to start. I have set date to schedule from, hours per day set to 8 and Working Monday thru Friday. My schedule shows Task 1 Duration 4 days, start Wed 6/2/10, Finish Mon 6/7/10 Task 2 Duration 3 days, start Mon 6/7/10, Finish Thu 6/10/10 Task 2 should have a Start Date of 6/8/10 not 6/7/10; what is causing this? Thanks in advance for your help. ...

Changing Cells and entering data in them
Thanks for the help again. Big thanks to Steve you've got me this far. I went out and bought a book, but it's like reading a foreign language. I was informed today that I can't have message boxes come up. I need to have the code point at the cells and if they are blank turn which ever one is blank red or if both are then both turn red then pause for each cell to be filled in. Cell F14 "Last Name" then automatically go to Cell F16 "First Name" on tab or enter. Basically if Cell F22 or F23 has an X in it, Cells F14 an F16 turn red and cell F14 has the focus...

Excel button problem
Hi All I have a macro that copies a worksheet in the active workbook and puts it into a new workbook - then formats it and deletes any buttons on the worksheet. On the first click on the button the macro works ok. On the second click, it fails because the all assigned macros on all buttons in the active workbook changed from "mba" to "book1!mba". Book 1 doesn't exists (wasn't opened, wasn't saved, doesn't have the macros). I've never experienced this problem before?? Can anyone help to solve this problem? FYI The macro to do this is c...

Excel 97 #9
Please can anyone help??? I have two columns in Excel 97. The first contains a list of statu values eg. pending, or granted or withdrawn. The second contains date eg.01/12/1997, 05/06/2003. I woudl like to know how to get all th granted apps before 31/12/2003. Can anyone help please -- Message posted from theres many ways, but an easiest way would be to do a sort. Highlight the 2 columns, click on data, then sort, then sort by status, then by date. this should group them all together. hope this helps...toe >-----Original Message----- >Please can anyo...

learning Excel #3
Hi, I was considering learning Excel as an additional tool for my data analysis work. Is it better to use data sets that I have previously used with SPSS and apply the same analysis tools as in SPSS? For applying the appropriate tools, I was considering using excel's online help. The second option I have is to use some excel book for data analysis and apply the techniques to data sets provided with the book. Any suggestions????? regards Metal ...

unable to paste Excel 2003 chart into Outlook 2003
(This was posted on "excel.charting" group.) I have a user who's unable to paste an Excel 2003 chart into Outlook 2003 email message. In Outlook options, the checkbox is selected for "Use Microsoft Office Word 2003 to edit e-mail messages". When I tested this on my own computer running the same version of Office, if the box is check, I have no problem pasting; if this box is cleared, I cannot paste. But on his computer, it doesn't work regardless. Thanks and regards, TL ...

How do I Remove a Split from my Comments in Excel 2003? #2
I have set my current workbook to split/freeze the first column and first 2 rows. Now, when I add a comment to the second row (in any column) my comments are cut off if I should scroll down. I don't ever remember the behavior before. And I don't know what I've done to enable it but it's really annoying. How do turn this off ? ...

Change position ID in HR
We would like to change the position ID in human resources. Does anyone have a suggestion on this. You would need to do it behind the scenes using a tool like Query Analyzer. -- Charles Allen, MVP "KT" wrote: > We would like to change the position ID in human resources. Does anyone have > a suggestion on this. careful though when you change it on the background as you need to know all the tables that use this position ID or Position Code and change it there too otherwise all the link would be gone and you end up with orphan records that its just the same as creatin...

I need a macro
I would like a macro to do the following: I would place the the cursor in any cell and this macro would give me the average of all the entries to the left if the selected cell. EX: I place the cursor in cell M12. I want this macro on display in cell M12 the average of all the values from Col C12 to Col L12. Note that there may NOT be entries in all the cells in that range. Also the start point would always be col C .Thanks You can do this easily with a worksheet_selection event in the sheet module but you would probably want to restrict to a certain range or columns and rows or it woul...

explanation of codes in Visual Basic when creating User form
Hi, I am trying to create a user form in Visual Basic however I'm trying to teach myself by reading/watching tutorials. (, etc) A lot of the instructions I am seeing simply give the code rather than explain how to actually write one from scratch. So... I need to know what each 'term' means so I can understand how the codes work. Any help is much appreciated :) One of the first codes is for the Add button Private Sub cmdAdd_Click() Dim iRow As Long Dim ws As Worksheet Set ws = Worksheets("PartsData") What d...

Excel corrupts when asking to update vlookups
We are experiencing weird behavior with some Office 2K3 Excel spreadsheets that contain lots of calculations, but no macros. On some pc’s Excel acts normally, on others you get the error. I have a couple of screen shots available. Any help is appreciated. If desired, send your file to my address below. I will only look if: 1. You send a copy of this message on an inserted sheet 2. You give me the newsgroup and the subject line 3. You send a clear explanation of what you want 4. You send before/after examples and expected results. -- Don Gu...

Uninstall of mappoint has caused errors with excel
Hi, I am running Office 2003 on the terminal server (windows 2003) and had a copy of mappoint as well. This is a mapping program. We ininstalled mappoint which has caused an error message with Excel and other office products. The error says "Cd:\documents and settings\administrator.ocrdc1\application data\microsoft\addins c:\Program files\common files\microsoft shared\geography\mpoai9.dll is not a valid add-in." I then click OK and excel opens up and everything is fine. The problem is that we are using other programs as well such as Quickbooks that export to excel and t...

I need to print a word in PDF from excel.
Hi, In my excel Macro, I update several values and then open a word linked with that excel. Finally what I need to do is to print that word to PDF. I already have a PDF Printer, so I just need the code to do it. Can anybody help me? Thanks in advance. Gast=F3n. Habilita la impresora predeterminada como la PDF y ya lo puedes imprimir a al formato deseado Gracias Francisco,=20 Tienes alg=FAn codigo para ello? gracias Repitiendo: Desde el panel de impresoras asigno primero la que me interesa y despues ejecuto mi macro desde el archivo excel asignando la instruccion sig: midefaultes = a...

How do I get Excell to sort dates that range from 1800 to 1900's
I have posted a cemetery survey on Excell spreadsheet. When I try to sort the birth dates or death dates in ascending or descending order the range of 1900 dates will sort but the 1800 dates fall after the 1900 and will not sort. Why? Is this inherit with this program? The WinXL default dates start with 31 December 1899, so earlier "dates" are interpreted as Text. You can use helper columns and text functions (LEFT(),RIGHT(),MID()) to put the Year, Month and Day in separate columns, then sort. Take a look here for additional workarounds:

How do you change the APR?
How do you change the APR in a Credit Card account? Like if you have to change it from 13.250% to 14.240% Depends on the version but (in M2007) go to the CC Account and click on Change Account Settings. Scroll down to Credit Information and click on Change Credit Details. If you want to change the Minimum payment rate/amount as well, click on the Minimum payment amount and a side window opens. -- Regards Bob Peel, Microsoft MVP - Money For UK tips & fixes see;EN-GB;mny. I do not respond to any emails that I have not specifically ...

After editing example1.xls and click SAVE, the filename changes to AABBEE.xls
After editing example1.xls and click SAVE, the filename changes to AABBEE.xls. The original file still exist and has been updated but the 'funny' filename also have the same content, and editable. The example1.xls is stored in a server and accessed by many people within the company. Everyone accessing to the file will change the filename unknowingly after saving it. ...

q Emailing Excel Sheet With Outlook
I am trying to send a table from Excel within the body of an Outlook (lastest versions) to be sent as a fax. Am able to do this, BUT when the document prints out on the fax machine the formating is off (too big for the sheet). I am flexible on changing the method I send it to the fax machine, however it must be sent to the fax via macro. Below is the code I am using. Any help would be greatly appreciated. David Public Sub DoIt() 'On Error GoTo Handler Dim EmailAddress(0 To 2) As String Dim Count As Integer Dim N As Integer Dim sRec1(0) As String Dim sRec2(0 To 1) As...

Excel VBA
When you select the autofilter, the drop down list is created from the unique data in that column. Is there any way to customize this list, so the changes are permanent? For instance, I might have in one(1) column the following 4 row entries: red, blue, green (all are single cell entries) red, blue red blue, green I want my autofilter drop down list to read: red instead of red, blue, green blue red, blue green red blue, green (like normal) If I chose red from the drop down list...

Business Portal Development
All: I know how to use Rational XDE, VS and the BP SDK to create new entities in BP, but how does one go about chaning labels? For example, if I wanted to change two labels on a requisition mgt screen: Can I just crack open the ASPX page and change the caption property? Thanks, Dwight -- Hi, To edit the lables you can use front page server extensions 2003. "Dwight Specht" wrote: > All: > > I know how to use Rational XDE, VS and the BP SDK to create new entities in > BP, but how does one go about chaning labels? > > For example, if I wanted to ch...

Problem with named range for a large spreadsheet in Excel 2007
Hello, I am trying to generate a large Excel spreadsheet apprx. 10000 rows and 40 columns. I am generating defined name section in the Workbook.xml part of the XLSM package. Here is a sample entry from that section <definedName name="_._44802_._0_._0_._0_._top_line" localSheetId="0" hidden="1">Sheet1!$B$2</definedName> Although the generation goes fine, I can not open the spreadsheet as the Excel throws an error message saying the package is corrupt. But this is not the case if the spreadsheet is small say, 200 rows by 10 columns. ...

how do I add error bars to a 3D chart in excel?
The help states you can only add error bars to data series in 2D area. Is there a way to add them to a 3D chart? Hi, I would not have thought so. Obviously as it is not a built-in option the only way would be a work around perhaps using dummy series. Unfortunately you can create 3d combination charts. Stick with the 2d view. Cheers Andy elahe wrote: > The help states you can only add error bars to data series in 2D area. Is > there a way to add them to a 3D chart? -- Andy Pope, Microsoft MVP - Excel I checked, and error bars are not offered for 3D ch...

MOVE TO FOLDER... only appears. I need MOVE TO FOLDER
This is very odd and I've found that I've had this problem before with not finding icons. Some I've found at the office don't appear here and vice versa, or they act differently. I have Outlook 2000 in both places! Very odd. At home, I couldn't find the HIGH IMPORTANCE icon under the list of items available in the customize. Yet I have it at work. That's the one where when the HIGH IMPORTANCE is on, it shows a depressed button state. I really need that in both places. Anyway, simple (or so I thought) - I ended up just brining the toolbar from work on a floppy, a...