Originally Posted by
SamT
I see a lot of problems with that procedure, I would use the FileDialog(msoFileDialogFilePicker) to get the file name and the OpenText Method to open the Text file in a new workbook and work only on that book's single worksheet.
In any Case, all " Time"s should be in the same column on the sheet.
However, for your If...Then...Else
If WorksheetFunction.DCountA(Sheets("X").Cells, "TimesColumn", " Times") = 1 Then
'Save the book
Else
'Separate the data
'Save the book
End If
Hey! I tried this code but it doesn't seem to work
Private Sub CommandButton2_Click()
If WorksheetFunction.DCountA(Sheets("1").Cells, "A", " Times") = 1 Then
Dim varFileName
varFileName = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If TypeName(varFileName) = "String" Then
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & varFileName _
, Destination:=Range("A1"))
.Name = "AddEmployee"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=True
Dim strFileFullName As String
strFileFullName = varFileName
Range("r7").Select
ActiveCell.FormulaR1C1 = strFileFullName
TextBox2.Text = ActiveSheet.Range("r7").Value
End With
End If
Else
varFileName = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If TypeName(varFileName) = "String" Then
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & varFileName _
, Destination:=Range("A1"))
.Name = "AddEmployee"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=True
Dim keyPhrase As String
Dim topCell As Range, bottomCell As Range
Dim dataCells As Variant
Dim sourceSheet As Worksheet
Set sourceSheet = ActiveSheet
keyPhrase = " Time"
Application.ScreenUpdating = False
With sourceSheet.Columns(1)
Set bottomCell = .Cells(.Rows.Count, 1).End(xlUp)
Set topCell = .Find(keyPhrase, After:=bottomCell, SearchDirection:=xlPrevious, LookAt:=xlWhole)
Do Until (topCell Is Nothing)
If (bottomCell.Row < topCell.Row) Then Exit Do
With .Parent.Parent
With .Worksheets.Add(After:=.Sheets(.Sheets.Count), Type:=xlWorksheet)
Range(topCell, bottomCell).EntireRow.Copy Destination:=.Range("A1")
End With
End With
Set bottomCell = topCell.End(xlUp)
Set topCell = .Find(keyPhrase, After:=bottomCell, SearchDirection:=xlPrevious, LookAt:=xlWhole)
Loop
End With
Application.ScreenUpdating = True
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In ActiveWorkbook.Worksheets
If Application.CountA(ws.Cells) = 0 Then ws.Delete
Next ws
Application.DisplayAlerts = True
Application.DisplayAlerts = False 'deleting sheet 1
Sheets("Sheet1").Select
ActiveWindow.SelectedSheets.Delete
strFileFullName = varFileName
Range("r7").Select
ActiveCell.FormulaR1C1 = strFileFullName
TextBox2.Text = ActiveSheet.Range("r7").Value
MsgBox "File loaded."
End With
Else
MsgBox "File is not loaded."
End If
End If
End Sub