Yeah, that's my fault. Sorry 'bout that. So's you know, I'm a beginner with VBA. The main file (your mock file) retains the focus because the other runs in the background. Therefore the ActiveCell will always be in the active window, which is the mock file; you haven't set focus to the destination file.
Quote:
Originally posted by pctsvs:
but it does however make the activecell on the sample.xls file in B4
|
Not exactly. Cell
B4 of
Sheet1 in
sample.xls is
selected not
active.
The problem here is that, because I don't know anything about Excel VBA, I didn't verify the code you were originally trying to run to see if it would work in my code model. Guess we found that one out.
For future reference, you don't have to select a cell to change its value (I knew this but thought I'd try your way so it'd be more familiar to you). All you really need is this:
oWb.Sheets("Sheet1").Range("B4").Value = "HELLO"
But let's add to that, because we're not just trying to put a string in a cell; we're trying to build a frame for a versatile script. A few modifications to the code we have thus far:
Code:
Private Sub OpenAFile()
Dim xlApp As New Excel.Application
Dim oWb As Workbook
Dim wsSource As Worksheet
Dim wsDest As Worksheet
Dim strTextToTransfer As String
Set oWb = xlApp.Workbooks.Open("C:\sample.xls")
Set wsSource = ActiveWindow.ActiveSheet
Set wsDest = oWb.Sheets("Sheet1")
'type "HELLO" into cell B4 of the mock file before
'running this macro. We'll grab that value on the
'fly.
strTextToTransfer = wsSource.Range("B4").Value
wsDest.Range("B4").Value = strTextToTransfer
With oWb
.Save
.Close
End With
Set wsSource = Nothing
Set wsDest = Nothing
Set oWb = Nothing
End Sub
Because this needs to be as dynamic as possible, we need as few literal references (like the
Range reference of
"B4") as possible. So we'll be adding more objects as we go. This time around there's a new worksheet object for the current file and the file to be opened. Also, there's a string object to hold the value that we're moving.
Sorry about the mistake last time around. I'll try to be more careful.
chris.