VBA calls another Workbook and after user input returns to Sub in orginal Workbook Error

webharvest

New Member
Joined
Apr 20, 2010
Messages
16
I've written some VBA that does the following:
1) User hits "button" in Original Workbook (WB) that initiates the Sub named GetDataFromWB2 ()
2) I have it clear any data in the cells I will be pasting new data in
3) I have the code ask the user to open the workbook that contains the data to be copied and display a message telling the user to select the new data to be copied and hit Enter when done.
4) My intent was this would "pause" the VBA until the user selected the data and once they hit enter it would call the Sub PasteSpecAnData into the right cells of the original workbook (WB)

See the code below. Everything works up to the point where I hit enter and it calls the PasteSpecAnData Sub. I get the following message popup while in WB2 that the user selects "The macro may not be available in this workbook or all macros may be disabled". It list out the full directory path to the macro which includes the workbook name.

Any help would be greatly appreciated. I'm fine with someone saying I'm making this way to hard also and there is an easier way to do this.


Code:
Sub ClearSpecAnData()
'
' ClearSpecAnData Macro
' Clear User Data from Worksheet
'
    Application.ScreenUpdating = False
    Range("C14:D12002").Select
    Selection.ClearContents
    Range("C14").Select
    Application.ScreenUpdating = True
End Sub

Sub PasteAfterEnter()
'Paste selected text after Enter Key is pressed
    Application.OnKey "{Enter}", "PasteSpecAnData"
    Application.OnKey "{~}", "PasteSpecAnData"
End Sub

Sub GetDataFromWB2()
'Call Sub to clear existing spectrum anlyzer data
Call ClearSpecAnData

'Last cell in column
Dim WS As Worksheet

Set WS = Worksheets("NTIA")

Dim wb As Workbook, wb2 As Workbook
Dim vFile As Variant

'Set source workbook
Set wb = ActiveWorkbook

'Open the target workbook
vFile = Application.GetOpenFilename("Excel-files,*.xlsx", _
    1, "Select One File To Open", , False)

'if the user didn't select a file, exit sub
If TypeName(vFile) = "Boolean" Then Exit Sub
Workbooks.Open vFile

'Set selectedworkbook
Set wb2 = ActiveWorkbook
MsgBox "Select the Spectrum Analyzer Data you want to copy with the Frequency (Hz) being in the first column and the Amplitude (dB)being in the second column then hit Enter"
Call PasteAfterEnter

'wb2.Worksheets("Plot").Range("B12:C2012").Select
End Sub

Sub PastSpecAnData()
'Reset the Enter keys to normal behaviour.
Application.OnKey "{Enter}"
Application.OnKey "~"
'Select cells to copy
Selection.Copy
'Go back to original workbook you want to paste into
wb.Activate

'Paste starting at the last empty row
wb.WS.Range("SpecAnDataStart").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Application.CutCopyMode = False
Application.ScreenUpdating = True

'Close and save the workbook you copied from
wb2.Close

End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
The reason this isn't working is it tries to finish running the macro in the new spreadsheet to copy the cells and I get the following error:
Can not run the macro "c:\........\PasteSpecAnData" The macro may be not be available in this workbook or all macros may be disabled.

Can anyone give me some ideas on how I can have a macro in one spreadsheet tell the user to open another spreadsheet then the macro will continue after they select text in the new spreadsheet and copy the data in the new spreadsheet to the clipboard?

Thank you
 
Upvote 0
Any help would be appreciated. I've tried moving the VBA into ThisWorksheet, modules and etc. I just can't get it to play. I've even tried removing the coping part of the VBA so when I open WB2 I manually copy the data then manually close WB2 and open the original WB1 before hitting ~. I still get the same error.

This one is killing me.
 
Upvote 0

Forum statistics

Threads
1,203,242
Messages
6,054,349
Members
444,717
Latest member
melindanegron

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