Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-05-2023, 03:34 AM
soroush.kalantari soroush.kalantari is offline How to go to Nth precedents of active cell? Windows 10 How to go to Nth precedents of active cell? Office 2016
Competent Performer
How to go to Nth precedents of active cell?
 
Join Date: Jun 2021
Posts: 115
soroush.kalantari is on a distinguished road
Default How to go to Nth precedents of active cell?

I have written the following code to make excel go to nth direct precedents of a cell which are in the same sheet. (As far as I know, there is no way to write a code that does this job for precedents which are out of the sheet the active cell lies.) it failes to do its expected job. (see the excel attachment, when running the code for cell E1 and entering 2 in the input box appearing after running the code, it is expected to go to cell D4 but it goes to cell c2.

Sub gotopresedence()




Dim Rng As Range, RngPrecedents As Range

Dim i As Long, j As Long
Application.Goto Reference:=Selection
Set Rng = ActiveCell
Set RngPrecedents = Rng.DirectPrecedents
Application.Goto Reference:=RngPrecedents(InputBox("what presedence"))
End Sub
Attached Files
File Type: xlsm direct.precenedts.xlsm (15.7 KB, 4 views)
Reply With Quote
  #2  
Old 07-13-2023, 04:04 AM
p45cal's Avatar
p45cal p45cal is offline How to go to Nth precedents of active cell? Windows 10 How to go to Nth precedents of active cell? Office 2021
Expert
 
Join Date: Apr 2014
Posts: 871
p45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond repute
Default

This should do it (no need for the other lines):
Code:
Sub gotopresedence()
Application.Goto Reference:=ActiveCell.DirectPrecedents.Areas(InputBox("what precedence"))
End Sub
It's the Areas part that was missing.


re:
Quote:
Originally Posted by soroush.kalantari View Post
As far as I know, there is no way to write a code that does this job for precedents which are out of the sheet the active cell lies
Have an explore of the likes of:
Code:
With ActiveCell
  .ShowPrecedents
  .NavigateArrow TowardPrecedent:=True, ArrowNumber:=1, LinkNumber:=1
  .Parent.ClearArrows
End With
Reply With Quote
  #3  
Old 07-14-2023, 08:20 PM
soroush.kalantari soroush.kalantari is offline How to go to Nth precedents of active cell? Windows 10 How to go to Nth precedents of active cell? Office 2016
Competent Performer
How to go to Nth precedents of active cell?
 
Join Date: Jun 2021
Posts: 115
soroush.kalantari is on a distinguished road
Default

@ p45cal. Thank you very much. Your reply solved the problem.
Reply With Quote
  #4  
Old 07-14-2023, 09:19 PM
soroush.kalantari soroush.kalantari is offline How to go to Nth precedents of active cell? Windows 10 How to go to Nth precedents of active cell? Office 2016
Competent Performer
How to go to Nth precedents of active cell?
 
Join Date: Jun 2021
Posts: 115
soroush.kalantari is on a distinguished road
Default

@ p45cal. I have a follow up question. Is there a way to make this code do the job, when the precedence is a relative defined name? (See the excel attachment. I have defined a relative name (“inflation”) and when in cell “E1”and enter 1 in the input box, I expect to go “E2”.)
Attached Files
File Type: xlsm direct.precenedts for relativenames.xlsm (18.6 KB, 1 views)
Reply With Quote
  #5  
Old 07-15-2023, 09:37 AM
p45cal's Avatar
p45cal p45cal is offline How to go to Nth precedents of active cell? Windows 10 How to go to Nth precedents of active cell? Office 2021
Expert
 
Join Date: Apr 2014
Posts: 871
p45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond repute
Default

When you use DirectPrecedents one of the areas is the area you use in the Name inflation, that is the (if you're in column O):
INDEX(Sheet1!O$1:Sheet1!O$2…
rather than the single cell result of that name, O2 on its own.


You can get to it with
.NavigateArrow TowardPrecedent:=True, ArrowNumber:=5, LinkNumber:=1
but you need to work out the arrownumber and linknumber. Good luck with that!
It all gets very complicated; see:
Excel VBA code to trace precedents of cell - Stack Overflow
vba - Programmatically select other sheet precedents or dependents in Excel - Stack Overflow
When called from an Excel VBA UDF, Range.Precedents returns the range and not its precedents. Is there a workaround? - Stack Overflow



Why do you want to do this?
Reply With Quote
  #6  
Old 07-15-2023, 08:27 PM
soroush.kalantari soroush.kalantari is offline How to go to Nth precedents of active cell? Windows 10 How to go to Nth precedents of active cell? Office 2016
Competent Performer
How to go to Nth precedents of active cell?
 
Join Date: Jun 2021
Posts: 115
soroush.kalantari is on a distinguished road
Default

Quote:
Originally Posted by p45cal View Post
When you use DirectPrecedents one of the areas is the area you use in the Name inflation, that is the (if you're in column O):
INDEX(Sheet1!O$1:Sheet1!O$2…
rather than the single cell result of that name, O2 on its own.


You can get to it with
.NavigateArrow TowardPrecedent:=True, ArrowNumber:=5, LinkNumber:=1
but you need to work out the arrownumber and linknumber. Good luck with that!
It all gets very complicated; see:
Excel VBA code to trace precedents of cell - Stack Overflow
vba - Programmatically select other sheet precedents or dependents in Excel - Stack Overflow
When called from an Excel VBA UDF, Range.Precedents returns the range and not its precedents. Is there a workaround? - Stack Overflow



Why do you want to do this?
Thank you for your reply. The area of named range can suffice me, although I can get the single cell result by adding some new line of codes (see the modified code. I define relative named range in the way the last cell of the area is the single cell result). But a problem remains: when I am in cell E1 (column E) and I enter 1 in the inputbox, excel goes to I1: I2 range rather than e1: e2 range(which is expected according to your reply) Can you guide me on this issue?

Sub gotopresedence()

Dim rng As Range
Dim n As Integer

Application.Goto Reference:=Selection

Application.Goto Reference:=ActiveCell.DirectPrecedents.Areas(Input Box("which precedenceż"))
Set rng = Selection
n = Selection.Rows.Count
rng(n).Select
End Sub
Reply With Quote
  #7  
Old 07-16-2023, 01:32 PM
p45cal's Avatar
p45cal p45cal is offline How to go to Nth precedents of active cell? Windows 10 How to go to Nth precedents of active cell? Office 2021
Expert
 
Join Date: Apr 2014
Posts: 871
p45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond repute
Default

Quote:
Originally Posted by soroush.kalantari View Post
Can you guide me on this issue?
Not really, the links I provided (a) could help but (b) show how complex things can get!
Reply With Quote
Reply

Tags
prcedent



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to go to Nth precedents of active cell? access active cell in word vba littlepeaks Word VBA 5 08-24-2022 09:14 PM
Move active cell down Marcia Excel Programming 2 06-22-2022 04:10 PM
How to go to Nth precedents of active cell? Change active cell after running macro in active cell via selection change Peterson Excel Programming 3 03-07-2021 12:19 PM
Cell equal value new value of active cell which is a drop down hassanmoukadem Excel Programming 1 04-13-2020 07:54 PM
How to go to Nth precedents of active cell? Clear all cell colors within a range starting at cell A8 and change row of active cell to yellow FUGMAN Excel Programming 7 02-05-2017 08:37 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:48 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft