BritsBlitz
New Member
- Joined
- Jan 10, 2014
- Messages
- 34
- Office Version
- 365
- Platform
- Windows
I have a workbook with two worksheets. My first worksheet collects data from a .txt file and my second worksheet displays a summary of the data. I have the following code to import the data from the .txt file. This code works fine when I have the command button on the "Import" worksheet, but it does not work when I try and move the command button to the "Summary" worksheet. I only want to display the "Summary" worksheet and I've hidden the worksheet with all the imported data, hence my need to run this command from the "Summar" worksheet.
How can I have the Command button on the "Summary" worksheet but still have the code import the data to the "Import" worksheet?
Private Sub CommandButton1_Click()
Dim fName As String
Dim LastRow As Long
Worksheets("Import").Activate
fName = Application.GetOpenFilename("text Fiels (*.txt), *.txt")
If fName = "False" Then Exit Sub
LastRow = Range("A" & Rows.Count).End(xlUp).Row + 1
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & fName, Destination:=Range("A" & LastRow))
.PreserveFormatting = True
.AdjustColumnWidth = False
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierNone
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "" & Chr(10) & ""
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
How can I have the Command button on the "Summary" worksheet but still have the code import the data to the "Import" worksheet?
Private Sub CommandButton1_Click()
Dim fName As String
Dim LastRow As Long
Worksheets("Import").Activate
fName = Application.GetOpenFilename("text Fiels (*.txt), *.txt")
If fName = "False" Then Exit Sub
LastRow = Range("A" & Rows.Count).End(xlUp).Row + 1
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & fName, Destination:=Range("A" & LastRow))
.PreserveFormatting = True
.AdjustColumnWidth = False
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierNone
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "" & Chr(10) & ""
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub