+ Reply to Thread
Results 1 to 8 of 8

Why must the starting cell be above the range for this to work?

  1. #1
    Registered User
    Join Date
    05-02-2006
    Posts
    95

    Why must the starting cell be above the range for this to work?

    Hello,
    This code looks for each blank cell and copies the exact formula located right above it. But if the activecell is located within the range, then it doesn't work or sometimes, it partially copies the above formula then continues on leaving some other cells blank. Why must I have the starting line of beginning at cell A2 for this to work?

    Please Login or Register  to view this content.
    By the way, if the first line was to be Range("A1").Select, it will fail. Don't know why.

    Thanks in advance,
    Ricky
    Last edited by mudraker; 06-08-2007 at 09:22 PM.

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Try

    Please Login or Register  to view this content.
    I've changed the variable from cell to ce as cell is a reserved word in excel - saves confusion.

    HTH

    rylo

  3. #3
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885
    Just so you know why it was failing if you used 'Range("A1").Select', because your code was calling 'ActiveCell.Offset....' and even though your function was specifying a different range, the first time it runs through the loop, the 'ActiveCell' would be A1.

    You could literally have set that Select statement to any cell below row 1 and it would have worked. B1, C1, etc. would all have failed.

  4. #4
    Registered User
    Join Date
    05-02-2006
    Posts
    95
    Quote Originally Posted by rylo
    Hi

    Try

    Please Login or Register  to view this content.
    I've changed the variable from cell to ce as cell is a reserved word in excel - saves confusion.

    HTH

    rylo
    hi Rylo,
    How do I alter the copy destination line to copy the exact formula of the above cell? I am still encountering some blanks after running this code. It doesn't fill in all blanks within my range.


    Thanks in advance,
    Ricky

  5. #5
    Forum Contributor
    Join Date
    01-10-2006
    Location
    Ahmedabad, India
    MS-Off Ver
    Office 2000
    Posts
    346
    Quote Originally Posted by ExcelQuestion
    Hello,
    This code looks for each blank cell and copies the exact formula located right above it. But if the activecell is located within the range, then it doesn't work or sometimes, it partially copies the above formula then continues on leaving some other cells blank. Why must I have the starting line of beginning at cell A2 for this to work?

    Please Login or Register  to view this content.
    By the way, if the first line was to be Range("A1").Select, it will fail. Don't know why.

    Thanks in advance,
    Ricky
    When you refer to any cell within a for each loop, the referenced cell does not become active cell. A cell becomes active only if you select it or you have a code cell.Activate. Thus through the whole loop the active cell remains A2 which you have selected at the begining. Therefore if you select A1 then offset (-1,0) refers to a nonexistant cell hence the error.

    Cell(ActiveCell.Offset(-1,0)) is a wrong syntax. Though it does not give error it may give unpredictable result. Correct syntax is ActiveCell.Offset(-1,0).Formula. Also since cell is a reserved word let us use ce ( though in your case it does not affect ). Selection of any cell at the begining is really not required. Your sub should be

    Please Login or Register  to view this content.
    This should copy "exact" formula the way you want. I assume that by exact you mean if formula is A3= B3 + C3 then when it gets copies to the cell below it should be same and not become A4 = B4 + C4 etc.

    If some of the cells remain blank then perhaps they are not empty but may have a space character which appears to be empty but is not. If you want cells with space characters also to copy formula from the cell above then you need to modify the code accordingly.

    A V Veerkar

  6. #6
    Registered User
    Join Date
    05-02-2006
    Posts
    95
    Quote Originally Posted by avveerkar
    When you refer to any cell within a for each loop, the referenced cell does not become active cell. A cell becomes active only if you select it or you have a code cell.Activate. Thus through the whole loop the active cell remains A2 which you have selected at the begining. Therefore if you select A1 then offset (-1,0) refers to a nonexistant cell hence the error.

    Cell(ActiveCell.Offset(-1,0)) is a wrong syntax. Though it does not give error it may give unpredictable result. Correct syntax is ActiveCell.Offset(-1,0).Formula. Also since cell is a reserved word let us use ce ( though in your case it does not affect ). Selection of any cell at the begining is really not required. Your sub should be

    Please Login or Register  to view this content.
    This should copy "exact" formula the way you want. I assume that by exact you mean if formula is A3= B3 + C3 then when it gets copies to the cell below it should be same and not become A4 = B4 + C4 etc.

    If some of the cells remain blank then perhaps they are not empty but may have a space character which appears to be empty but is not. If you want cells with space characters also to copy formula from the cell above then you need to modify the code accordingly.

    A V Veerkar
    Thank-you A V Veerkar,
    Your explanation makes complete sense. I understand now. The reason that I had encountered some blank cells after running the macro was because I had a group of cells highlighted. This was before I added Range("A2").Select to the first line.

    Thanks again,
    Ricky

  7. #7
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Ricky

    If you want to match the exact formula then

    Please Login or Register  to view this content.

    rylo

  8. #8
    Registered User
    Join Date
    05-02-2006
    Posts
    95
    Quote Originally Posted by rylo
    Ricky

    If you want to match the exact formula then

    Please Login or Register  to view this content.

    rylo
    Thanks Rylo.

    Much appreciated.

    Ricky

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1