' Excel VBA copy using .Workbooks or .Windows OOP Method

DocAElstein

Banned user
Joined
May 24, 2014
Messages
1,336
'
''. Hello Everyone … ---o00o---`(_)`---o00o---
''.
''. I am new to Excel VBA and I am trying to understand the theory better.
''.
''. I wanted a simple direct method to copy the content (in this case a number) from a cell in one Workbook
(Mappe2)
'' into a cell in another Workbook (Mappe1). I wanted to write the code myself from an understanding of VBA OOP
'' rather than simply using the Macro Recorder
''.
''. That is (or should be) very simple and indeed I manage it. However, not all the Methods I tried worked and I do not understand why! Probably it is because I do not quite fully understand the theory or implementation of the Object/Class Orienteer Program Concept!
'. (Or is it the case that you can only take or place values in a cell in a Workbook that is not Activated,
'' using the unique
.Windows.ActiveCell Method? (Note here that you sometime need to activate the receiving Workbook, (Mappe1)
‘. but not the Workbook with the desired value in it (Mappe2) )
''. And you can not directly work on a Cell by co-ordinates at all if the Workbook is not activated?
'.
'. Maybe a Profi can help me to understand the following Codes.:-
'
''. The following 4 Codes are an attempt by me on a simple direct Method.
''. But only the forth seems to work
''.
''‚…………… ……….---o00o---`(_)`---o00o---



''.------ 1
Sub WorkbooksCellsMethodDirect()
Excel.Application.Workbooks("Mappe1.xlsm").Application.Cells(1, 1).Range("a1").Value _
= Excel.Application.Workbooks("Mappe2.xlsm").Application.Cells(1, 2).Range("a1").Value
End Sub
'.


'. 2 ------------------------------------------------------------2
'.

Sub WorkbooksActiveCellMethodDirect()
Excel.Application.Workbooks("Mappe1.xlsm").Application.ActiveCell.Range("a1").Value _
= Excel.Application.Workbooks("Mappe2.xlsm").Application.ActiveCell.Range("a1").Value
End Sub
'.


'. 3 ------------------------ 3
'.

Sub WindowsCellsMethodDirect()
Excel.Application.Windows("Mappe1.xlsm").Application.Cells(1, 1).Range("a1").Value _
= Excel.Application.Windows("Mappe2.xlsm").Application.Cells(1, 2).Range("a1").Value
End Sub
'.
'. 4 ---------------------------------------4
'.

Sub WindowsActiveCellMethodDirect()
Excel.Application.Windows("Mappe1.xlsm").Activate
Excel.Application.Windows("Mappe1.xlsm").ActiveCell.Range("a1").Value _
= Excel.Application.Windows("Mappe2.xlsm").ActiveCell.Range("a1").Value
End Sub
'.
'. ----------------------------------------
'.
'. It looks as though only The Windows Object has an extra direct
.ActiveCell under object
'. instead of only
Application.Activecell available. Somehow this makes the difference!?!
'.
' (In other words:-
Workbooks has .Application.Cells and .Application.ActiveCell.
'. Windows has also this .Application.Cells and .Application.ActiveCell,
'. ..but Windows has an extra .ActiveCell that Workbooks does not have!?!)
'.
'. Can anyone explain this to me!!?!
'.
' Thanks Alan Elston.
'. Hof, Bavaria on 23.05.2014
'. Email DocAElstein@t-online.de
'.............
'.
'. P.s. A more complicated method which activates every time the Workbook
‘. and stores the wanted cell value in a
TemporaryVariable works every time!?
'. That is to say, the following 4 Methods all work fine!!!
'. ---o00o---`(_)`---o00o---

'.-----------

Sub WorkbooksCellsMethodWithTemporaryVariable()
Dim TemporaryVariable As Double

Excel.Application.Workbooks("Mappe2.xlsm").Activate

Let TemporaryVariable _
= Excel.Application.Workbooks("Mappe2.xlsm").Application.Cells(1, 2).Range("a1").Value

Excel.Application.Workbooks("Mappe1.xlsm").Activate

Excel.Application.Workbooks("Mappe1.xlsm").Application.Cells(1, 1).Range("a1").Value _
= TemporaryVariable
End Sub
'.
'. ------------------------------------------- '.


Sub WorkbooksActiveCellMethodWithTemporaryVariable()
Dim TemporaryVariable As Double

Excel.Application.Workbooks("Mappe2.xlsm").Activate

Let TemporaryVariable _
= Excel.Application.Workbooks("Mappe2.xlsm").Application.ActiveCell.Range("a1").Value

Excel.Application.Workbooks("Mappe1.xlsm").Activate

Excel.Application.Workbooks("Mappe1.xlsm").Application.ActiveCell.Range("a1").Value _
= TemporaryVariable
End Sub
'.
'. ----------------------------------------
'.

Sub WindowsCellsMethodWithTemporaryVariable()
Dim TemporaryVariable As Double

Excel.Application.Windows("Mappe2.xlsm").Activate

Let TemporaryVariable _
= Excel.Application.Windows("Mappe2.xlsm").Application.Cells(1, 2).Range("a1").Value

Excel.Application.Windows("Mappe1.xlsm").Activate

Excel.Application.Windows("Mappe1.xlsm").Application.Cells(1, 1).Range("a1").Value _
= TemporaryVariable
End Sub
'.
'. -------------------------------------------
'.

Sub WindowsActiveCellMethodWithTemporaryVariable()
Dim TemporaryVariable As Double

Excel.Application.Windows("Mappe2.xlsm").Activate

Let TemporaryVariable _
= Excel.Application.Windows("Mappe2.xlsm").Application.ActiveCell.Range("a1").Value

Excel.Application.Windows("Mappe1.xlsm").Activate

Excel.Application.Windows("Mappe1.xlsm").Application.ActiveCell.Range("a1").Value _
= TemporaryVariable
End Sub
'. -------------------------------------------------
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

Forum statistics

Threads
1,218,047
Messages
6,140,151
Members
450,264
Latest member
LeeEad

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