Macro to select & open file into sheet two?

Jacko058

Board Regular
Joined
Jan 13, 2014
Messages
180
Basically, I have a macro running on sheet two.

You have paste the contents into Sheet 2 > A1 > Press CTRL+R
That brakes down all of the contents to the bits that I need.

I was wondering, Is there such thing as a macro that will just open a excel file into Sheet 2 A1 ?

Just being lazy, Save's opening the file > selecting everything > going to macro file page 2 > pasting > CTRL+R

Possible?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
you mean select another workbook and copy/paste certain sheet/cells to your sheet2?

Without actually opening the source file?

I have a file that has content in sheet 1 that brings information from sheet 2 (call this MAIN)

I basically want to open MAIN on sheet two, be able to run a macro that opens the dialog to select a file.
Then select (call it SUB) SUB to open into MAIN>sheet 2.

I have this macro, Which works fine but opens in sheet 1 which will replace all of my main data.

Code:
Sub OpenFile()
    Dim fn, e
    Dim x, y, n As Long, txt As String, flg As Boolean
    Dim i As Long, ii As Long, A(), maxCol As Long
    fn = Application.GetOpenFilename("csv,*.csv", 1, "Open CSV", MultiSelect:=True)
    If Not IsArray(fn) Then Exit Sub
    n = 1
    For Each e In fn
        If FileLen(e) > 0 Then
            txt = CreateObject("Scripting.FileSystemObject") _
            .OpenTextFile(e).ReadAll
            x = Split(txt, vbCrLf)
            ReDim A(1 To UBound(x) + 1, 1 To 20)
            For i = 0 To UBound(x)
                y = Split(CleanCSV(x(i), Chr(2), Chr(3)), ",")
                maxCol = Application.Max(maxCol, UBound(y) + 2)
                If maxCol > UBound(A, 2) Then
                    ReDim Preserve A(1 To UBound(A, 1), 1 To maxCol)
                End If
                For ii = 0 To UBound(y)
                    A(i + 1, ii + 1) = y(ii)
                Next
            Next
            Sheets(1).Cells(n, 1).Resize(UBound(A, 1), maxCol).Value = A
            n = n + UBound(A, 1)
        End If
    Next
    With Sheets(1).UsedRange
        .Replace Chr(2), ",", xlPart
        .Replace Chr(3), vbNullString, xlPart
    End With
End Sub
 
 
Function CleanCSV(ByVal txt As String, ByVal subComma As String, _
    ByVal subDQ As String) As String
    Dim m As Object
    Static RegX As Object
    If RegX Is Nothing Then Set RegX = CreateObject("VBScript.RegExp")
    With RegX
        .Pattern = "(^|,)(""[^""]+"")(,|$)"
        Do While .test(txt)
            Set m = .Execute(txt)(0)
            txt = Application.Replace(txt, m.firstindex + 1, _
            m.Length, m.submatches(0) & Replace(Replace(m.submatches(1), _
            ",", subComma), """", subDQ) & m.submatches(2))
        Loop
    End With
    CleanCSV = txt
End Function
 
Last edited:
Upvote 0
I had the same type of problem, I put the Macro into SHEET 2 but I tried to open and run the macro while I was viewing SHEET 1. This code was added after the Dim statements were finished. Import is SHEET 2. Maybe your problem is the macro is on SHEET 1, try to copy, actually CUT it out of SHEET 1 and paste it into SHEET 2, it looks like you just have it on the wrong sheet to me??? With my code, if I try to run it from SHEET 1, SHEET 2 will be activated first before continuing to run the rest of my macro, but I think your situation is a little different???


Dim lrb As Long, lrc As Long, c As Range
With Sheets("Import")
.Activate
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,509
Members
448,967
Latest member
screechyboy79

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