Articles

Select all non-empty cells in the sheet but omit the header

February 20, 2013 at 02:09:32
Specs: Windows Vista, Dual Core 3ghz / 4 GB

Hey everyone,

I'm looking to modify and automate some VBA code. Currently I have a number of worksheets. Each sheet holds 1 table with data. My current VBA code exports the selected cells in the current worksheet to a textfile. As I have quite a few sheets, I have to manually select each of the (differently sized) tables in each sheet and then call the VBA code to export the selection.

I would like to automate this and save each worksheet's table values (no header) to a textfile named after the worksheet the table is in. Here is an example of one worksheet and its table:

http://i.snag.gy/O9pR4.jpg

In this case, I want to omit that header and save the A2:E4 range to a ~ separated textfile named after the worksheet the table is in.

Many thanks in advance and best wishes!


This would involve three functions:
1) run the code automatically for every worksheet
{
2) in each of these worksheets, select the entire table except the header which holds data-descriptors (first row)
3) run the code I already have to save the current selection to an external file (named after the active sheet)
}

This is the VBA code I currently call to start the ExportToTextFile function, saving the selection to a ~ separated .doa textfile named after my active worksheet. I still need to manually select each table (except header) on each worksheet though using this.

Sub DoTheExport()
ExportToTextFile FName:="C:\Users\user\AppData\Local\Main Build\Resources\DATA\Database\" & ActiveSheet.Name & ".doa", Sep:="~", _
SelectionOnly:=True, AppendData:=False
End Sub


The ExportToTextFile function it calls looks like this:


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ExportToTextFile
' This exports a sheet or range to a text file, using a
' user-defined separator character.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub ExportToTextFile(FName As String, _
Sep As String, SelectionOnly As Boolean, _
AppendData As Boolean)

Dim WholeLine As String
Dim FNum As Integer
Dim RowNdx As Long
Dim ColNdx As Integer
Dim StartRow As Long
Dim EndRow As Long
Dim StartCol As Integer
Dim EndCol As Integer
Dim CellValue As String


Application.ScreenUpdating = False
On Error GoTo EndMacro:
FNum = FreeFile

If SelectionOnly = True Then
With Selection
StartRow = .Cells(1).Row
StartCol = .Cells(1).Column
EndRow = .Cells(.Cells.Count).Row
EndCol = .Cells(.Cells.Count).Column
End With
Else
With ActiveSheet.UsedRange
StartRow = .Cells(1).Row
StartCol = .Cells(1).Column
EndRow = .Cells(.Cells.Count).Row
EndCol = .Cells(.Cells.Count).Column
End With
End If

If AppendData = True Then
Open FName For Append Access Write As #FNum
Else
Open FName For Output Access Write As #FNum
End If

For RowNdx = StartRow To EndRow
WholeLine = ""
For ColNdx = StartCol To EndCol
If Cells(RowNdx, ColNdx).Value = "" Then
CellValue = Chr(34) & Chr(34)
Else
CellValue = Cells(RowNdx, ColNdx).Value
End If
WholeLine = WholeLine & CellValue & Sep
Next ColNdx
WholeLine = Left(WholeLine, Len(WholeLine) - Len(Sep))
Print #FNum, WholeLine
Next RowNdx

EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #FNum

End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' END ExportTextFile
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''



See More: Select all non-empty cells in the sheet but omit the header

Reply ↓  Report •

Related Solutions


Ask Question