Issues with Application.Goto

lostitagain

New Member
Joined
Jul 5, 2012
Messages
21
Running Excel 2010
Here is my code:
Code:
   'Select the correct Range to copy and copy into the sheet
   Set FromRange = Workbooks(Info.FromBook).Worksheets(TEMPLATESHEET).Range(NumHeads)
   Set ToRange = Workbooks(Info.ToBook).Worksheets(Info.ToSheet).Range("A" & RowTo)
   'Copy the template
   Application.Goto FromRange
   Selection.Copy
   Application.Goto ToRange
   Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
      SkipBlanks:=False, Transpose:=False
   Worksheets(Info.ToSheet).Paste
   FromRange = Workbooks(Info.FromBook).Worksheets(Info.RuleSet).Range("E" & FoundRow)


This code copies a template to a worksheet from a different workbook. Each template has a named range and the one chosen is determined by the variable 'NumHeads'.
Whenever I run the program I get a Runtime Error 1004 Method Goto of object application failed.

Info is a custom class that I created and it works fine with all other modules

It worked properly for a few minutes and then randomly quit working. I think the problem is either in the assigning a value to the FromRange variable or in the Application.Goto statement.


Any help is appreciated
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Were are you giving FoundRow a value?

Also, is Info.RuleSet a worksheet name?
 
Upvote 0
FoundRow is given a value earlier by calling a sub that searches the workbook for a matching entry and reports the row it is found on. This sub is working correctly.

Yes, Info.RuleSet is a string value that is a valid name of a worksheet.

It should also be noted that the debugger highlights this row:

Code:
Application.Goto FromRange
 
Upvote 0
So it's that row causing the error?

By the way, why are you using Goto instead of a straight copy/paste?
Code:
FromRange.Copy ToRange
 
Upvote 0
No, that was just another error that I caught.

I'm using a Goto because I want to use PasteSpecial so that I can maintain my formatting and column widths of the template.

Your code works but I lose the column widths.
 
Upvote 0
I don't see why you would need to use Goto for that.

If you also want to paste special the column widths just add that after the copy.
Code:
Range.Copy ToRange

FromRange.Copy

ToRange.PasteSpecial Paste:=xlPasteColumnWidths
 
Upvote 0
Here's what ended up working:

Code:
 'Select the correct Range to copy and copy into the sheet
   Set TempRange = Workbooks(Info.FromBook).Worksheets(TEMPLATESHEET).Range(NumHeads)
   Set ToRange = Workbooks(Info.ToBook).Worksheets(Info.ToSheet).Range("A" & RowTo)
   
   'Copy the template
   TempRange.Copy ToRange
   TempRange.Copy
   ToRange.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
   'Set to copy from the found entry
   Set FromRange = Workbooks(Info.FromBook).Worksheets(Info.RuleSet).Range("E" & FoundRow)

I had to change the name of the first FromRange to TempRange as it seemed to conflict.

Thank you for the help.
 
Upvote 0

Forum statistics

Threads
1,213,507
Messages
6,114,029
Members
448,543
Latest member
MartinLarkin

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