Benford's Law in Excel - excel

I have a large data set, and I was hoping to test it against Benford's Law.
So far, I have been extracting the first digit, and creating a chart based on that
=LEFT(A1,1)*1
I multiply the formula by 1 because LEFT turns the digit into a string.
I was hoping to automate this procedure. Is there a way to do this with VBA or a simpler/more elegant formula for this?

Here's a thought.
Let's assume your data is in cells A1:A100 and your workbook is named "Benford".
Using the Name Manager on the Insert tab of the ribbon, create a new named range, say, "digits", defined as =VALUE(LEFT(A1:100,1)). The Name Manager will automatically treat the result of this equation as an array.
Insert a chart into the workbook without selecting a data range. The chart will be totally blank.
Select the chart and then in the Chart Tools tab, press the 'Select Data' button.
When the 'Select Data Source' dialog box opens up, again do not specify a data range, but instead click on 'Add Series'.
In the 'Edit Series' box that comes up, set the 'Series value' to =Benford!digits. The workbook reference is important: Excel won't accept the bare defined name here.
If initially the data does not show on the chart, just manually recalculate the sheet with the F9 key.
You can then add titles, axis labels, etc., as needed.
This can be automated via VBA; whether that makes sense depends on your needs.

I'm sure you can use Google as well as anyone. I searched for "excel formula for benford's law". I'm pretty sure the first result is what you are looking for.
Here's the link:
http://www.isaca.org/Journal/Past-Issues/2010/Volume-1/Pages/Using-Spreadsheets-and-Benford-s-Law-to-Test-Accounting-Data1.aspx
It covers testing lead digits, how to use benford's law and some useful do's and don'ts. This should get you most of the way.
FYI - the ISACA is an auditing standards organization. Their content is designed to be useful in professional applications.
Good luck!

Related

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.

Auto populate column by reference?

I am working on the following sheet, called Raw_Data:
In a new sheet, I want to copy the registration_date column by reference. This means that if I change the registration_date on the Raw_Data, the changed value should be reflected in the new sheet.
To implement this, I have entered the following =Raw_Data!C2. So far it works fine as you can see below:
But the problem is that when I double click on the little green square here, it doesn't automatically populate the entire column.
I don't want to manually drag-and-drop because there are several thousand rows. Does anyone know how I could automatically populate the column by reference?
While there are some automation things you could do, I think a lot of that would be overkill for what could be just an input issue with how you are choosing to copy in Excel.
If you find that you need to copy a large block of data, rather than dragging the corner of your cell like that, try one of these alternate methods:
While selected on the cell, press CNTRL + C. Then in the 'Name Box' (where it shows the address of the cell you are on), type in the cell where you want to go (A17000); then press SHIFT + ENTER. This will jump you to that cell, and will highlight all cells inbetween where you were and where you are going. Then press CNTRL + V.
Another method of moving around a large data block in Excel is to hold CNTRL and press an arrow key. This will move you as far down the data block as possible. Note that this will not work on a blank sheet, as there is no data and therefore Excel doesn't know when to stop.
Again - some automation would be possible here, but moving around an Excel worksheet is something you will be doing too frequently in too many different ways to want to automate what might be 5 keystrokes once a week.
In excel the "double click to fill" feature is a heuristic based feature that fills the cells that appear to be relevant with the selected formula (+ reference corrections).
In your case there is no hint the heuristic can use to tell what to fill so nothing is being done.
Regarding dragging "several thousand rows", that's not a real problem. If you only do it once, there no reason to even trying anything "smart" or complicated.
You can also copy the source cell, select all the cells you want to fill and then paste. You can select cells in any way you like, not just dragging.
You could instead of all the dragging/copying/filling approach simply copy directly from the source, and paste by reference.
As a last resort, you can always go for a VBA solution to do that for you.

Reference Excel cells in Word

Does anyone know of a pre-packaged solution for referencing Excel cells directly in a Word document?
For example,
{[TestExcel]Tab1!A1}
Dear Mr. {[TestExcel]Tab1!A2},
Our systems have alerted us that your account {TestExcel]Tab1!AccountNumber} is overdue. Please remit payment of {TestExcel]Tab1!PaymentDue} immediately.
This example uses a hypothetical { } syntax, where the contents are standard Excel formula.
Every solution I've seen uses custom VBA to accomplish this. It seems like an extremely common need for which there should be some good 3rd party software available that lets non-technical users accomplish this task.
As indicated in the example above both named ranges and standard cell references would need to be supported.
Yes.
Step 1: Copy the excel cell.
Step 2: Go to your word document where you want to paste the reference.
Step 3: Right click
Step 4: Under paste options, select "link with source formatting" or "link and merge formatting".
I tried #Green Demon's method, but the two Paste Options listed were not present for me (Office 2013). I only had Keep Source Formatting, Merge Formatting, and Keep Text Only. However, I poked around and found the Paste Special, which was hidden on the Ribbon
The instructions are as follows:
Copy the Excel range.
Go to your Word document, and left click where you want to paste the Excel range.
Click on Paste Special from the Ribbon as shown below, or type Ctrl+Alt+V.
Click on the Paste link radio button.
Click on Microsoft Excel Worksheet Object. The Paste Special dialog box should look something like this:
Click OK.
The range should now be pasted in your document, and if you save, close, and re-open the Word document, you will be prompted about whether or not you want to "update this document with the data from the linked files," which works the same way as it does if you're doing Excel-to-Excel linking.
This feature is fantastic, and I'm so happy to have found it today. However, in my brief experience using it, having these Word-to-Excel links dramatically increases the amount of time it takes to open your Word document, even before it prompts you about updating. After that, the act of updating the Excel links takes much longer than it would if you were doing the same linking Excel-to-Excel. All of my work is done on network-share files, so YMMV.
While Will's solution indeed links from the Cell objet, the content is inserted as a picture.
In the same dialog, selecting "Unformatted Unicode Text" instead will link and insert only the cell's content, which is desirable if you apply custom formatting on your text.
Unfortunately, this action only works for a single cell. Copying multiple cells will result in a text block with individual cell content seperated by tabs or line skip, with the entire block considered linked and entire. The solution is to link individual cells using this method.
This can become tedious if you plan on linking large portions of table from Excel, as for every cell the dialog must be invoked. It can be sped up by using hotkeys, but the sequence gets tedious:
alt,h,v,s, then alt+l, tab, ↓, and finally Enter
Obviously this is not practial for large numbers of cells. A AutoHotkey script can help but it third party grafted solution, on par with using VBA scripts.
alt+h+v+l works too. If you're building big documents clicking the ribbon every time gets murderous after a while.
As this is still a problem but the other answers do not take into account the latest development in word, there is a new solution, editing Green Demons answer:
Step 1: Copy the excel cell.
Step 2: Go to your word document where you want to paste the reference.
Step 3: go to the "home" tab
Step 4: Under paste options, go to "Paste Special", click on "paste link", click on "Unformatted Text".
This solves the issue others were having with the new lines that you would otherwise have with just Green Demons' solution.
Hope this still helps others.

Add/Modify/delete calculated column formula in Excel Listobject/Table via VBA

If I manually enter a formula into a column in an Excel table (i.e. ListObject), AutoCorrect applies this formula to the whole column.
Is there any way to control the this behavior via VBA, i.e. can I somehow modify/delete/add this formula?
I know I can simply change the formula of the ListObject.ListColumns(1).DataBodyRange object - but this will overwrite any manually values entered before - while changing the formula in the UI will leave this untouched...
Thanks to Doug's and bonCodigos comments/answers, I found the simple answer:
ListObject.ListColumns("Column name").DataBodyRange.FormulaR1C1 = "new formula"
This will overwrite any manual value (just as the normal behavior with AutoCorrect).
Best would be if you could show us a screen shot of your sheet. Based on that we would have articulated the answer.
Here is with the general assumption. That you have one list object to dump some data to a column and rest of the cells in that column are manually interacted.
You could try the following manually first and see if it works for you. Still the manual one is being overtaken by the code level, then you do this in code level. :)
The main action here is to Stop automatic corrections
Go To -> Tools menu -> Click AutoCorrect Options ->
In the AutoCorrect Tab ->
1- To prevent ALL automatic corrections
Clear the check box for Replace Text as you type
2- To prevent SPECIFIC corrections
Clear the corresponding check box for the option. I believe you are more interested in the latter - specific data range that you just dump via a listobject.
Now here is the code for disabling this feature at code level.
When working with Excel Tables (ListObjects) there are two AutoCorrect options to consider: You can read about those two in details.
* Apply new rows and columns in table
(VBA AutoCorrect.AutoExpandListRange Property)
* Fill formulas in tables to create calculated columns
(VBA AutoCorrect.AutoFillFormulasInLists Property)
Code you may want to use at the top of our listobject is,
Application.AutoCorrect.AutoFillFormulasInLists = False
And totally agree that it would be so much more useful if Application.AutoCorrect. AutoFillFormulasInLists controlled on a table by table basis at the ListObject level. So here is a workaround.
So one way is to clear your table data each time. And when you clear data you can make sure for TABLE TO FORGET forumulae and formatting. So it will clear the contents of the data body range before deleting table contents.
Sub forgetMe()
With Sheet1.ListObjects("myTable")
If Not .DataBodyRange Is Nothing Then
.DataBodyRange.ClearContents
.DataBodyRange.Delete
End If
End With
End Sub
When you input the data, start with auto stuff off.

How to crack an Excel workbook that looks up values?

Alright I know this isn't 100% related to programming (the Excel book in question doesn't use VBA at all) but I'm hoping someone can help me out with this or point me in the right direction.
My boss got a spreadsheet from a vendor that has a combobox/dropdown list with various part numbers; when you select one it populates the rest of the form with a lookup containing additional items. I've been tasked with "cracking" this and finding the list that they're using to populate so we can make use of it.
The thing is... there's no VBA code, no macros, no data connections, and only one Worksheet displayed in Excel while the lookup code references a Sheet1. I've tried to display hidden worksheets and it says there are none... so where on earth could this list be kept? My boss is getting impatient and is asking me if I've broken it yet. It's not a big deal if it can't be done, I just have no clue where to continue looking for it and I don't know what to tell my boss when he asks me if I'm done.
Can anyone help?
It's possible to hide a worksheet using VBA so that it can't be unhidden from the UI.
Try the method for un-hiding all hidden workhseets outlined here:
http://www.automateexcel.com/2004/12/14/excel_vba_unhide_all_worksheets/
My guess is that it is a Data Validation list which references a constant list of values or a range on a "Very Hidden" sheet. In Excel 2007, select one of the cells with the drop-down, click on the "Data" tab in the Ribbon, click on "Data Validation" in the Ribbon and see what you have. In Excel 2003 it is the Data -> Validation... command.
Another possibility if you know the name of the worksheet is "Sheet1" is to add a new worksheet, enter "=Sheet1!A1" into cell A1 of the new worksheet, and copy this cell down and to the right for as large of an area as you need to see the data you are interested in.
If you can post a URL to download the workbook (assuming it is not a trade secret) you would be more likely to get an accurate answer.
Could it be some data stored on the same sheet.
Possibly in columns which are either hidden, or which are far off the actual page?
Isn't this just data from the worksheet only?
Column header dropdown lists acts as filters, they show distinct values of a column.
This is a feature of Excel.
The items could be cached from a currently unavailable resource. Try saving it out to xml and searching for a known string.
Click on the cell that display a drop down list when selected
From the menubar select data>validation
In the dialog box copy the content of the source text box
Now paste the content on any empty cell on your worksheet
Select a drag it down to see the values populating the list
Chris
------
Convert your Excel spreadsheet into an online calculator.
http://www.spreadsheetconverter.com
I am assuming that you have broken this by now, but just in case you havent. This is certainly a case of data validation using a named range which is house on another sheet that was designated "very Hidden" from the vba console. You will need to open up the VBA project of this worksheet and designated the "very Hidden" sheet to just Hidden and then you will be able to unhide it, or the other setting at which point it will be viewable.

Resources