UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

3 Pages V   1 2 3 >  
Reply to this topicStart new topic
> Loop Certain Number Of Times, Office 2010    
 
   
bakersburg9
post Mar 26 2012, 01:26 PM
Post #1

UtterAccess Ruler
Posts: 4,237
From: Downey, CA



This code runs all the way to the end of my worksheet, evaluating EVERY cell in column G - I want it to only go say 679 iterations, but I can't get the syntax right - currently, it runs through every row - all 1,048,000+

CODE
Dim lstrow As Variant
ActiveCell.SpecialCells(xlLastCell).Select
lstrow = ActiveCell.Row

Range("G2").Select

Do Until ActiveCell.Row = lstrow + 1

If IsEmpty(ActiveCell) = True Or IsDate(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
Else

If IsEmpty(Range("ListsNotes!g9")) = True Or IsDate(Range("ListsNotes!g9")) = False Then
MsgBox "There is no date to compare"
Exit Sub
Else

If ActiveCell < DateValue(Range("ListsNotes!g9")) Then
ActiveCell.Offset(0, -1).Select
ActiveCell.FormulaR1C1 = "D"
ActiveCell.Offset(1, 1).Select
    Else

ActiveCell.Offset(0, -1).Select
ActiveCell.FormulaR1C1 = "A"
ActiveCell.Offset(1, 1).Select

End If

End If

End If

Loop


'Go to top
Range("A1").Select

End Sub
Go to the top of the page
 
+
Bob G
post Mar 26 2012, 01:30 PM
Post #2

UtterAccess VIP
Posts: 8,135
From: CT



that sounds peculiar. save and close the workbook.
open the workbook manually
press the Home End Home keys in order. What Cell does it take you to??

Do not run any macro other anything else, just open the workbook and do the home end home.
Go to the top of the page
 
+
bakersburg9
post Mar 26 2012, 01:36 PM
Post #3

UtterAccess Ruler
Posts: 4,237
From: Downey, CA



QUOTE (Bob G @ Mar 26 2012, 06:30 PM) *
that sounds peculiar. save and close the workbook.
open the workbook manually
press the Home End Home keys in order. What Cell does it take you to??

takes me to X1048576
Go to the top of the page
 
+
Bob G
post Mar 26 2012, 01:38 PM
Post #4

UtterAccess VIP
Posts: 8,135
From: CT



then you have something physically in that cell
Go to the top of the page
 
+
bakersburg9
post Mar 26 2012, 01:47 PM
Post #5

UtterAccess Ruler
Posts: 4,237
From: Downey, CA



QUOTE (Bob G @ Mar 26 2012, 06:38 PM) *
then you have something physically in that cell

Nothing visible - Is there a way to clear all cells ? I tried simply doing a delete row - putting my cursor at the end of the data (Visible data), and doing a CTRL+SHIFT+END, then did a delete row, but I guess that didn't delete everything.

Steve
Go to the top of the page
 
+
Bob G
post Mar 26 2012, 01:52 PM
Post #6

UtterAccess VIP
Posts: 8,135
From: CT



highlight all of the data you actually have and copy it to a new worksheet
Go to the top of the page
 
+
ipisors
post Mar 26 2012, 01:53 PM
Post #7

UtterAccess Certified!
Posts: 6,930
From: Arizona, United States



Steve:
CODE
takes me to X1048576

1048576 being the last cell in the worksheet, it means you do NOT have anything in any of the cells.

Steve, which specific part of the code you posted are you expecting to stop at 679 iterations?

You might try something like:

Dim x as Long
For x = 0 to 679
....Your Code
Next x

to limit it to 679.

This post has been edited by ipisors: Mar 26 2012, 01:55 PM
Go to the top of the page
 
+
Bob G
post Mar 26 2012, 01:56 PM
Post #8

UtterAccess VIP
Posts: 8,135
From: CT



isaac

not sure what you mean. forgetting any vba whatsoever, home-end-home takes you to the last used cell for the current session of that worksheet. if you delete it, then save the worksheet and open it up again it wont take you to that position again.

if you open a brand new worksheet, and do home-end-home as the very first thing, your cursor shouldnt move from A1.
Go to the top of the page
 
+
ipisors
post Mar 26 2012, 02:01 PM
Post #9

UtterAccess Certified!
Posts: 6,930
From: Arizona, United States



I don't think all users have the exact same experience with Home End Home. For me personally it doesn't work necessarily as you described. But to me the point is, if you are doing any (any one of a number of ways to: ) get to the last cell in the workbook, and if it takes you straight to row 1048576, that just means nothing was in between. 1048576 is the last physical row in v. 2007 and greater. I know we seem to discuss this 'last cell' concept a lot. I'm firmly against a top-down method of finding the last cell, I find it to be inaccurate or at best, just very misleading. Going from the bottom up is more reliable because then you know for SURE you're getting the BOTTOM MOST used cell. Just my opinion.

So if I were going to go the keystroke method, which I don't , but if I were, it would focus more on Ctrl+Home and Ctrl+End. But always starting at the bottom row and going up, not the top and going down.
Go to the top of the page
 
+
Bob G
post Mar 26 2012, 02:07 PM
Post #10

UtterAccess VIP
Posts: 8,135
From: CT



remember, i am not talking about using it in VBA for the moment. just the simple usage of the microsoft method to reach the end of the active worksheet.

QUOTE
Ctrl+End or End, Home
Cell in the worksheet at the intersection of the last column that has data in it and the last row that has data in it
(that is, the last cell of the so-called active area of the worksheet).
Go to the top of the page
 
+
ipisors
post Mar 26 2012, 02:09 PM
Post #11

UtterAccess Certified!
Posts: 6,930
From: Arizona, United States



I see what you mean, I just don't use top-down methods as I've found them to be unreliable. Sorry that's what I meant (clarifying). I'm thinking that the OP obviously doesn't have data in 1048576, so something else is going on. Probably, hitting Ctrl+End twice, or "one time more" than takes you to the last row..
Go to the top of the page
 
+
DanielPineault
post Mar 26 2012, 02:11 PM
Post #12

UtterAccess VIP
Posts: 1,457



I avoid the method you use to determine the last row (top down approach) because it can be flawed (as experience has proven). I much prefer using

CODE
Dim iLastRow As Long
iLastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row


See http://www.devhut.net/2011/05/18/ms-excel-...rmine-last-row/ for a little more info.
Go to the top of the page
 
+
ipisors
post Mar 26 2012, 02:13 PM
Post #13

UtterAccess Certified!
Posts: 6,930
From: Arizona, United States



I agree - sometimes 'invisible content' can confuse the heck out of a person.

So to take Daniel's post and then translate it into, if you prefer, a "non-VBA" solution, it might be like this:

Go to 1048576 and then do End, Home, or Ctrl+UpArrow if you know which column.
Go to the top of the page
 
+
Bob G
post Mar 26 2012, 02:13 PM
Post #14

UtterAccess VIP
Posts: 8,135
From: CT



Daniel,

outside of VBA, just using keystrokes, have you ever seen end,home not work ?
Go to the top of the page
 
+
norie
post Mar 26 2012, 02:14 PM
Post #15

UtterAccess VIP
Posts: 4,296



If you want to loop through a range why not use a For Next with the last row in the range.
CODE
For I  = 2 To lstrow
     MsgBox Range("D" & I).Value
Next I

Go to the top of the page
 
+
bakersburg9
post Mar 26 2012, 03:04 PM
Post #16

UtterAccess Ruler
Posts: 4,237
From: Downey, CA



QUOTE (norie @ Mar 26 2012, 07:14 PM) *
If you want to loop through a range why not use a For Next with the last row in the range.
CODE
For I  = 2 To lstrow
     MsgBox Range("D" & I).Value
Next I

caught in a loop - just evaluates g2 and g3, over and over
Go to the top of the page
 
+
norie
post Mar 26 2012, 03:10 PM
Post #17

UtterAccess VIP
Posts: 4,296



It's probably not really the loop that's causing the problem, it's more likely to be the code within the loop.

In particular the use of ActiveCell and Select.

What in words is the code meant to do?

I've actually tried to work that out but all that Selecting hurts my head and I lose track of whether it's coming, going or moving in other mysterious ways.(IMG:style_emoticons/default/dazed.gif)

Bob G

I've seen it not working.
Go to the top of the page
 
+
bakersburg9
post Mar 26 2012, 03:25 PM
Post #18

UtterAccess Ruler
Posts: 4,237
From: Downey, CA



QUOTE (norie @ Mar 26 2012, 08:10 PM) *
What in words is the code meant to do?


first of all, there is a date value typed in cell G9 in a worksheet named "ListsNotes" - the date is meant to be a threshold - it's currently 2/15/2012 - that's the cutoff date where if a report is prior to that date, the 'code' in col. F s/b "D" for delete - otherwise, it should be "A" active. In the future, I want to complicate things a bit, but I can't even get this to work :-(

We'll call that value the "TDATE," or threshholdDate

OK, in english:

Start in cell G2 - right now, there are 3 scenarios:

1. if value in G2 is empty or not a date,then ACTIVECELL.OFFSET(1,0) <aka go down one cell to evaluate cell G3>

if value in G2 IS a date, then evaluate it....
2. if it's LESS THAN THE TDATE, then go to the left one cell to the adjacent cell in col. F, and type a "D"

3. if it's GREATER THAN THE TDATE, then go to the left one cell to the adjacent cell in col. F, and type an "A"

go back to column G, and move down 1 cell

repeat 700 times
Go to the top of the page
 
+
DanielPineault
post Mar 26 2012, 03:42 PM
Post #19

UtterAccess VIP
Posts: 1,457



No, I can't say that I have.
Go to the top of the page
 
+
bakersburg9
post Mar 26 2012, 03:44 PM
Post #20

UtterAccess Ruler
Posts: 4,237
From: Downey, CA



QUOTE (ipisors @ Mar 26 2012, 07:13 PM) *
I agree - sometimes 'invisible content' can confuse the heck out of a person.

So to take Daniel's post and then translate it into, if you prefer, a "non-VBA" solution, it might be like this:

Go to 1048576 and then do End, Home, or Ctrl+UpArrow if you know which column.

so go from the bottom up ......... how would you know when you reached cell G1, and time to stop evaluating ?
Go to the top of the page
 
+

3 Pages V   1 2 3 >
Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 22nd May 2013 - 01:58 PM