View Single Post
  #15   Report Post  
Posted to microsoft.public.excel.programming
Grace[_4_] Grace[_4_] is offline
external usenet poster
 
Posts: 106
Default EXCEL VBA question

Because of some other recommendations, the code I chose was a little different from what you wrote. It works fine except for when i try to use the wbsource, saved = true and wbSource.close commands. They bomb the macro out. I've copied the macro below (not through the very end, because the problem is earlier than the end) showing the four commands in bold that, somehow, seem to be contributing to the problem. Can you tell what's wrong? I really do want to have that capability to close out all the many files i open up. I guess I don't understand the need for both myFilename (you used "F") and wbSource.

Dim wbTarget As Workbook
Dim wbSource As Workbook
Dim myFilename As Variant
Dim RngToCopy As Range

Set wbTarget = ActiveWorkbook
myFilename = Application.GetOpenFilename("All files, *.*")

If myFilename = False Then
Exit Sub 'user hit cancel
End If
Set wbSource = Workbooks.Open(myFilename)


Workbooks.OpenText Filename:=myFilename, Origin:=437, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array( _
Array(0, 1), Array(7, 3), Array(17, 1), Array(32, 1), Array(40, 1), Array(48, 1)), _
TrailingMinusNumbers:=True

Application.Goto Reference:="R14C1"
For i = 1 To 1400
Selection.End(xlDown).Select
If ActiveCell.Row = 65536 Then GoTo l_pool
ActiveCell.Resize(13).EntireRow.Delete

Next i
l_pool: Rem

Selection.End(xlUp).Select
Application.Goto Reference:="R1C1"

x = Cells(Rows.Count, "a").End(xlUp).Row
'to use in range
Range("a1:f" & x).Select
Application.Calculation = xlCalculationManual ' Turn recalc off

'ActiveCell.Range("A1:F20").Select
'Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

wbSource.Saved = True ' This closes a source workbook without saving
wbSource.Close

wbTarget.Activate
....


"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