I have an excel file, i need to do is, format the cell depending on another cell.
If cell "S1" is having 3 decimals cell "T1" should be have the same number of decimals and it is calculated field.
ex:
S1 = 11.123 ---- T1 = 1.099
I need to do for the entire column.
I am able count the number of decimals on the cell by placing the code on worksheet_change. But i have no idea how to format it.
Please help in this regards.
Thanks in advance
Since you wanted an example, here it is :) But please note that this is not an answer. It's just that the comments will not be able to hold all this data plus it will ruin the formatting as well. Having said that, it is almost as good as an answer as it covers almost every aspect of what you want...
My Assumptions:
Data is in Sheet1
Col S and Col T are identical
Logic to achieve what you want
Get the last row of Col S. For example see this
Loop though the cells in Col S and check the number for decimals
Use .Numberformat for format the cells in Col T based on the number of decimals in respective cell in Col S
Few code snippets
Looping Through cells in Col S
For i = 1 To LastRow
If Sheets("Sheet1").Range("S" & i) .... Then
End If
Next i
Setting the number format of cell T in the above loop
'~~> n below is the number of decimal places that you want
Sheets("Sheet1").Range("T" & i).NumberFormat = "0." & String(n, "0")
Incorporate all these and then try to come up with a code. Let use know where you are stuck and we will take it from there.
Related
I'm new to VBA and was just trying to figure this out.
I have a the following range CR7, CR9:CR24, CR28:CR39, CR45:CR50, CR52
Currently, each of these cells reference a value from another worksheet.
CR7='Trend'!CO7
CR9='Trend'!CO9
CR10='Trend'!CO10
This will follow the entire range.
What I was wanting to do was change the reference to 3 columns to the right.
CR7='Trend'!CO7 will then be CR7='Trend'!CR7
What I was trying to do was remove the last 3 characters after the "!" and then replace it with "RC[-3]" but I don't know what I'm doing at all. I just looked up different codes on this site and tried to piece it together, knowing that I would probably run into some obstacles because I'm new at this.
Here's the code I was trying to implement but running into an error.
For Each C In Range("CR7,CR9:CR24,CR28:CR39,CR45:CR50,CR52")
C.Value = Left(C.Value, InStr(C.Value, "!") - 1)
C.Value = C.Value & "RC[-3]"
Next
Any suggestions on how to get this corrected or is there a better way in doing this? Thanks in advance for looking into this!!
Solution
As stated in the comments, since you are looking for a specific header, the solution might be faster as follows:
Assuming the headers are in Sheet 1 as follows
Jan-22 (IE:actual data is 01-01-2022)
Feb-22
Mar-22
10
30
60
20
40
70
Where A1 = Jan-22, B2= Feb-22, etc.
Just use a combination of index and match, seems like you need to bring the row at the same level, so it will be as follows:
Set a referece in your formula to where you will have the criteria to look: In this example will be A1
=INDEX(Sheet1!$A:$C,ROW(),MATCH($A$1,Sheet1!$1:$1,0))
I'll explain:
Index will bring the column where you want the data, in this case will be the data under the month (Col A), seems like the row should be at the same level, so we will say the row will be equal to where the formula row is being casted, that's why the row(), finally, it will be in where the column matches the criteria that you want where the header is found, if you need an offset even after finding the column name, just sum it up at that part
MATCH($A$1,Sheet1!$1:$1,0) + 3
Demo:
I'm working on data-analysis where i would like to be able to automatize color fill when looking through large amount of data where there are abundant amount of ghost logs and taking too much of my time as they are severely irrelevant.
So what i would like to do in Excel, is to be able to color fill a cell when the number changes in a column marking a different set of logs.
For instance if there are six rows of log number 456455, i would like the code to color fill the first cell when the number changes to 456456 so that it helps me identify logs faster when i know where the sets are starting. I'm kind of a newbie when it comes to Excel but this loop would help me a lot!
Thx for your time
This can be done with conditional formatting. Use a rule that compares the current cell with the cell in the row above and format if the two are different. Note that you will need to use relative references without $ signs. In the screenshot below, the conditional format is applied from row 2 to 19 and in row 2 the formula compares A2 with A1, in the next row it compares A3 with A2, and so on. If the two cells are different, the cell will change colour.
If you have some knowledge in VBA, you can implement a macro that looks at the column where you have your log number and if the value changes from one cell to another, then you highlight this cell.
I attached a template of code that works for this task.
Sub highlightChange()
Dim preVal As Integer
preVal = 0
For Each o In Range("A:A")
'Go through column
If o.Value <> preVal Then
o.Interior.Color = vbRed 'Color the selection
End If
preVal = o.Value
Next o
End Sub
There may be other solution without VBA, however, it is quite easy and practical to use a macro.
I really hate to ask what I feel like should be able to be resolved by a quick run to a search engine, but I have looked to no avail.
I am extracting some data from a CSV that really should be expressed as a decimal but is not (it should 757.46 not 75746 for example). Using Format(expresion, "Currency"), Format(expression, "Fixed") and so on, does not have the desired effect. The functions just append a decimal point and extra zeroes (75746.00 instead of 757.46). Is there any an easy way to indicate to the Module to take an integer, add a decimal with two numbers to right and variable numbers to the left, and not add anything extra. I am sure there is a way to do this by converting to string, using sub strings and the like, but I would like to avoid that if possible.
Thanks!
If using Format(expresion, "Currency") is giving you 75746.00 as the cell, then Format(expression/100, "Currency") should give the right result. You would also want to put in a check that expression did not already have a decimal point
Dividing the numbers you've been given by 100 and then formatting as currency should work.
Use the code below, it will loop through the rows and put the orig values from Column A in Column B:
Sub ChangeOrigFormat()
Dim lrow As Long
' modify to your last row , or use dynamic last row method
For lrow = 2 To 10
' put result on Column B
With Cells(lrow, 2)
' looking in original values in Column A >> modify according to your needs
.Value = Cells(lrow, 1) / 100
' using a decimal with 2 digits after the "."
.NumberFormat = "0.00"
End With
Next lrow
End Sub
I have two worksheets with data from different sources. I need to copy the data to a single worksheet and remove duplicates. To achieve this objective, I need all the data formatted the same on both worksheets. All of this is already coded except with one column of data I am having issues. These columns contain a representation for percentage. In worksheet A, the value is showing as .4386 which equates to 43.86%. I use this code that converts the value without issue:
Worksheets("Verification").Range("F2:F2000").NumberFormat = "0.00%"
In worksheet B, the same data is shown as 43.86, but the above code changes it to 4386.00%. I also tried changing this line to .NumberFormat = "General\%" and this almost works, but returns a value of 44%. What do I need to add to my code to get this to show 43.86% on worksheet B?
Sorry for the slow reply in comments - I will just submit an answer.
Like Ralph said, it's really better to make sure they are the same number.
43.1 and .431 are not the same number.
For Each c In [A1:A10]
If c.Value < 1 Then
c.Value = c.Value * 100
End If
c.NumberFormat = "0.00\%"
Next c
Results:
You are stating that .4386 on worksheet A is the same data [...] as 43.86 on worksheet B. So, Excel is correct to convert 43.86 to 4386.00%. Maybe you need a conditional formatting: when the number is smaller or equal to 1 then format it "0.00%" and otherwise format it as "0.00""%""".
Yet, I would assume that you'll be running into problems when comparing the data between the sheets with this solution. Hence, I would divide all numbers on sheet B by a 100 first to really make them comparable.
Note, that just by making numbers "look alike" they are not the same. Example: write in cell A1 the value 1000 and in cell B1 also 1000. Then change the number format for A1 to 0 and the number format for B1 to 0, (or to 0. outside the US). A1 will show 1000 while B1 will show 1. If you ask in cell C1 =A1=B1 you will get a TRUE as the answer.
There are some food names and prices as you can see between I2 and J22. For instance AYÇICEK YAĞI(SUNFLOWER OIL IN ENGLISH) is 4$ per kg. In the left of the sheet, you can see other list. What I need is;
I want to compare all A* columns with Strings between I2:I22 and get the price which is written between J2:J22 then write it to the D* columns.
There are more than 500 rows and I need to do it for all rows.
And there are some headings as u can see in bold font, they should be protected.
You seem to have come up with a formula; now you need a way to dispense it. Your worksheet design does not lend itself to simply filling down a formula. However, with the numbers in column C identifying valid entries that require a formula in columns D and E, a short sub procedure can make quick work of putting the formulas into the correct places.
Sub fillFormula()
Dim w As Long, vWSs As Variant, vFRMLs As Variant
vWSs = Array("ogle_aksam_gramaj", "kahvalt" & ChrW(305) & "_gramaj", _
"araogun_gramaj")
For w = LBound(vWSs) To UBound(vWSs)
With Worksheets(vWSs(w))
With .Columns(3) '<~~ going to look at column C for numbers
With .SpecialCells(xlCellTypeConstants, xlNumbers)
.Offset(0, 1).FormulaR1C1 = _
"=IFERROR(VLOOKUP(RC1, 'urunler'!C1:C2, 2, FALSE), """")"
.Offset(0, 2).FormulaR1C1 = _
"=IFERROR(RC4*RC3, """")"
End With
End With
End With
Next w
End Sub
The IFERROR function has been used to 'wrap' the lookup and mulltiplication formulas. It catches errors and offers alternative results; in this case, zero-length strings that look blank.
The kahvaltı_gramaj worksheet causes problems in VBA due to the unicode character. You might try other methods of cycling through the worksheets.
That binary (macro-enabled) workbook is available from my own public dropbox here.
In the workbook you have attached, VLOOKUP will return #N/A when there is no available value.
In Sheet ogle_aksam_gramaj Cell D4 use the following Formula:
=SUMIF($I:$I,$A4,$J:$J)
You can then drag it down and it should be giving you the prices based on the details provided in the same sheet (Range I:J)
The good thing (or bad, depends on you) of sum if is that it will return 0 if there is nothing to sum. in your sheet, the items must be unique in the list, otherwise, it will keep summing every instance. So if AYÇICEK YAĞI is there 2 times, it will be summed twice.
You can use Advanced Filter with (unique values only) to make sure that all the ingredients are accounted for and are mentioned only once.
Thanks.