I have some VB code that imports a file with the following code
Later, I change the size of the range to include the first row, which is the header using the following code.
The problem I run into is the Named Range is distinct to the sheet but I need it to be a Workbook wide Named Range. I don't see anything that allows me to change the scope of an existing range short of deleting the one created on import and creating a new one.
Code:
Sub ImportFile()
Sheets(WEXImportSheet).Select
' Original Code to remove existing named range, variable didn't work so I did it in the case statement for variable definition
' ActiveWorkbook.Names(OpisFileName).Delete
Range("A2").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & WEXImportFile, _
Destination:=Range("$A$2"))
.Name = WEXFileName
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = StartRow
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = DataArray
.TextFileFixedColumnWidths = FieldArray
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
Later, I change the size of the range to include the first row, which is the header using the following code.
Code:
Sub ChangeRange()
'
' Deletes Original Named Range
' Creates new Named Range including header row
'
Sheets(WEXImportSheet).Select
Call RowCount
With ActiveWorkbook.Worksheets(WEXImportSheet).Names(WEXFileName)
.RefersToR1C1 = "=" & WEXImportSheet & "!R1C1:R" & Row & "C4"
End With
End Sub
Sub RowCount()
' Gets number of Rows in the import file
Dim FoundData As Boolean
Dim CellName As String
Row = 1
Col = 1
FoundData = True
Do While FoundData
Range("A1").Offset(Row - 1, Col - 1).Select
CellName = ActiveCell.Value
If Trim(CellName) = "" Then
FoundData = False
Exit Do
End If
Row = Row + 1
Loop
Row = Row - 1
End Sub