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

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
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,214,545
Messages
6,120,132
Members
448,947
Latest member
test111

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