Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with absolute reference in excel vba 1

Status
Not open for further replies.

kokon

Programmer
Apr 27, 2006
31
0
0
PL
Hi I have the below code inside 3 "For" loops. I need to insert in few cells the same formula with absolute references to $J$119 cell.
That code works but it inserts $J$119*100, $J$120*100, $J$121*100 like relative reference. Please help


Cells(i, j).Offset(k + Insert_row, 0).Offset(0, 8).Formula = "=" & Cells(i, j).Offset(k + Insert_row, 0).Offset(0, 5).Address(0, 0) & "/ $J$119*100"
 



Really?

I ran your code and the $J$119 remaind ABSOLUTE!

???
Code:
For i = 1 To 2: For j = 1 To 2: For k = 1 To 2
Cells(i, j).Offset(k + Insert_row, 0).Offset(0, 8).Formula = _
    "=" & Cells(i, j).Offset(k + Insert_row, 0).Offset(0, 5).Address(0, 0) & "/ $J$119*100"
        
Next: Next: Next

Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 
Why couldn't your code just be ..

Code:
Cells(i, j).Offset(k + Insert_row, 8).Formula = "=" & Cells(i, j).Offset(k + Insert_row, 5).Address(0, 0) & "/ $J$119*100"

?? Other than that, without your entire code, the full results you are receiving and the full results desired, it's really hard to tell.

Regards,
Zack Barresse

Simplicity is the ultimate sophistication. What is a MS MVP? PODA
- Leonardo da Vinci
 
Sorry I forgot that I have something like that

For i = 1 To 2: For j = 1 To 2: For k = 1 To 2
Cells(i, j).Offset(k + Insert_row, 0).EntireRow.Insert
Cells(i, j).Offset(k + Insert_row, 0).Offset(0, 8).Formula = _
"=" & Cells(i, j).Offset(k + Insert_row, 0).Offset(0, 5).Address(0, 0) & "/ $J$119*100"

Next: Next: Next
 

Of course, it all depends on the value of Insert_row. If it is greater than 119 all is well. If it is less than 119, then when you insert a row previous formulas will be adjusted to the new address of what had been in J119. That's just the way it works.

Perhaps it's time to tell us what it is that you are trying to accomplish.


 


Well, there you go! That answers the dilema!

Each time your INSERT, you displace the ABS REF cell by one row.

FIRST, insert the rows you need.

THEN run the code sans insert.

Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 



...but keep in mind, each INSERT displaces your row of interest.

1) I would recommend against the inserts or

2) NAME the cell that J119 is ORIGINALLY in and reference the range name.

Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 
Thx a lot SkipVought. Can you please give me little tip how to name the cell, and do reference to the range name...
 

(Using a range name won't make any difference as long as the inserts are taking place above row 119.)

 
yes it take place above row 119. All right so I will insert all necessery rows and than insert formula
 



Z,

The SYMPTOMS ("... but it inserts $J$119*100, $J$120*100, $J$121*100 ...") indicates that row 119 is BELOW the reference point.

Use the Name Box to enter a Range Name (no spaces)

Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 



BTW,

Named Ranges are a powerful feature in Excel. You ought to get familiar with using them.

Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 

I meant that using a range name would not prevent the magic cell from dropping from row 119 to a lower row everytime a new row is inserted. True that all of the formulas will be "better" in that they will all reference the same absolute cell, but it won't be J119.

Without knowing exactly what kokon is really trying to do it sounded like after the macro finishes someone is going to update cell J119. That will produce unexpected results, and will happen whether or not a range name is used. (Although the results would be different.)

 



I agree that doing the INSERTS is NOT a good approch as it change the location of this TARGET.



kokon,

We relly need to know more about your application to see if there's some other way to accomplish the end result. This INSERT approch is NOT a good one.

Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 
I have always been frustrated when excel moves the cursor. I have gone into Tools/Options/Edit, and then unchecked the 'move selection after enter' box. However that will stop it from moving for everything you do. (I suppose with programs also?)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top