﻿'This script searches the BLN file using the BLN flag value in
'Column B and then automatically enters the correct number of points
'in the header line in Column A. This is useful if you have a long
'BLN file and the header count is off for a few objects.

'============================================================

Sub Main
Debug.Clear

Dim SurferApp As Object
Set SurferApp = CreateObject("Surfer.Application")
SurferApp.Visible = True

'Specifies input data
	BLNFile$ = GetFilePath( , "bln",path1, "Select bln file")
	If BLNFile$ ="" Then End

'Opens BLN file
	Dim Wks As Object
	Set Wks = SurferApp.Documents.Open(FileName:=BLNFile$)

'Defines worksheet range as columns A and B
	Dim WksRange As Object
	Set WksRange = Wks.Columns(Col1:=1, Col2:=2)

'Define the header flag in Column B for the objects in the BLN file
Flag= Val(InputBox$("Enter the flag value in Column B in the BLN file. It must be the same for each object in the BLN. Usually, this is a 0 or 1.", "Specify Flag Value", "1"))

'Sets BLN header
RowToChange = 0

   For row=1 To WksRange.RowCount
    	'If Grid.IsBlanked(row,col) = True Then Grid.SetNode(row,col) = 0
      If Wks.Cells("B"+Format(row)).Value=Flag Then
      	If RowToChange >0 Then
      		Wks.Cells("A"+ RowToChange).Value = Format(row)-RowToChange-1
      	End If
      	RowToChange = Format(row)
      End If
	  If Format(row) = WksRange.RowCount Then
    	Wks.Cells("A"+RowToChange).Value = Format(row)-RowToChange
      End If

   Next row

'Saves BLN file
	BLNfilepath = Left(BLNFile$,InStrRev(BLNFile$,".")-1) + "_Corrected.bln
	Wks.SaveAs(FileName:=BLNfilepath)
	Debug.Print "File saved as ";BLNfilepath

End Sub
