Activate workbook based on a range reference.

S Oberlander

Board Regular
Joined
Nov 25, 2020
Messages
147
Office Version
  1. 365
Platform
  1. Windows
I have the following little macro as a shortcut on my quick access toolbar.
It works great, however it frequently turns up a different workbook when completed, instead of the workbook I just pasted the data into.
That can get very frustrating when I have 20+ wrkbks open and I have to go find the one I'm working with currently.
Can I somehow use the range captured with the input box to ensure the workbook I need stays on top?

VBA Code:
Sub copypastclose()
On Error GoTo quitit 'if input data is invalid or the user clicks cancel it will exit the macro
    Dim Original As Workbook
    Dim DataRange As Range
    Dim Newloc As Range
    Set Original = ActiveWorkbook
    Set DataRange = ActiveSheet.UsedRange
    Set Newloc = Application.InputBox("Select workbook and cell where to copy the data", "Paste", , , , , , 8)
    DataRange.Copy Newloc
    Application.CutCopyMode = False
    Original.Close savechanges:=False
quitit:
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
try this ( untested):
VBA Code:
ub copypastclose()
On Error GoTo quitit 'if input data is invalid or the user clicks cancel it will exit the macro
    Dim Original As Workbook
    Dim DataRange As Range
    Dim Newloc As Range
    Set Original = ActiveWorkbook
    Set DataRange = ActiveSheet.UsedRange
    Set Newloc = Application.InputBox("Select workbook and cell where to copy the data", "Paste", , , , , , 8)
    selname = ActiveWorkbook.Name
    DataRange.Copy Newloc
    Application.CutCopyMode = False
    Original.Close savechanges:=False
    Workbooks(selname).Activate
    
quitit:
End Sub
 
Upvote 0
You could also try
VBA Code:
Newloc.Parent.Parent.Activate
 
Upvote 0
Solution
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,045
Messages
6,122,840
Members
449,096
Latest member
Erald

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