Variable difficulties

A

Andyjim

Hi-
Can you tell me where I am wrong in regard to this code. Below I have noted
where it causes an error;



Sub MovePastTradesLoop()

'Define Variables
Dim TradesEnteredPast As Range, PastCheck As Range


With Sheets("Analysis")
Set TradesEnteredPast = Range("at17:at56")
End With


'Loop: Check for complete trades, copy to Trade History
For X = 1 To TradesEnteredPast.Count
Set PastCheck = TradesEnteredPast(X)



If PastCheck.Value = "True" Then

PastCheck.EntireRow.Select ERRORS OUT HERE
Selection.Copy
Sheets("TradeHistory").Select
Range("A4").Activate
Selection.End(xlDown).Select
ActiveCell.Offset(rowoffset:=1, columnoffset:=0).Activate
ActiveCell.EntireRow.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("A1").Select
Sheets("Analysis").Select
Range("A1").Select
Else
MsgBox ("OK") 'Goes with Else. Comment out
Exit Sub 'Goes with Else. Comment it out.
End If
Next 'Ends "For Each" Loop


End Sub
 
C

Chip Pearson

The first part of the problem lies in the following code.
With Sheets("Analysis")
Set TradesEnteredPast = Range("at17:at56")
End With

For the Range to be subordinate to the Sheets("Analysis") object in the With
statement, you *must* preceed the Range with a period. Without the period,
Range refers to a range on whatever sheet happens to be active, which is not
necessarily the "Analysis" sheet. With the period before Range, Range refers
to the sheet specified in the With statement. For example,

With Sheets("Analysis")
Set TradesEnteredPast = .Range("at17:at56") '<<< Note period before "Range"
End With

The next problem is with

PastCheck.EntireRow.Select

If the worksheet that contains the range referenced by PastCheck is not the
active worksheet, the Select method will fail. In order to Select a Range,
the worksheet containing that Range must be active. So, you could use

With PastCheck
.Worksheet.Select '<<< Note leading period before "Worksheet"
.Select ' <<< Note leading period before "Select"
End With


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2008
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top