Address of first layer of precedent cells via VBA in Excel - excel

On the debug toolbar, you can click a button that shows you directed arrows of cells that are directly called by the selected cell (I'm calling these direct precedents the 'first layer'). However, when you use the Precedents property on a cell, it returns all of the cells on the sheet that the cell is dependent upon.
For example, I have cell DT836 with the formula:
=DP836+DR836+DS836
"Trace Precedents" Debug toolbar shows a nice blue arrow connecting those three cells to cell DT836.
Range("DT836").Precedents contains a 70,000+ cell range with (as far as I can tell) no way to distinguish between how distant a dependent is from its precedent.
Is there a way to discern this using VBA?

It turns out there is a different property for ranges that tells you this, DirectPrecidents.
I had thought that it would be a property of the precedents, not a separate property in and of itself.

Alec,
Thank you for closing out your own question. I am posting these comments as an answer rather than a comment given their length
Excel is rather flakey when it comes to 3D functionality (across sheets). Both Precedents and DirectPrecedents work only on the local sheet
To work with off-sheet Precedents and Dependents you will need to use the NavigateArrows Method, this is the VBA equivalent of tracing precedents and dependents via the audit toolbar.
Bill Manville did the heavy lifting on this approach some time back, below are two links applying Bill's approach. The first provides Bill's function, the second link is from me, this article looks "backwards" to test if a certain number eventually links back to a specific sheet
Bill Manville's FindPrecedents function (written up at vbaexpress)
My article 'Using a recursive NavigateArrows Method to analyse all local and off sheet cell dependencies' at Ee

Related

Specifying range in VBA macros

I am really new to VBA. SO this might seem a silly question for experts.
after researching the internet on how to put dynamic range for filters in excel considering a situation when your list might grow or shrink I stumbled on couple of ways. One of them is as the following.
ActiveSheet.Range("A1").AutoFilter Field:=46, Criteria1:="1"
I know the you can put two arguments to specify ranges and the second one is optional. In this particular I am just putting the first cell where my data starts. My questions are below:
does the code automatically select the active range based on the first specified cell?
Had my data started from "C5" would i still have to chose the argument as "A1"?
I am filtering out something from column 46. what does it matter where I specify my first cell? for example if I had chosen "C5" instead of "A1" how it would have affected my macro?
Let's say, I want to filter out something from column 2 ie, Field 2. But the argument for range I choose as "D5". How it would affect my macro code?
AutoFilter: A Small Study
Well, I was curious, too. So I studied it a little bit and created a
workbook where you can input the parameters of some of the arguments
of the AutoFilter method and just by clicking on a command button
apply the changes. The next click removes the filter.
The workbook also contains the results of the behavior when changing
by row. The same thing could be done by column.
Workbook
Download
(Dropbox)
The following image displays the initial state of the only worksheet in the workbook.
This I was most curious about: what would happen if I would filter in C8. There's a surprise.
There still remains a ton of unanswered questions, but this could get you started to investigate deeper.

Locating Lost Excel Macro Formula From Old Data Sheet

I'm resurrecting some old scientific data from the early 2000s.
I need to locate the custom functions that allowed the data to be shown. The spreadsheet that I have is full of #REF! cells, as they are supposed to be calculated based on a custom-defined formula (here, called 'RESECTION').
How do I find this formula? If I can see the math it was performing, I will be able to use this old data, and extend our timeseries significantly.
The spreadsheet is an ".xlsm" document. There is an associated file that is ".XLM"; it provides some GUI-like functionality that is now broken, and I do not see how to access the commands (?) or other VBA that is inside.
I have not had success with this solution.
File with the VBA can be found here; SURVEY.XLM.
Problem is seen here; calling function from SURVEY.XLM. How do I access the formula within here?
I can see that the formula is in there; how do I see the calculation it performs?
RESECTION is a named range refering to cell A4 on the hidden Survey sheet.
In the VBE immediate window type thisworkbook.Sheets(2).visible = true and then thisworkbook.Sheets(2).select.
Cell Survey!A4 contains the value =RESULT(64).
The rest of the sheet contains the macros - first time I've seen or tried to use a filled in macro sheet.
I tried Ctrl+Fto find the definition of RESULT but it comes up with Macro error at cell [SURVEY.XLM]SURVEY!A364.

How to change the tab color based on a cell in the worksheet?

I have a very large spreadsheet with lots of worksheets that I use to create invoices based on a project number. Each project doesn't get billed on a monthly basis. I have to print each invoice to PDF and to avoid having to open every tab to see if there is information to bill, I'd like to change the tab color based a cell which contains the total of the invoice.
I have used VBA very little but have found several macros that are trying to do the same thing but because they rely on something different, I can't get what I need. I found one that just turns all the tabs a color. I found one that changes the color based on a true/false formula. I don't have the knowledge around this code to change it so that if cell H22 is >0, it turns the tab a color.
The invoices created before I took my role aren't consistent so not all totals will be in cell H22 -- it could be H14. I'm even willing to put a button on each tab that I could press to apply the macro as I do my preliminary work on each tab. I just don't have the knowledge good enough to know how to write the code. Thanks!
The easiest way would be to find an cell that none of the worksheets use (say AZ99) and do the equivalent of the H22>0 example you described on each sheet.
e.g sheet1 might have AZ99 as X10>0, sheet2 might be Y11>0 or whatever.
Use the true/false VBA code to look at cell AZ99 or whatever the cell you choose is.

Groovy POI created cells considered blank by other programs until ENTER is manually pressed in cells

Ok this is kind of hard to explain.
I create a cell in my worksheet using JAVA POI
newCell= row.getCell(index)
if (containerCell == null) {
containerCell = row.createCell(index)
}
newCell.setCellType(HSSFCell.CELL_TYPE_STRING)
newCell.setCellValue(strVar)
If i then open the worksheet in EXCEL, I see that the cell value is indeed set. However if I load this into another external program that reads EXCEL sheets, it claims the cell I just set is blank.
NOW, if I go back into excel and do a simple "Hit return" on the cell in question, in the formula bar (even though its not a formula) and try to reload it into the external program, it works fine. Do I need to evaluate a formula on a string?
Thanks
I have no experience with Java, but having to hit enter in a cell is usually indicative of calculation being set to manual. Since you are working with API it may be treating your input from Java as a formula (?).
Open the offending workbook in its broken state, and on the sheet you are having problems with choose [Formulas] tab at top, then [Calculation]>Calculate Sheet.
If this updates the value then I see two options...
See if there is an option to manually calculate the sheet with POI, after the value has been entered.
Alternatively, you can write just a tiny bit of VBA to force calculation on that sheet when workbook is opened. MSDN offers a simple example.
http://msdn.microsoft.com/en-us/library/office/aa223802%28v=office.11%29.aspx
I have had to do this for Excel apps where calculation had to be set to manual to avoid excessive overhead. Just a guess though..
I am not sure why this was happening.. and evaluating the individual cells did not solve it however running evaluteAll() on the entire workbook made it work!

VBA: Finding a value, copying row, pasting row somewhere else, Finding the next value

So here is what I want to do in VBA:
Find the value in E10, copy the row it is in, paste the row somewhere else (doesn't matter as long as I can find it) then finding the next row with the value E10 and doing the same thing, etc, until I reach the end of the spreadsheet
I'm really new to VBA and macros in general, so any help would be appreciated!
Generally speaking you'll find that asking questions without explaining "what have you tried" tends to be frowned upon in these parts.
I suggest that you take a look at this somewhat similar question that I answered just earlier today:
Copy & Paste row of data in Excel 2003 to different sheet by Email search
The principal differences between that one and your need are:
Instead of an InputBox, you'll use the value in E10 to run the .Find method; and
You need to create a loop. You still run the .Find method as shown in my example but you do it in a loop until you've looped through every instance of the value in E10 in the sheet. (Get the address of the first range that you find, and keep running the .Find method until you get back to that address.) Look up the While statement and the For statement in VBA help for examples on how to create a loop.
To get the value of an individual cell one way is just to create a reference to the cell range, then query the .Value property. For example a fully qualified reference could be:
Thisworkbook.Worksheets("Sheet1").Range("E10").value
There are other syntaxes which will yield the same result, but you can learn about those in time. Recording your actions using the Macro Recorder is a good way to learn about properties and methods. The problem is that in yet another part of the Excel 2010 interface that I detest, Microsoft has decided to hide the thing. To get to the Macro editor, go to File -> Options -> Customise Ribbon and turn on the Developer tab.
Give all of that a try and if you get stuck with something specific, post again with the details.

Resources