Results 1 to 3 of 3

Thread: [RESOLVED] [Excel 2000/2003] Spreadsheet cell location

  1. #1

    Thread Starter
    Lively Member datapard's Avatar
    Join Date
    May 2008
    Location
    Silicon Valley, CA
    Posts
    107

    Resolved [RESOLVED] [Excel 2000/2003] Spreadsheet cell location

    Okay, what I'm trying to do.

    Need to be able to run a loop that checks contents of cells or lack thereof.

    That's the easy part.

    My problem comes from the fact that I have to have the macro start at A1 and work down the column from there, with A1 and an unspecified number of cells below it empty before I reach the first "live" cell. Once I reach that cell, everything is fine.

    macro Code:
    1. Sub dataFill()
    2.   Sub dataFill()
    3.     Dim strIs, strFill As String
    4.     Dim i As Integer
    5.     i = rngCount()
    6.     Range("A1").Select
    7.    
    8.     Do While i <> 0
    9.         strIs = ActiveCell.Value
    10.        
    11.         If strIs = "" Then
    12.             ' if ActiveCell position is A1 then
    13.             '     ActiveCell.Offset(1,0).select
    14.             '     i=i-1
    15.             ' else
    16.             '     strFill = ActiveCell.Offset(-1,0).value
    17.             '     ActiveCell.Value=strFill
    18.             '     ActiveCell.Offset(1,0).select
    19.             '     i=i-1
    20.             ' end if
    21.         Else
    22.             ActiveCell.Offset(1, 0).Select
    23.             i = i - 1
    24.         End If
    25.     Loop
    26. End Sub
    If you have to do it more than once...
    Automate it!

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Excel 2000/2003] Spreadsheet cell location

    there are several ways to get the first active row
    vb Code:
    1. frow = sheets("sheet1").usedrange.row
    2.   ' loop only rows in used range, if any column in row 1 has data frow will be 1
    3. lrow = sheets("sheet1").usedrange.rows - frow
    4. for r = frow to lrow

    or

    vb Code:
    1. frow = range("a1").end(xldown).row
    2. lrow = range("A65535").end(xlup).row
    3. '  will get the first and last used row of specific column

    you should avoid selecting each cell, just loop through the cells by address

    vb Code:
    1. dim c as range
    2. for each c in range("a" & frow & ":a" & lrow)
    3.   if isempty(c) then c = c.offset(-1,0)
    4. ' not sure that this is what you actually want to do but may help get the general idea
    5. next
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Lively Member datapard's Avatar
    Join Date
    May 2008
    Location
    Silicon Valley, CA
    Posts
    107

    Smile Re: [Excel 2000/2003] Spreadsheet cell location

    Quote Originally Posted by westconn1 View Post
    there are several ways to get the first active row
    vb Code:
    1. frow = sheets("sheet1").usedrange.row
    2.   ' loop only rows in used range, if any column in row 1 has data frow will be 1
    3. lrow = sheets("sheet1").usedrange.rows - frow
    4. for r = frow to lrow

    or

    vb Code:
    1. frow = range("a1").end(xldown).row
    2. lrow = range("A65535").end(xlup).row
    3. '  will get the first and last used row of specific column

    you should avoid selecting each cell, just loop through the cells by address

    vb Code:
    1. dim c as range
    2. for each c in range("a" & frow & ":a" & lrow)
    3.   if isempty(c) then c = c.offset(-1,0)
    4. ' not sure that this is what you actually want to do but may help get the general idea
    5. next
    All snippets look like they would work and I intend to file the other two for possible future use, but solution #2 as a function works perfectly.

    As always westconn your solutions are fantastic.

    Thak you

    datapard
    If you have to do it more than once...
    Automate it!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width