Formatting decimal number in excel 2007 - excel-2007

I have tables with decimal numbers and i wnat that there will be maximum 2 digits after the point. e.g the number 4.567 will be 4.67, but the number 2 will stay 2, and the number 5.6 will stay 5.6. How can i do that? I managed to do that there will be only two digits, but the number 2 converted to 2.00 and 5.6 to 5.60...

Format Cells->Number->Custom...and enter "0.##"
The "0" means "always displayed" (so in this case a digit before the decimal point). The "#" means "display if present" (so up to two digits after the decimal point). It's not quite perfect as "2" will display as "2.", but I think it's close as you're going to get

To add to #Ed 's answer, if you really want to display whole numbers without the . add a conditional format to the cell using formula (eg for cell A3)
=A3-TRUNC(A3,0)=0
and apply the general format

Related article: Excel 2007 ROUND Function
The syntax for the ROUND function is:
= ROUND ( Number, Num_digits )
1.Enter the following data into cell C1: 34.567
2.Click on cell D1 in the spreadsheet - this is where the function will be located.
3.Type " = round ( " in cell C1.
4.Click on cell D1 in the spreadsheet to enter the data in that cell into the formula.
5.Type a comma ( , ) followed by the number (one (1) or HOW MUCH YOU WANT) in cell D1 to indicate that the data should be reduced to one decimal place.
6.Type the closing bracket " ) " .
7.Press the ENTER key on the keyboard.
8.The answer 34.6 appears in cell D1.
9.If you click on cell D1, the complete function = ROUND ( 34.567 , 1 ) appears in the formula bar above the worksheet.

Related

Date Sequence using VBA

SITUATION
I have a sequence of date serial numbers, for example:
42519, 42526, 42533, 42540, 42547, 42554
in cells
B2, C2, D2, E2, F2, G2
respectively.
Eventually there will be 52 date serial numbers, each one representing a Weekly Invoice Date.
You will notice that each one has been incremented by 7 in a previous macro, which presented no problems.
OBJECTIVE
I need to convert these date serial numbers into a format "dd-mmm-yy", using VBA mentioned in Method 2 below (as opposed to copying formulas manually).
So let's say the first date number is 42519 in cell B2.
Method 1.
This method which converts Date Serial Number 42519 to "dd-mmm-yy" format presents no problem to me but is long winded and involves formula copying manually:
Using expression:
Range("B3")="=TEXT(B2,""dd-mmm-yy"")" ' returns 29-May-16 in cell B3
I can even use the expression:
Range("C3")="=TEXT(B2+7,""dd-mmm-yy"")" 'returns 05-Jun-16 in cell C3
Method 2.
This has me stumped and I'm coming up against a brick wall. It was my idea to do something like the following and if I can get this to work I can go ahead and use a loop to generate 52 dates in a row of 52 cells (C3, D3, etc in the format "dd-mmm-yy", each one incremented by 7 days over the period of a year):
Sub sbNumToText_01()
Dim intAdd7 As Integer
intAdd7 = 0
Dim lngSerialDate As Long
lngSerialDate = Range("C2").Value
MsgBox lngSerialDate + intAdd7 'returns 42526, as expected
Range("C3") = "=TEXT(lngSerialDate + intAdd7,""dd-mmm-yy"")"
End Sub
Instead of cell C3 displaying "05-Jun-16", cell C3 displays #Name? and the mini drop down error menu on the left of cell C3 says "The formula contains unrecognised text".
Can any of you out there please explain how to use the TEXT function incorporating variables? Or any other solution along these lines.
Your problem is with the line
Range("C3") = "=TEXT(lngSerialDate + intAdd7,""dd-mmm-yy"")"
If you look at the formula in cell C3 after running the macro, you'll see it contains
=TEXT(lngSerialDate + intAdd7,"dd-mmm-yy")
But lngSerialDate and intAdd7 are VBA variables, not Excel names, so they are meaningless in a worksheet formula and that's why you see the error. What you want to do is convert the sum of those variables to a number before placing it in the worksheet function:
Range("C3") = "=TEXT(" & lngSerialDate + intAdd7 & ",""dd-mmm-yy"")"

Return values from other workbook

Have a question about formula which will resolve my issue.
In my main workbook I need to compare data from two sources.
One of the columns must retrieve data(amounts) from other workbook.
I want formula which will search for all amounts in column G and will skip all blank cells. Tried to use VLOOKUP, INDEX and SMALL functions but no effect.
Each day amounts are different and I need to match them in main file and find exeptions.
Any ideas?
How about an array formula such as the following?
=INDEX($G$2:$G$20,SMALL(IF(($G$2:$G$20)=0,"",ROW($G$2:$G$20)),ROW()-1)-ROW($G$2:$G$20)+1)
The formula would have to be placed into cell I2 as an array formula (which must be entered pressing Strg + Shift + Enter). Then you can drag down the formula to get all the other values.
It doesn't have to be in column I but it has to be in row 2 because this formula get's the n-th Number from the list which is not = 0. The n-th place is (in this formula) row()-1. So for row 2 it will be 2-1=1 and thus the 1st number. By dragging down the formula you get the 2nd, 3rd, etc. number. If you start with the formula in cell I5 instead then it would have to be adjusted to be as follows:
=INDEX($G$2:$G$20,SMALL(IF(($G$2:$G$20)=0,"",ROW($G$2:$G$20)),ROW()-4)-ROW($G$2:$G$20)+1)
You could loop through the column and store each value >0 in an array and then compare or you loop through the column and compare directly...
something like:
Dim i as Integer = 0
Foreach value in Maintable
Do
If otherworkbook.cells(i,7) = value Then '7 for G
do your stuff
End If
i = i + 1
While i < otherworkbook.rows.count
Next
I think that could be the right approach

Conditional formatting formula to highlight appropriate date in range

I need a formula for conditional formatting that will highlight a date between A2:Z2 which matches a number that I enter into a “Committed Sessions Cell” (A1). In row 2 there are a series of numbers that appear above each date column (1,2.3, etc). For example, if I enter a “3” in cell A1, the date in J3 should match the number 3 above it and be highlighted. The idea here is to provide a quick visual prompt for how many sessions are in a client’s contract.
Note: the sequenced numbers 1,2,3 etc in row 2 appear every 5th column (with nothing in between) but there IS other data in between the dates in row 3. Only the appropriate date should be highlighted.
A B C D E F G H I J K L M
1 3
2 1 2 3
3. 1/2/14 2/3/14 2/15/14
With grateful thanks,
~ Jay
Your example is not consistent. If you have the date every fifth column, the dates should be in columns A,F,K,P, etc. with 4 columns between 2 points.
I came up with the following formula: =AND(A2=$A$1,MOD(COLUMN(A3)-1,5)=0) which is applicable to the entire 3rd row. Create it as follow:
IMPORTANT: Select cell A3 (the reference point for the formula)
Without selecting another cell, highlight the entire row 3
Go to Conditional Formatting -> New rule
Choose "Use a formula to determine which cells to format"
In "Format values where this formula is true", put =AND(A2=$A$1,MOD(COLUMN(A3)-1,5)=0)
Choose the formatting that you want (example: Fill with yellow)
Click OK all the way
NOTES:
To change the location of your Committed Sessions, change $A$1 to another cell. Important to keep the dollar signs
The MOD function is the one that controls every fifth column. If you want the highlight every 4th column (i.e. 3 cells between each point such as A, E, I, etc.), replace the number 5 with the number 4

Auto Fill Row B with the last four characters of Row A

So basicly i want a VBA script to fill Row B with the last four characters that are in Row A
RowA contains a telephone number with around 12 numbers in it.
Assuming that you meant to say
I have a series of telephone numbers in column A. I would like to
create a second column in which I have just the last four digits of
these numbers. I am new to Excel. Could someone please help me get
started on this?"
The answer would go like this:
In Excel you can create formulas that compute "something" - often based on the contents of other cells. For your specific situation, there is a function called RIGHT(object, length) which takes two arguments:
object = a string (or a reference to a string)
length = the number of characters (starting from the right) that you want.
You can see this for yourself by typing the following in a cell:
=RIGHT("hello world", 5)
When you hit <enter>, you will see that the cell shows the value world.
You can extend this concept by using a cell reference rather than a fixed string. Imagine you have "hello world" in cell A1. Now you can put the following in cell B1:
=RIGHT(A1, 5)
and you will see the value "world" in B1.
Now here is the cool trick. Assume you have a bunch of numbers in column A (say starting at row 2, since row 1 contains some header information - the title of the column). Then you can write the following in cell B2:
=RIGHT(A2, 4)
to get the last four digits. Now select that cell, and double-click on the little box in the bottom right hand corner:
Like magic, Excel figures out "you want to do this with all the cells in this column, for as many rows as there is data in Column A. I can do that!" - and your formula will propagate to all cells in column B, with the row number adjusted (so in row 3, the formula will be
=RIGHT(A3, 4)
etc.
Try
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
With ws.Range("B2:B99")
.Formula = "=Right(A2, 4)"
.Value = .Value
End With

VBA conditional cell selection

I was having a play around with excel 2007 VBA and was hoping to create a Macro that generates a random number, then outputs a string based on the number generated. For example,
Number String
1 Athena
2 Belerephone
3 Castor
4 Dionysos
If the random number is 4, the output would be Dionysos, 1 would be Athena and so on.
Basically, I want the Macro to search through the "Number" column, find the cell that matches the randomly generated number, then output the string in the cell to its right.
I have a table of a similar nature in my Excel worksheet.
So far I have not had much success at doing this, any thoughts?
With your data in A1 thru B4, use:
=VLOOKUP(RANDBETWEEN(1,4),A1:B4,2,FALSE) or its VBA equivalent
EDIT#1 :
With the upper bound of the table stored in cell D3 :
=VLOOKUP(RANDBETWEEN(1,D3),INDIRECT("A1:B" & D3),2,FALSE)
You can use application.worksheetfunction.vlookup to perform a vlookup within your macro, and use the 1+Int(10 * Rnd()) function to return random numbers from 1 to 10 (and possibly 11, but statistically unlikely).