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.
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