+ Reply to Thread
Results 1 to 2 of 2

Macro needed to Paste Values and prevent Macro operation

  1. #1
    Registered User
    Join Date
    12-10-2004
    Posts
    7

    Macro needed to Paste Values and prevent Macro operation

    Sub Macro1()
    '
    '
    Dim myBk As Workbook
    Set myBk = Workbooks.Open(Application.GetOpenFilename(, , "Select the File"))

    Range("To_Database").Copy ThisWorkbook.Worksheets(1).Range("A65536").End(xlUp)(1).Offset(2, 1)

    myBk.Close False

    End Sub


    In the above macro I need to prevent macros operating in the External File when it opens. I also need the Named Range ("To_Database") from the External File to be copied into the file from which this macro operates as PasteValues.

    At the moment the Formula in the Named Range is coming across, so that I'm creating an External Formula Link.

    How can I do this???

  2. #2
    Dave Peterson
    Guest

    Re: Macro needed to Paste Values and prevent Macro operation

    Maybe something like...

    Option Explicit
    Sub testme01()

    Dim WkBkName As Variant
    Dim wkbk As Workbook
    Dim fRng As Range
    Dim DestCell As Range
    Dim RngName As String

    RngName = "To_DataBase"

    WkBkName = Application.GetOpenFilename(filefilter:="Excel Files, *.xls", _
    Title:="Select the File")

    If WkBkName = False Then
    MsgBox "Try later"
    Exit Sub
    End If

    Application.EnableEvents = False
    Set wkbk = Workbooks.Open(Filename:=WkBkName)
    Application.EnableEvents = True

    Set fRng = Nothing
    On Error Resume Next
    Set fRng = wkbk.Names(RngName).RefersToRange
    On Error GoTo 0

    If fRng Is Nothing Then
    MsgBox RngName & " doesn't exist in " & wkbk.FullName & "!"
    Else
    With ThisWorkbook.Worksheets(1)
    Set DestCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
    End With
    fRng.Copy
    DestCell.PasteSpecial Paste:=xlPasteValues
    End If

    Application.EnableEvents = False
    wkbk.Close savechanges:=False
    Application.EnableEvents = True

    End Sub

    The "application.enableevents = false" lines stop the workbook_open and
    workbook_beforeclose events from firing.



    thunderfoot wrote:
    >
    > Sub Macro1()
    > '
    > '
    > Dim myBk As Workbook
    > Set myBk = Workbooks.Open(Application.GetOpenFilename(, , "Select
    > the File"))
    >
    > Range("To_Database").Copy
    > ThisWorkbook.Worksheets(1).Range("A65536").End(xlUp)(1).Offset(2, 1)
    >
    > myBk.Close False
    >
    > End Sub
    >
    > In the above macro I need to prevent macros operating in the External
    > File when it opens. I also need the Named Range ("To_Database") from
    > the External File to be copied into the file from which this macro
    > operates as PasteValues.
    >
    > At the moment the Formula in the Named Range is coming across, so that
    > I'm creating an External Formula Link.
    >
    > How can I do this???
    >
    > --
    > thunderfoot
    > ------------------------------------------------------------------------
    > thunderfoot's Profile: http://www.excelforum.com/member.php...o&userid=17341
    > View this thread: http://www.excelforum.com/showthread...hreadid=378063


    --

    Dave Peterson

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1