VBA not working in .exe format

The following code works in regular excel file, but in converted .exe file not working:

Sub PASTEIT()
On Error Resume Next
Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Activate
ActiveWindow.ActivateNext
Selection.Copy
ThisWorkbook.Activate
ActiveCell.PasteSpecial Paste:=xlPasteValues
ActiveWindow.ScrollRow = Range("A" & Rows.Count).End(xlUp).Row
End Sub

How can I change thi code to make it work after compilation?

What does happen if you remove On Error Resume Next ?

Not working after On Error Resume Next removed.
I cannot activate another workbook from compiled application. I need to copy selected range from another excel workbook to compiled application.
I guess I must to apply your code to activate another workbook. I checked the code in section 10 of guide:

image

But, I need to copy a range from previusely activated workbook (ActiveWindow.ActivateNext), which may be located in any folder.

I cannot activate another workbook from compiled application

How do you try to activate the other workbook? Are you trying to open it with VBA?

I open another workbook in regular way from any location (it maybe any folder in computer or removeable drive). I select a range on another workbook go to .exe application and run the macro and use ActiveWindow.ActivateNext command to activate another workbook and copy range and paste to .exe application. It works fine if both copy from and paste to workbooks are excel or both .exe applications. But not working if I copy from regular excel to .exe application.

I read a post How to replace Workbooks(1) with the .exe file in a macro and author mentioned, that he could manage to copy from excel to compiled application, but I could not.

If you were to open another exe protected file from your original exe - (Need to declare public variables first:)

Public Declare Function ShellExecute _
Lib “shell32.dll” _
Alias “ShellExecuteA” ( _
ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) _
As Long

Public DestinationWorkbook As Workbook
Public SourceWorkbook As Workbook

Public Function PathToFile(Filename As String)
Dim XLSPadlock As Object
On Error GoTo Err
Set XLSPadlock = Application.COMAddIns(“GXLSForm.GXLSFormula”).Object
PathToFile = XLSPadlock.PLEvalVar(“EXEPath”) & Filename
Exit Function
Err:
PathToFile = “”
End Function

Then in your sub:
Set DestinationWorkbook = ActiveWorkbook

'Open required file
Dim AnalysisFilepath As String
AnalysisFilepath = PathToFile(“FileName.exe”)

    Application.DisplayAlerts = False
    Application.AskToUpdateLinks = False
    
    'Call Shell(AnalysisFilepath) doesnt work in exe file
            
    Dim strAction As String
    strAction = "OPEN" ' action might be OPEN, NEW or other, depending on what you need to do
    lngErr = ShellExecute(0, strAction, AnalysisFilepath, "", "", 0)

'Assign opened file
Set SourceWorkbook = ActiveWorkbook

Then place your normal code
Hope this helps another. method 2 to follow

Method 2: Open a standard excel from protected exe
Public DestinationWorkbook As Workbook
Public SourceWorkbook As Workbook

'Set file path
Set DestinationWorkbook = Application.ActiveWorkbook

Dim XLSPadlock As Object
Set XLSPadlock = Application.COMAddIns("GXLSForm.GXLSFormula").Object
Filepath = XLSPadlock.PLEvalVar("EXEPath")
'Filepath = Application.ActiveWorkbook.Path  'For unprotected testing

Filepath = Filepath & "Export\Repair\"  'Assign desired default path

'only allow the user to select one file
With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = False
.Title = “Select File to be Imported.”
.InitialFileName = Filepath
.Filters.Clear
.Filters.Add “Back Up file”, “*.xlsx”
End With

'make the file dialog visible to the user
    intChoice = Application.FileDialog(msoFileDialogOpen).Show

'determine what choice the user made
If intChoice <> 0 Then
    'get the file path selected by the user
    SourceFile = Application.FileDialog( _
        msoFileDialogOpen).SelectedItems(1)
    'MsgBox "File selected"
Else
    MsgBox "No File Selected - Import cancelled"
    Application.Run "Go_To_Main_Menu"
    Exit Sub
End If


Workbooks.Open SourceFile
'DestinationFilepath = Application.ActiveWorkbook.Path
Set SourceWorkbook = ActiveWorkbook

Dear adworsak,

Thanks for the code, but I could not use it for my task.
Your code seems not executing what I want.
Let me explain you what I want.

  1. I open standart excel file (.xlsx, .xslb, .xlsm) from any location (C, D, Flash Drive) by regular way, not with macro and select a range of cells
  2. I go to application (.exe) and run a macro.
    The macro activates standart excel file-copy selected range-activate .exe application-paste values to active cell.
    The macro works if both files are standart excel workbooks or both files are .exe applications. But not working if Application - Standart Excel.
    Here is my code:
    Sub PASTEIT()
    On Error Resume Next
    Application.ScreenUpdating = False
    ActiveWindow.ActivateNext
    Selection.Copy
    ThisWorkbook.ACTIVATE
    ActiveCell.PasteSpecial Paste:=xlPasteValues
    Range(“D” & Rows.Count).End(xlUp).Offset(1, -2).ACTIVATE
    ActiveWindow.ScrollRow = Range(“D” & Rows.Count).End(xlUp).Offset(-5, 0).Row
    Application.ScreenUpdating = True
    End Sub

Please, help me with this code.
I need this code ASAP, because I have difficulties with copy-paste in regular way.
Or if you know someone who can perform this task for payment, Please advice me.

As asked before, since you removed “On Error Resume Next”, do you get any error?
The problem may arise from ActiveWindow.ActivateNext, since the two workbooks are not in the same Excel instance.

Copy Paste is not working, only activates and scrolls to last cell
The macro drops out copy paste. Because ActiveWindow.ActiveNext is not working, and no error info.

Alternatively would be fine if there’s a solution for copy from external source and paste values without loss of any data. Currently I can paste only visible data as a text. I need to paste full data, if I copy digits (0.32423) and if it’s rounded as (0.3) I copy paste incorrect data.

The sole workaround to this copy and paste problem would be exporting the data to copy to a third workbook file (not protected) and then have the second Excel instance open that workbook file, read data and close it.

The problem is I cannot paste data from unprotected excel to protected file (.exe). I cannot understand your answer. Is there a sample file or code?