Running command from a different Worksheet

BritsBlitz

New Member
Joined
Jan 10, 2014
Messages
19
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
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Code:
Private Sub CommandButton21_Click()

    Dim fName As String
    Dim LastRow As Long
    
    fName = Application.GetOpenFilename("text Fiels (*.txt), *.txt")
    If fName = "False" Then Exit Sub
    
    With Worksheets("Import")
        LastRow = .Range("A" & Rows.Count).End(xlUp).Row + 1
    
        With .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 With
    
End Sub
Please use CODE tags.
 
Upvote 0

Forum statistics

Threads
1,215,325
Messages
6,124,252
Members
449,149
Latest member
mwdbActuary

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top