Excel's cell center shifted - excel

I have been working on a math drill program for a bit now. I had asked a similar question to this previously but I wasn't getting any good help with it. So I decided to research the problem more thoroughly and found something interesting.
Now before I begin, I just want to go over the preliminaries. Yes, I am using Microsoft Excel 2007. No, while this may work for you, it is not for me.
Okay now that, that is taken care of:
The problem I am having is when I have
ActiveCell.NumberFormat = "# ?/?"
in my my code it causes the excel's center line to be shifted to the left (that is the only way i know how to explain it).
Meaning, if you have something right justified, it will look centered, if it is centered it will be almost left justified and there is very little difference between left and center justified.
if i have
ActiveCell.NumberFormat = "?/?"
then there is none of the above problems.
The entire code of this section is as follows:
Sub test()
Sheets("sheet1").Select
Range("a1").Select
For i = 1 To 10
ActiveCell.NumberFormat = "# ?/?"
With ActiveCell
.Locked = False
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
ActiveCell.Value = 33
ActiveCell.Offset(0, 1).Select
Next i
End Sub
Any suggestion or reason for why this is happening would be greatly appreciated. Ideally the user should be able to see a mixed number.
Further information, if I change each cell by hand to fraction then it works fine. it is only when excel does it.

It is aligning the number so that the fractional parts are aligned when viewed in columns with a fixed width font.
3 1/3
2
1 3/4
3 2/7
2 2/3
3 1/2
6

EDIT (2)
I tried it with Excel 2007 too, and it makes no difference if I change the NumberFormat by hand or by script.
Anyway, the following script should do what you want. Add it to your Worksheet's source.
It is triggered whenever a value is changed. Then it checks if the correct column is affected (1 in my case), and then it tries to format the number with fractions, but if no fractions result from that, it uses a no-fraction-format.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
For Each cell In Target.Cells
If cell.Column = 1 Then
If IsNumeric(cell.value) Then
cell.NumberFormat = "# ?/?"
cell.Calculate
If InStr(1, cell.Text, "/") = 0 Then
cell.NumberFormat = "#"
End If
End If
End If
Next cell
End Sub
Original post
I've only tried it with Excel 2003, but here is no difference between setting it by hand or via VBA. Please make sure that your code does exactly the same that you do manually, just record a macro of what you do manually and run that on another cell.
The reason for the shift is that Excel reserves that space for the fractions.
1,2 -> | 1 1/5|
1 -> | 1 |
33 -> | 33 |
33,5 -> | 33 1/2|
Is the following what you would like to have?
1,2 -> | 1 1/5|
1 -> | 1|
33 -> | 33|
33,5 -> | 33 1/2|
Why would you like it in that format?

Actually a conditional format would work much better to center everything. See the image below for my setup.

Related

Textbox_change() event only on keyboard input

Function FtoC(ByVal Temp As Variant) As Variant
FtoC = Round((Temp - 32) * 5 / 9, 1)
End Function
Function CtoF(ByVal Temp As Variant) As Variant
CtoF = Round(Temp * 9 / 5 + 32, 1)
End Function
Private Sub Temp_F_Change()
If Temp_F <> "" Then Temp_C = FtoC(Temp_F)
End Sub
Private Sub Temp_C_Change()
If Temp_C <> "" Then Temp_F = CtoF(Temp_C)
End Sub
I have the above functions/subs for a little project to learn/sharpen my VBA skills. The logic is all fine and dandy. However, ideally I would only like the textbox_change event to only run if the change was due to a keyboard input (or a not sub output). A working example would be starting from blanks for each if I input 78 to Temp_F then it converts to 25.6 outputs it to Temp_C. Temp_C has now changed and it then converts 25.6 to 78.1 and outputs it to Temp_F, then of course temp_f has changed and it converts it to 25.6 which is not a change bringing both subs to their end points. Ideally i would like for it to take 78F convert to 25.6C and for the form to recognize that the Temp_C field change was due to a sub output(or not due to a keyboard input) and exit the temp_c_change() sub Is this even possible with VBA, if so how simple/complex is it? I tooled around with the idea of using a counter that is reset by a keypress event but my scripting skills aren't good enough to make it work. I thank you guys in advance and apologize that my question is beneath the talent that I've witnessed searching this site for answers before.
Well, for something this small you could use a worksheet_change event that first checks the target cell to see if either of the two temperature cells changed (assuming they do not move around the sheet - if they do, consider a named range). If either of those cells changed, then create the appropriate conversion formula as a string and put it in the other cell.
My apologies, I just noticed that you are working with a textbox event and not a worksheet event.
What happens to the data after it is converted? Does it go into a cell or get plugged into another function/sub? Right now, your code will just perform a calculation but doesn't show it or do anything else with it. Maybe you need to approach it from whatever that happens next.

Summing mapped values in excel

My situation is the following: I have a table in which there are codes of working shifts i.e. :
John | 1 | 34 | 1 | 34 | Total: | 40 <----
Maria| 1 | 1 | 1 | 1 | Total: | 32 <----I need a formula for this last cell
Every one of this codes has hours amount set in another table.
Codes | Hours
1 | 8
34 | 12
I need to calculate the total amount of work hours based on the codes. I have found ways to map the values to the other table with INDEX and MATCH but when I try to do it for a lot of rows it just gives me the first code mapped, not an array with the mapped values.
I think what will help is the following custom VBA function(see example below). Use as follows:
=multipleMatch(range of cells to look in, range of cells to INDEX, range of cells to MATCH)
Let me know if you have any questions.
Function multipleMatch(inputRng As Range, indexRng As Range, matchRng As Range) As Double
On Error GoTo errhandle
Dim runTot As Double
runTot = 0
Dim cll As Range
Dim mtch As Long
For Each cll In inputRng
If Not IsError(cll.Value) Then
On Error Resume Next
mtch = Application.WorksheetFunction.Match(cll.Value, matchRng, 0)
On Error GoTo errhandle
If mtch <> 0 Then
runTot = indexRng(mtch) + runTot
End If
End If
mtch = 0
Next cll
multipleMatch = runTot
Exit Function
errhandle:
multipleMatch = CVErr(xlErrValue)
Exit Function
End Function
Is there a reason why you have upper table going to the right?
Is there more than John in it?
If the upper table is the record of actually worked shifts, and the bottom one is the default working hours by shift code, and you plan on having more people, then i could suggest the following solution that will also re-organises your tables a little bit, and utilises Excel tables which will allow you to avoid having to maintain the formulas and ranges:
select your Bottom table range, press Ctrl+t which will turn it into
a blue table.
Rename columns just for this example to work - you can rename after you're done with steps here.
your "upper" table gets transposed, having 3 columns Name, Shift Code, Hours.
Do the same thing with it: select range, press ctrl+t, tick "Table has Headers" in the pop-up window. Rename columns as in picture.
in the 1st cell of Hours Column of table 2 (as in picture), paste this formula and hit Enter. (Now is a good time to change column names to your preference)
=SUMIF(Table1[Shift Code],[#[Shift Code]], Table1[Default Hours])
Create a pivot table of Table #2 next to it or wherever you ened it.
If you add data to tables 1 and 2, formulas will update automatically in table 2, and pivot table source. Refresh pivot table to see updated totals.
this is what it would look like.
I hope this is what you tried to achieve.
and if you want, you can put Shift Code into Columns area of pivot table as in the picture below, and have it broken down by code as well as a grandtotal:
*****P.S. Please remember to mark the most useful answer as "Answer" and vote up if other answers or comments were helpful*****

Need formula for excel, to subtract the number "9" to each number individually and

I want you to have some fun. I need something specific.
First i must explain what i do. I use a simple codification for product prices at retail store, because i dont want people know the real price for themselves. So i change the original numbers to another subtracting the number 9 for each number.
Normally I manually write down all the prices with this codification for every product.
So.. for example number 10 would be 89. (9-1 = 8) and (9-0 = 9)
Other examples:
$128 = 871
$75 = 24
$236 = 763
$9 = 0
Finally i put 2 number nines (9) at the beginning of the codified price also, to confuse people who might think that number could be the price.
So the examples i used before are like this:
99871 (means $128)
9924 (means $75)
99763 (means $236)
990 (means $9)
Remember that i need 2 (two) nines before the real price. The real prices never start with 0 so, the nines at the beginning exist only to confuse people.
Ok. So, now that you understand, here comes the 2nd part.
I have an excel whith hundreds of my products added, with prices, description, etc. And i decided it is time to use a printer and start to print this information from excel. I have a software to do that, but first i need to have the codified prices in the excel also.
The fun part begins when i want to convert the real prices that are already written in my excel document into a new column AUTOMATICALLY. So that way i donĀ“t have to type again all the prices in codified form for the old and new items i add in the future.
Can someone help me with this? Is it even possible?
I tried with =A1-9999 but, it works well with 2 character number only. Because if the real price is 5, i will get 3 nines: 9994(code). And if the price is 234 i will get only 1 nine 9765(code). And it is a condition i need to have the TWO nines at first.
Thank you very much in advanced!
Though you have requested for formula , I am suggesting VBA program which seems to me very convenient.
You have to open VBE and insert a module and copy the program. Change the code lines wherever indicated to suit your requirements for sheets etc.
Sub NumberCode()
Dim c As Range
Dim LR As Integer
Dim numProbs As Long
Dim sht As Worksheet
Dim s As Integer
Dim v As Long
Dim v1 As Long
Set sht = Worksheets("Sheet1") ' change as per yr requirement
numProbs = 0
LR = sht.Cells(Rows.Count, "A").End(xlUp).Row
For Each c In sht.Range("A1:A" & LR).Cells
s = Len(c)
v = c.Value
v1 = 99
For s = 1 To Len(c)
v1 = v1 & (9 - Mid(c, s, 1))
Next
c.Offset(0, 1).Value = v1
v1 = 99
numProbs = numProbs + 1
Next
MsgBox "Number coding finished"
End Sub
Sample sheet of results is appended below.
I will be using helper cells but you could dump it all into one cell if you want since you are only dealing with 4 characters.
For the purpose of this example, I am assuming your original price list starts in B11.
=IFERROR(9-MID($B11,COLUMN(A1),1),"")
Place that in D11 and copy to the right three more times so you have it from D11 to G11. That formula strips off 1 character from your price and subtracts that character from 9. When you go the next column it repeats itself. If you do not have that many characters, it will return "".
In C11 you will build your number based on the adjacent 4 columns using this formula:
="99"&D11&E11&F11&G11
It places 99 in front then adds the numbers from the adjacent 4 columns.
Select cells C11 to G11 and copy and paste downward beside your data column as far as you need to go.
An alternate more concise method would be:
=REPT(9,LEN(B11)+2)-B11
Perhaps I'm missing something, though simply:
=REPT(9,2+LEN(A1))-A1
seems good to me.
Regards

How to detect values that do not fit in Excel cells, using VBA?

We are generating long Excel sheets using various tools, which have to be reviewed and used as input further down in the workflow.
The problem that some cells are too small for texts they contain.
So humans and programs that are reading the worksheets will not see the same data.
This is usually true for merged cells containing auto-wrapped texts, when Excel does not adjust the row height properly. But there are also other cases: for instance, when some columns have width explicitly set, which is not enough for long values.
|Group|Def1 |Subgroup|Definition| Id |Data |Comment |
|-------------------------------------------------------|
| G1 | | G1-1 |Important |G1-1-1|... | |
| |Long | |about G1-1|G1-1-2|.....|........ |
| |text |-------------------------------------------|
| |about| G1-2 |Another |G1-2-1|... | |
| |group| |important |G1-2-2|... |long comme|
| |G1. | |text about|G1-2-3| | |
|-------------------------------------------------------|
Here, some cells in "Definition" and "Comment" are not fully visible.
Is there any method to find such cells programmatically?
To detect these cells (I'm not talking about fixing the problem), you could use the Text method of a Range object.
For example, Range("A1").Value might be 123456789, but if it's formatted as Number and the column is not wide enough, Range("A1").Text will be "###" (or however many # signs fit in the cell).
Here's a trick I've used before:
With Columns("B:B")
oldWidth = .ColumnWidth ' Save original width
.EntireColumn.AutoFit
fitWidth = .ColumnWidth ' Get width required to fit entire text
.ColumnWidth = oldWidth ' Restore original width
If oldWidth < fitWidth Then
' Text is too wide for column.
' Do stuff.
End If
End With
Of course this will apply to an entire column at a time. You can still use this by copying cells over one by one to a dummy column and do the AutoFit test there.
But probably more useful to you is an earlier answer of mine to this question: Split text across multiple rows according to column width. It describes a method to determine the width of the text in any given cell (and compare it to the cell's actual width to determine whether the text fits or not).
EDIT Responding to your comment: If some of your cells are tall enough to show 2 or more lines of text, then you can use a similar approach as described in my previous answer, first using .EntireRow.AutoFit to determine the height of the font and .RowHeight to determine how many lines fit in the cell, then figuring out whether the text can fit in that number of lines in a cell of that width, using the method of the previous question.
Unmerge all cells in the workbook and use
Thisworkbook.sheets("Name").rows(index).entirerow.autofit
And the same for columns.
What's the use of keeping the merged cells anyway except for esthetic reasons?
Only the value of the "base cell" is taken into account (upper left).
I bumped into the same problem today. I am trying this trick to dodge it. Perhaps, it might be useful for you. It is coded to deal with one column width merging areas:
'Sheet2 is just merely support tool no data sheet in ThisWorkbook
With Sheet2.Range(target.Address)
target.Copy
.PasteSpecial xlPasteAll
.UnMerge
If .MergeArea.Count > 1 Then .UnMerge
.ColumnWidth = target.ColumnWidth
.Value = target.Value
.EntireRow.AutoFit
target.MergeArea.EntireRow.RowHeight = _
1.05 * .RowHeight / target.MergeArea.Rows.Count
.ClearContents
.ClearFormats
End With
Unfortunately, if there are several columns with merged cells like this, perhaps their mutual needed height will collide with each other and extra code will be needed for restoring harmony. Looks like an interesting piece of code.
Wish you find this helpful.

Changing .Interior.ColorIndex has no effect

In Excel 2003, when I change Series.Interior.ColorIndex to a value I need, it has no effect. It has an effect only when I first manually change color and then run the macro. Apparently this triggers some update mechanism. Does anyone have an explanation for this? Is there a way to somehow trigger this in the chart?.. I.e. make sure that the color changes take effect.
In addition, when I step through the code and watch ColorIndex, it does not change even after value is assigned. Is this one of the many bugs in Excel?
The code looks like this:
Sub DoStuff()
Dim j As Long
For j = 1 To ActiveChart.SeriesCollection.Count
With ActiveChart.SeriesCollection(j)
Select Case ActiveChart.SeriesCollection(j).Name
Case "Milk"
.Interior.ColorIndex = 4
Case "Cookies"
.Interior.ColorIndex = 28
Case "Honey"
.Interior.ColorIndex = 26
End Select
End With
Next j
End Sub
Edit: I am working with bar chart.
Try setting the border as well.
.Interior.ColorIndex = 4
.Border.ColorIndex = 4
.Border.Weight = xlMedium
Edit: In response to comments & edits to the original question:
I laid out an example Excel file, and I was able to get your code to work. Here is how my data is laid out:
| A | B | C | D |
--------------------------------
1 | Milk | 3 | 1 | 4 |
2 | Cookies | 1 | 5 | 9 |
3 | Honey | 2 | 6 | 5 |
And the bar graph looks something like this:
._.
|C|
._.._. |C|._.
._. ._. |C||H| ._.|C||H|
|M|._.|H| ._.|C||H| |M||C||H|
|M||C||H| |M||C||H| |M||C||H|
---------------------------------
1 2 3
Where all of the bars labeled "M" in the above diagram belong to the "Milk" series, all of the bars labeled "C" belong to the "Cookies" series, and all of the bars labeled "H" belong to the "Honey" series.
When I run your code on this chart, the bar colors are changed as expected. Can you tell me what is different between my setup and yours? I'll try to figure it out, but I need more information :)
probably a bug in Excel, which version of Excel are you using?
make sure you service pack/patch it and try again to reproduce the problem.
I had the same problem, using excel 2007 and 2003.
But i fixed it by opening the workbook in 2003, then formatting the data series by right-clicking (etc) and setting it's fill color to Automatic. then when i ran my macro, the colorindex setting part would take hold.

Resources