How to activate already open workbook through VBA code

Daiwik

New Member
Joined
Jan 12, 2016
Messages
15
Hi All,

I'm very new in writing VBA. Please help me in activating already open workbook. Following is the code i'm using but don't know how to complete it. Please help.

Sub AssessmentDataPaste()
Dim MyPath As String

MyPath = Sheets("Main").Range("C7").Value

' Pop-up a msgbox if NO path is available in the field.
If MyPath = "" Then
MsgBox ("Please provide a valid destination/file path.")
End If

Workbooks.Open Filename:=MyPath

Range("F12:F3000").Select
Selection.Copy
Workbooks("Ingestion_Template.xlsm").Activate
Sheets("IngestionTemplate").Activate
Range("B7").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B7").Select


Workbooks.Activate
Range("P12:P3000").Select
Selection.Copy
Workbooks("Ingestion_Template.xlsm").Activate
Sheets("IngestionTemplate").Activate
Range("C7").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("C7").Select


End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi There

Replace the line by this

Code:
Workbooks("workbookname").Activate

that should do the trick


regards
 
Last edited:
Upvote 0
Hi daivik!

I suggest this:
Code:
Sub AssessmentDataPaste()
Dim MyPath As String

MyPath = Sheets("Main").Range("C7").Value

' Pop-up a msgbox if NO path is available in the field.
If MyPath = "" Then
MsgBox ("Please provide a valid destination/file path.")
End If

Workbooks.Open Filename:=MyPath
[COLOR=#ff0000]owbn = Activeworkbook.Name  'this will add the opened workbook name to a variant[/COLOR]

Range("F12:F3000").Select
Selection.Copy
Workbooks("Ingestion_Template.xlsm").Activate
Sheets("IngestionTemplate").Activate
Range("B7").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B7").Select


Workbook[COLOR=#ff0000]s(owbn).A[/COLOR]ctivate
Range("P12:P3000").Select
Selection.Copy
Workbooks("Ingestion_Template.xlsm").Activate
Sheets("IngestionTemplate").Activate
Range("C7").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("C7").Select


End Sub
 
Upvote 0
Here is a example of how you can do things without needing to activate and select.

Code:
Sub Test_Me()
Workbooks("Book1").Sheets(1).Range("A1").Copy Destination:=Workbooks("Book2").Sheets(1).Range("A1")
End Sub
 
Upvote 0
Or if you want to use PasteSpecial try this:

Code:
Sub Test_Me()
Workbooks("Book1").Sheets(1).Range("A1").Copy
Workbooks("Book2").Sheets(1).Range("A2").PasteSpecial xlPasteValues
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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