EXCEL VBA question
Hi Grace
Lots of things here. I'll try:
First, do I replace the name of the worksheet I want to copy from in place
of "Sheets(1)".
wbSource.Sheets("Inputs").Range("C11:E12").Copy
Also, do I need to use your:
wbSource.Close
after each paste operation, or just at the end?
Well, it closes the newly opened workbook. If you use this in a loop with
multiple workbook openings then wbSource will be reassigned to a new file
each time and your code won't keep track of the rest of them. But if you
copy several ranges from a single workbook, don't close it until all are
done.
Also what would be the command for unprotecting a worksheet
before copying?
wbTarget.Sheets("Inputs").Unprotect
'actions
wbTarget.Sheets("Inputs").Protect
or with a passwords:
wbTarget.Sheets("Inputs").Unprotect ("GraceLockedMe")
'actions
wbTarget.Sheets("Inputs").Protect ("GraceLockedMe")
HTH. Best wishes Harald
"Grace" skrev i melding
...
This certainly seems a lot quicker and I think I realize you don't want to
teach me inefficient programming. However, what little I do know is just
that so, going this route will require more clarifications, so please bear
with me!
First, do I replace the name of the worksheet I want to copy from in place
of "Sheets(1)". I tried that for a worksheet named "inputs" as follows:
wbSource.Inputs.Range("C11:E12").Copy wbTarget.Inputs.Range("C11:E12")
and it bombed the macro. If not, how do I identify the worksheet name?
Also, do I need to use your:
wbSource.Close
after each paste operation, or just at the end? If not, what is this
command doing? Also what would be the command for unprotecting a
worksheet
before copying?
Thanks,
G
"Harald Staff" wrote in message
...
Grace
I didn't answer your question because I believed it was the wrong
solution
to a problem. What you want done is to copy from one place to another,
not
to activate windows. So I still won't tell you, but see if this does
what
you want:
Sub test()
Dim F As Variant
Dim wbSource As Workbook
Dim wbTarget As Workbook
Set wbTarget = ThisWorkbook 'or ActiveWorkbook
F = Application.GetOpenFilename("Workbooks (*.xls), *.xls", _
, "Select a file to copy into:")
If F = False Then Exit Sub
Set wbSource = Workbooks.Open(F)
wbSource.Sheets(1).Range("A1:F14").Copy
wbTarget.Sheets(2).Range("D1:I14")
wbSource.Saved = True
wbSource.Close
End Sub
OR: Do you need to manually select a range in the middle of your macro ?
HTH. Best wishes Harald
"Grace" skrev i melding
...
Perhaps I am confused but I don't think you have answered my question.
What
I do is copy data from a source file to the same location in a
destination
file. I do this for many different areas on many different tabs.
So, I go to the source file (which you have called F) and I highlight,
say,
worksheet tab 1, then a range of cells on that tab. Then I activate
the
destination file, find the same tab and same cell area and paste it.
Then, I go back to the source file and repeat for another tab and
area.
So,
each time I am toggling between two files. I was trying to find a way
to
call the source file each time. I thought the command:
Windows(F.xls).Activate
would work but it bombs the macro out. What is the right syntax to
toggle
back to that sheet?
Thanks,
Grace
"Harald Staff" wrote in message
...
"Grace" skrev i melding
...
I know that before
each copy operation, I don't want to re-open the file, but would
rather
activate it.
Usually one doesn't have to select or activate anything to move/copy
stuff
from one place to another. It just slows things down while looking
ugly.
Best wishes Harald
|