Input formula into cell using xlR1C1 style - vba

I have a formula to implement in xlR1C1 style.
I add a new marketing entry into my marketing database. I want Cells (emptyRowM,9) to search a marketing referral code in Cells (emptyRowM, 7) in the pivot table of another sheet. This pivot table shows the total revenue associated to each marketing referral code. Hence if Cells (emptyRowM, 7) is zero, Cells (emptyRowM, 9 will be blank, otherwise, it will sum up all the revenue associated with the referral code.
Dim emptyRowM As Long
emptyRowM = Range("B100000").End(xlUp).Offset(1, 0).Row
Cells (emptyRowM, 9) = "=IF(AND(SUMIF('Pivot Table'!B6:B99,'Marketing Data'!G12,'Pivot Table'!C6:C99)=0),"",SUMIF('Pivot Table'!B6:B99,'Marketing Data'!G12,'Pivot Table'!C6:C99))"

Go File-> Options -> Formulas there under working with Formula tick "R1C1 reference style"
So your formula should become something like
=IF(AND(SUMIF('Pivot Table'!R[-5]C[-5]:R[88]C[-5],'Marketing Data'!R[1]C,'Pivot Table'!R[-5]C[-4]:R[88]C[-4])=0),"",SUMIF('Pivot Table'!R[-5]C[-5]:R[88]C[-5],'Marketing Data'!
Then use the Cells (emptyRowM, 9).FormulaR1C1="=IF(AND(SUMIF('Pivot Table'!R[-5]C[-5]:R[88]C[-5],'Marketing Data'!R[1]C,'Pivot Table'!R[-5]C[-4]:R[88]C[-4])=0),"",SUMIF('Pivot Table'!R[-5]C[-5]:R[88]C[-5],'Marketing Data'!"
Let me know if you have any problems.
Cheers,
Sujoy

Related

Excel code to look for a specific text in a cell (from a list of possible texts) and populate another cell

So I am populating a spreadsheet with a copy n paste of my banking transactions. I want excel to populate a cell 10 to the left of it with that specific costs cost centre number.
Eg, Petrol has a cost centre number of 10
I copy and paste my bank statement in, the sheet sees all the cells containing 'petrol' and populates the cell ten to the left with a 10
I do not want to do this with a formula, I want to put it in vba.
Can someone help please?
Thanks in advance
You can use the following code to copy a value ten cells to the left.
Cells(Row,Column).Value = Cells(Row,Column + 10).Value
To get the values for 'Row' and 'Column', you can use the find function to find the address for 'petrol' and all the other values that you want.

Find a match on a different sheet then replace a cell in that row

I'm not a VBA expert so struggling to get this to work by recording macros investigation - I currently have no macro I'm working off as the one I tried seems to delete random cells
I have a range of data:
Column A-P and rows 5-54, in Sheet 1
I have two values in sheet 2, the search ID (C2) and the new input (F2)
I'd like to search Sheet 1 column A for the search ID (sheet 2, C2) then replace the value in column P of that row with the value in Sheet 2 F2.
The use of this is on a register of items that I want to change as on or off site (the register is sheet 1), the ID of the item that is moved is put in Sheet 2 C2, then F2 uses =vlookup to show the current status of the item, this is then changed using a drop down box and I'd like this update to be copied over to the old register to update it.
The rest of the formula would be to clear the change of location form and repopulate the vlookup into the F2 cell:
Sheets("Sheet 2").Select
Range("C2").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = "HPC-RS-"
Range("F2").Select
Selection.ClearContents
ActiveCell.FormulaR1C1 = _
"=IFERROR(VLOOKUP(R2C3,'Sheet 1'!R5C1:R54C16,15,FALSE),"""")"
Your "delete random cells" comment leads me to beleive that maybe you're not intentionally using R1C1 relative cell notation in your code, as opposed to "standard/absolute" A1 notation.
For example, Range(F2) refers to the cell at the intersection of column F and row 2, whereas R4C2 refers to the cell 4 rows down and 2 columns to the right of "whatever cell is currently active".
Before recording a macro, you can enable/disable relative references with a button near the "record macro" button. (I'm on my phone so I can't double check the button...)
A relative reference in an Excel macro means relative to the currently active cell. So use caution with your active cell choice — both when you record the relative reference macro and when you run it.
This page walks you through creating a demo of both types of macros: https://www.dummies.com/software/microsoft-office/excel/recording-excel-macros-with-relative-references/

Get price from table 1 if word is in the sentence tabel 2 (Excel, VBA, Vlookup, if, ? ,...)

I try to get a price (col "B" sheet "PP") if a product (col "A" sheet "PP") is in any of sentence (col "A" sheet "MASTER"). If not, then it should pick up the average price (cell "C2" sheet "PP"). I've tried to use several methods but only this one gives SOME ;) results:
=IF((LEN(B2)-LEN(SUBSTITUTE(UPPER(B2);UPPER(LEFT(PP!A2;LEN(PP!A2)-5));"")))/LEN(LEFT(PP!A2;LEN(PP!A2)-5))>0;VLOOKUP(PP!A2;PP!$A$2:$J$82;2;0);PP!C2)
As you can see on a picture only a few products get results and it's not correct. I think it's because counting characters is different for different words and phrases. Maybe there are some other errors in this formula.
In Picture 1 you can see products with prices and its average from the sheet "PP".
In Picture 2 you can see sentences in which are products in red color - sheet "MASTER". There is one sentence - cell "B8", where are two products, and formula can pick up any of them, can be i.e the first one. Three last products on the mint background aren't in the list (sheet "PP"). Therefore for them, the formula should take an average price from cell "C2" sheet "PP".
The formula should make a loop because the products from sheet "PP" can be in a few sentences.
Can anybody help me to solve this issue?
SOLVED:
=IFERROR(LOOKUP(2^15;SEARCH(PP!$A$2:$A$11;B2);PP!$B$2:$B$11);PP!$C$2)

Copy Row From One Sheet to Another if Marked with "X" in Excel

I do purchasing for my company and have been working on a spreadsheet (Excel 2003) to help people order things. I have two worksheets, Items and Orders. I would like the user to be able to type a quantity into the cell specified and have that row of information copied over to the orders sheet.
For example, If I type 500 into cell A3 in the Items sheet, cells A3-F3 would be copied over to the Orders sheet in the first available row (happens to be A2 right now). If the next item being ordered is on A9, they would type the quantity into A9 of the Items sheet and it would copy A9-F9 to A3-F3 of the Orders sheet and so on. After the different rows are copied over, they would be able to copy the grouped up rows into an email and send it.
So, any number typed into Column A of the Items Sheet will copy A-F over to the first available row in the Orders sheet. Let me know if I left something out. Sorry if this is a dumb question but I have spent a good 5 hours and can't seem to figure this one out.
Thanks!
place this code in Items worksheet code pane:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
With Worksheets("Orders")
.Cells(.Rows.Count, "A").End(xlUp).Offset(1).Resize(, 6).Value = Target.Resize(, 6).Value
End With
End If
End Sub
Without using any macros, you could set up the sheet like this:
Make the entire first worksheet into a table (select it, and then click 'Insert'>'Table'). Have the user enter the amounts needed, then filter the table by 'Quantity' and do not include 0. Then you can copy and paste the order into an email! You don't even need the second worksheet.

Conditional Formatting or Macro Enabled - Change cell background color based on another cell value

I have a workstation inventory list that has a purchase date in cell "D" in which I have cell "I2" with =TODAY(). Cell "E" has a formula of =ROUND(YEARFRAC(D3,$I$2),0) to get the # of years value in Cell "E".
My question is regarding cell "F" I'd like for the cell to automatically change color based on the numbers of years in cell "E", but I'm struggling to do so. With either conditional formatting or finding a good macro to do so.
I have attached a screenshot to give you folks a better idea. I might've missed a similar thread while doing my research, but if anyone knows where I could look. That's a good answer for me.
You can use the regular conditional formatting:
Select "Use a formula to determine which cells to format" , set the rule like in the screen-shot below (if Years >= 1 and Years <=3).
Apply this formula to your entire range (don't forget to remove the $ sign before the row number so it will change with the rows numbers).
Repeat this step by adding 2 more rules for the other scenarios (same type by using a formula):
(if Years > 3 and Years <=4) then Yellow.
(if Years > 5) then Red. In your post you wrote: (Years > 5) in your legend in the right, but in your requested screen-shot you actually want to display if (Years >= 5) then Red. so you need to decide which one you want.
Select ColumnF and HOME > Styles - Conditional Formatting, New Rule..., Use a formula to determine which cells to format and Format values where this formula is true::
=AND(ROW()>2,E1>4,E1<>"")
Format..., select red Fill, OK, OK.
Then add a new rule of:
=AND(ROW()>2,E1<5,E1<>"")
with yellow fill and finally a third rule (though you could choose to apply a 'standard' fill instead) of:
=AND(ROW()>2,E1<4,E1<>"")
with the rather strange colour fill.
[1] If not added in the above order they should be rearranged in the Conditional Formatting Rules Manager window with red at the bottom and yellow in the middle.
[2] Check Stop if True for all three rules (or at least the top two).
[3] 0 is covered within the same rule as 1 to 3 years.
[4] To simplify range selection and updating all rules apply to the entire column. This though has complicated avoiding applying the formatting to the first two rows and any that are blank in ColumnE.
Try below conditions, it worked for me. You need to apply three different rules on same range
=$K$5>=5 - Red
=AND($K$5<5,$K$5>3) -Yellow
=AND($K$5>=1,$K$5<=3) - Green
I tend to limit conditional formatting to cases where I use them for a quite small and FIXED (never to be changed/added/deleted/moved) number of cells
Otherwise in the medium/long run it can both result in a worksheet mess (after copying&pasting and/or inserting/deleting cells) and size increase
That's why for the colouring of cells down a list I'd always use a VBA approach, like the following code:
Option Explicit
Public Sub main()
Dim cell As Range
Dim firstColor As Long, secondColor As Long, thirdColor As Long
With Worksheets("Inventory") '<--| change "Inventory" to your actual sheet name
firstColor = .Range("I9").Interior.Color '<--| change "I9" to your actual cell address with "1st color"
secondColor = .Range("I10").Interior.Color '<--| change "I10" to your actual cell address with "2nd color"
thirdColor = .Range("I11").Interior.Color '<--| change "I11" to your actual cell address with "3rd color"
For Each cell In .Range("E3", .Cells(.Rows.Count, "E").End(xlUp)).SpecialCells(XlCellType.xlCellTypeFormulas, xlNumbers) '<--| loop through column "E" from row 3 down to last non empty row cells with numbers deriving from formulas only
cell.Offset(, 1).Interior.Color = Switch(cell.value <= 3, firstColor, cell.value <= 4, secondColor, cell.value > 4, thirdColor) '<--| adjust current cell adjacent one color according to current cell value
Next cell
End With
End Sub
Note: I adjusted Switch() function conditions to match your example column "E" and "F" result, which is slightly different from what would come out of your "NO. Of Years" range legenda. Also, this latter has its first two ranges overlapping each other