So this is a test function I created before I try implementing but I need a better way to do it
I'm trying to create a function that looks up a text value in a table column, and if that value is TRUE, to proceed with a specific formula. my problem is I want this formula to be more exact and do this for only each cell in the table(the table is dynamic by it's total amount of rows is everchanging) that. An example of what I want is if (lets say our Variable range is at 2 and Table1[Column1] is located on G Column)G2 is equal to "RTBR" then I want the value of H2(or another subsequent column of designation, although "RTBR" will only be in one specific Column and subsequent columns will have other designators.) there's going to be 3 criteria values, and each criteria has it's own column and sperate value, but each row could have values in each column, and i need columns that don't match not to change.
Sub Jim()
Dim VarG As Range
For Each VarG In Worksheets("Sheet1").Range("Table1[Column1]")
If VarG.Value2 = "RTBR" Then
With Worksheets("Sheet1").Range("z2:z4")
.FormulaR1C1 = "=R3C1+R[0]C9"
.Value = .Value
End With
With Worksheets("Sheet1").Range("Table1[Column2]")
.FormulaR1C1 = "=R[0]C26"
.Value = .Value
End With
Else
End If
Next
With Worksheets("Sheet1").Range("z2:z4")
.ClearContents
End With
End Sub
Status
Days of RTBR
Days of ORSD
Days of EH
RTBR
3
2
1
EH
5
3
1
*Notes: the Z column is a value place hold column because I don't know another of doing this formula without losing the absolute value of the cell.
also an explanation of R1C1 formula, R3C1 is just a SUM of a different formula(pretty much today's date minus a previous date of when the workbook was opened to give me the adjusted days.) that I'm applying to these cells if they meet the requirement of the if statement. Ii it doesn't need to be a R1C1 formula then that's fine, I should be able to clean up that, Ideally if there's a way to do this without bringing another cell into the equation that would be great.
Update: If there is a way to take the range of VarG row value and use it as a variable in R1C1 that would be highly apricated and would solve my problem
Related
I'm looking for a way by VBA or whichever works, even 1 command button but 4-5 processes to be called
Call process1
Call process2
End Sub
on one click which somehow seems impossible for me as I'm not that advanced in Excel VBA, but hopefully this challenge is a piece of cake for others. The task is to highlight cells horizontally by range based on the formula referring to a cell in Column AM. So "B8" is an amount of 100 which needs to be divided by the number of partitions appearing on "AM3". So 100/6. Now "AM3" is 6 so starting from "C8" a number of 6 cells (Merged in 4's) will be highlighted horizontally. "C9" is relating to "AM4" which is having a value of 9 and will highlight 9 cells (Total of 36 cells since merged) horizontally .
For now this is what i have applied but it limits to only within that range :
Sub HighlightRangeOfCells()
Dim rng As Range
For Each rng In Range("C8:AL12")
If IsNumeric(rng.Value) Then
If rng.Value <> 0 Then
rng.Interior.Color = vbRed
End If
End If
Next rng
End Sub
Thanks for your efforts and reply whatever it may be if possible or not.
As mentioned by SSlinky, the best way to do this is using conditional formatting. I have created a simple Excel sheet for explaining you how to do such a thing: I have put the formula =COLUMN()<=$J1 in cell "A1", I have used this formula for conditional formatting, I have dragged to the right, and then I have dragged down.
This is what it looks like:
As you see, cells get highlighted when their column number in some way corresponds with the value of column "J". All you need to do is replace "J" by "AM" and describe the correspondence as you see fit.
For your information: in the formula, I'm using the reference $J1, which means that while dragging and dropping, the row number might change, but the referred column always needs to be "J" (it's a combination of absolute and relative cell references).
I have a very long formula in columns I2:HM13 that contains values that need to change depending on what department uses the worksheet and these values may change over the course of the year. This formula repeats in all cells within the stated range with only the reference column/row changing. I would like to create a UserForm were the end user can input the values specific to their department in textboxes and click a commandbutton and the values in the textboxes get inserted into the formula.
I have tried some find and replace codes, but nothing I have tried has work at all. Below is the formula I am trying to modify.
=IF($H2="No","",IF(($B2-I$1)<100,"",IF(((($B2-I$1)*$C2)*1000000)<260000000000,"",IF(((I$1*$C2)*1000000)<330000000000,"",IF(AND(($B2-I$1)>=200,($B2-I$1)<=800,((($B2-I$1)*$C2)*1000000)>=620000000000,((($B2-I$1)*$C2)*1000000)<=920000000000,I$1>=375,I$1<=420,((I$1*$C2)*1000000)>=680000000000,((I$1*$C2)*1000000)<=790000000000,$C2>=1300,$C2<=2100),1)))))
The above is a small portion of the formula of interest. The values I am trying to changed based on the textbox inputs are all the values after the less than, less than or equal to, greater than, or greater than or equal to symbols.
For example in textbox 1 the user inputs 150, 150 would then replace all "100" in the formula. If the user inputs 270000000000 in textbox 2, all the "260000000000" would be replaced with 270000000000.
Your best approach would be to create sheet-scoped names which you then use in your formulas: your userform can then change the "refersto" values for those names to the user-supplied values.
Eg:
With ActiveSheet
.Names("FOO").RefersTo = txtName1.Value
.Names("BAR").RefersTo = txtName2.Value
End With
On your sheet:
=FOO/BAR
Found this solution to my question:
Private Sub CommandButton1_Click()
Dim cell As Range
For Each cell In Range("I2:HM2")
cell.Formula = Replace(cell.Formula, "100,", TextBox4.Value + ",")
cell.Formula = Replace(cell.Formula, "260000000000,", TextBox5.Value + ",")
Next cell
End Sub
I would need to repeat this format for all the values I would want to change per department.
I am trying to Create a Formula that checks 4 Cells next to each other if they have the same number once positive and once negative see in the example:
If the formula sees there is a Plus 50 and a Minus 50 its has to colour the cell on the right side or the cells with the numbers blue.
The list is a inventory of multiple stores if one store sells alot of that product and may run out they ask another store to restock the product. Sometimes they forget to send a note. This List is supposed to make the control as easy as possible.
I expect the formula to color the cell on the right side of the list to be colored blue if 2 of the cells have the same value in plus and minus.
I tried to use cell formatting rules but its not possible to do it with that.
Another Example since people seem to have trouble understanding what the formula should do:
I marked every cell blue like the formula should and yellow colored value is the reason.
You can do this, using conditional formatting, using this formula (just for the first row):
=OR(A1+B1=0;B1+C1=0;C1+D1=0)
This formula checks if the sum of two adjacent cells equals zero, which is another way of saying that they should have the same value, but opposite signs.
Obviously, you might consider changing this formula, e.g.:
Instead of:
A1+B1=0
you put:
AND(A1+B1=0;A1<>0)
When the sum of two values equals zero and at least one of them is not zero, then both are not zero.
All this together in one formula yields the following:
=OR(AND(A1+B1=0;A1<>0);AND(B1+C1=0;B1<>0);AND(C1+D1=0;C1<>0))
Use such a formula in the conditional formatting of cell E1, and apply this for all cells in E column.
Try:
Option Explicit
Sub test()
Dim Row As Long, Column As Long
Dim rng As Range
'Let us assume that we use Sheet1 & columns A to F
With ThisWorkbook.Worksheets("Sheet1")
For Row = 2 To 100 ' <- Let us assume that data starts in row 2 and ends in row 100
Set rng = .Range("B" & Row & ":E" & Row)
For Column = 2 To 6
If .Cells(Row, Column).Value <> 0 Then
If Application.WorksheetFunction.CountIf(rng, (-1 * .Cells(Row, Column).Value)) > 0 Then
.Range("F" & Row).Interior.Color = vbBlue
Exit For
End If
End If
Next Column
Next Row
End With
End Sub
I am working on a model that requires me to sort data in a range by date, before copying the data into a different template on another sheet. Every row has a formula that pulls data from Bloomberg, so even if the cell looks empty, excel recognizes that there is content in the cell. Sorting the date column as it is does not work, as excel wont recognize the data that Bloomberg pulls as a date, so it would be sorted from A to Z, which scrambles the dates instead.
To work around this, I inserted and adjacent column with the formula (using cell A1 as an example) "=(A1+0)" that then allows the column to be sorted from new to old. The problem here is that if cell A1 does not display a date (in other words, it appears empty yet the cell contains a formula that leaves the cell looking empty if no date is pulled) it returns a #VALUE! error and sorting new to old would put the errors at the top (Thank you Excel for this fantastic feature, btw).
To work around this new issue, I replaced the above formula in the cell with "=IFERROR(A1+0,1)" which gives me the date 1/1/1900. Fine, now the data is sorted in the manner I want it, but I have a bunch of ancient dates that just make my end product look ugly.
I have two questions, first; how can I use VBA to delete the data in the cells where the date equals 1 (which shows the date 1/1/1900), and only those cells? Or, alternatively, only copying the rows above the cell that contains 1/1/1900. This is a relatively small amount of cells that would be affected by this, 40 at most.
Second; is there a different way of sorting the data using VBA that I am missing, that might be more efficient?
Try this macro, I tried to test it with as many anomalies as possible.
Sub SortByDateColumnH()
Dim r As Range: Set r = Sheet1.Range("B3:P40")
Dim cel As Range
For Each cel In r.Columns(7).Cells
If IsError(cel.Value) Then
cel.Value = 0
ElseIf Not IsDate(cel.Value) Then
cel.Value = 0
Else
cel.Value = CLng(cel.Value)
End If
Next
r.Columns(7).NumberFormat = "0"
r.Sort Key1:=r.Cells(1, 7), Order1:=xlDescending, Header:=xlNo
For Each cel In r.Columns(7).Cells
If cel.Value < 100 Then cel.ClearContents
Next
r.Columns(7).NumberFormat = "m/d/yyyy" '<-- set the format to your preference
'r.Copy Destination:=someDestination ' you can copy the range by code if needed
End Sub
Hi guys this is my first post, I'm wondering if you can possibly assist me.
I'd like to write a macro / script that will allow me to put a formula into the column to the right of the currently selected one (for all active rows of the current column) based on what column I've selected. The issue I'm having is that I don't always know the current column letter (as my selection changes from worksheet to worksheet).
To give you an example:
One of my columns currently contains dates, that dates are entered in different formats though, some are separated with ".", some with "-", some with spaces and so on. I have a formula that will deal with this so I need to put this formula in the column to the right of the selected column (which has the dates).
I have been able to do this when I specify the column letter, but not if it changes.
Please can you help?
Give this a go,
Sub SomethingNeat()
Dim rng As Range, x
x = Selection.Column
On Error Resume Next
Set rng = Columns(x).SpecialCells(xlCellTypeConstants, 23)
If Not rng Is Nothing Then rng.Offset(, 1) = "'=MyFormula"
End Sub
You can use ActiveCell.Offset(0,1).Value = Variable
That means that whetever your current cell is you can move and "select" to put a value to the right cell of the one you have activated. You can move the selection using a loop.
Do
Workbooks("Yur workbook name").Worksheets(1).Range(Adress you want to start adding).Offset(0, 1).formula = "=FORMULA"
i = i + 1
ActiveCell.Offset(1, 0).Activate
Loop While i <= max_row
Edit: 2nd
Put the formula in a cell lets say C1
'Select a range
Set take = Worksheets(1).Range("C1")
take.Copy 'copy the formula
Worksheets(1).Paste Destination:=Worksheets(1).Range("B1:B10")
That will copy your function whenever you want it to