VBA Code in Excel to select a cell based on two criteria - vba

What I want to happen is for a button to be pressed on "PP1 & PP2 Shortage list".
When this button is pressed I want the macro to take the values from D5 and F5 and locate the row where these two values appear in separate columns on "PP1 Paint plan".
It will then highlight columns A:AF below the found row copy everything and paste this back in to A2.
I have tried a number of loop codes and find codes but so far I have had no luck, I'm hoping one of you brilliant people could shed some fresh thinking on the problem.

R3uk is right but as you're looking for fresh ideas.
Why not combine a formula that can find the row number on "PP1 Paint plan" that matches your criteria with a simple macro that uses that row number to copy everything below. no looping required!
Put the formula below into an empty cell on the same sheet as you D5 and F5 cells. I've used A1 in the macro. Change the search columns accoridingly (I used $D:$D and $F:$F). IMPORTANT: press SHIFT+CTRL+ENTER to add {} to this formula.
=MATCH(D5&F5,'PP1 Paint plan'!$D:$D&'PP1 Paint plan'!$F:$F,0)
Next copy this code and change it as required. It sets a variable called intFoundRow to the cell that contains the formula (change the A1 to your cell reference). It then copies everything below that row and pastes it into your destination sheet. I'm not sure what that is called so please change the text.
Sub CopyShortageData()
intFoundRow = Sheets("PP1 & PP2 Shortage list").Range("A1").Value
Range(Sheets("PP1 Paint plan").Range("A" & intFoundRow & ":AF" & intFoundRow), Sheets("PP1 Paint plan").Range("A" & intFoundRow & ":AF" & intFoundRow).End(xlDown)).Copy
Sheets("Your destination sheet").Range("A2").PasteSpecial xlPasteAll
End Sub

Related

Excel moving cell value down one row continually

I have a workbook where there are two cells, namely DX42 and DX43. These cells have values derived directly from a neighbouring sheet called RANDBETWEEN. The formula for DX42 is =RANDBETWEEN!A2 and for DX43 is =RANDBETWEEN!B2. In sheet RANDBETWEEN, there are two columns (A for DX42 and B for DX43) that span around 100 rows, full of numbers gradually increasing over time.
What I want to do is have a macro that will automatically shift the values of both DX42 and DX43 down one row every time I press a button. For example, I press once and DX42 becomes =RANDBETWEEN!A3 while DX43 becomes =RANDBETWEEN!B3. I click again and they become =...!A4 and =...!B4 etc etc.
If it is not possible to shift both cells at the same time, I'm fine with having two separate buttons (one for DX42 and one for DX43) that shift their respective values individually.
Also, the macros I've already tried (without success) take me into sheet RANDBETWEEN every time I click the button. I would prefer to remain in sheet1 (my main sheet) while the macro does its thing, so that I can continuously press the button without having to navigate back from RANDBETWEEN to sheet1 every time I do.
Can anyone help me out?
Many thanks!
Try associating this sub procedure with a button,
sub shiftDXdown()
range("DX42").formula = _
"=" & worksheets("randbetween").range(mid(range("DX42").formula, 2)).offset(1, 0).address(external:=true)
range("DX43").formula = _
"=" & worksheets("randbetween").range(mid(range("DX43").formula, 2)).offset(1, 0).address(external:=true)
end sub
It would be better if a proper parent worksheet reference was available for DX42 and DX43 but I would imagine you are putting this into the parent worksheet's private code sheet.

Writing a cell in Excel

This query might seem a bit childish, but this has been bugging me for quite a few days.
I am completely new to Microsoft Excel and want to know how do hold same values in two different excel cells?
Example: Cell A1=bandwidth Cell E1=[minimum:bandwidth():utilization]
I want the word "bandwidth" to be same in both the cells. So if the user
inputs "bandwidth" into cell A1, then this generated into E1.
Kindly help me with this one.
In E1 enter the formula:
="minimum:" & A1 & "():utilization"
as you update A1, the displayed text in E1 will follow.
Adding to the solutions already given, you can define the format to be used for A1 with the TEXT function:
="minimum:" & TEXT(A1,"#,##0.00") & "():utilization"

How to do a summation only within specific cells via VBA

In reference to the picture below, I would like to loop through a certain column (Column D in this case) until I hit a specific cell (Yellow cells in this case). In my final spreadsheet I have multiple yellow cells that I would like to target. Once I hit a yellow cell, I would like to start a simple summation of the values one cell to the left of the yellow (Column C). I would like to keep summing the values until I hit a blank cell, which would indicate the end of the set.
Please let me know if you need any more clarification!
Here's some code that should get the job done. However you are going to have to adapt it to however you want to use it.
Dim Summation as Double
For Each Target in Range("D:D")
If Target.Interior.ColorValue = 6 Then
Summation = Summation + Target.Offset(0, -1).Value
End If
Next Target
I hope this helps. However, don't forget about FreeMan's suggestions about good question asking and using the macro recorder!

How to highlight a cell when formula result from another sheet changes?

This is one that's been killing me and I've tried almost every solution on the Internet.
Here's background. I have an HR model that has each department broken out on separate tabs. I want to run an extract from our payroll system each payroll run and send highlight any updates individually. If someone's title or salary or status changes, I want to have that called out by highlighting the cell.
Each tab uses an INDEX/MATCH lookup to the extract tab to pull in the current information. What I want is if any value changes or is new(new hire, for example), highlight the cells.
I've played with Worksheet_Calculate and Worksheet_Change to no avail. Worksheet_Change doesn't fire because I'm not making the change directly on the sheet and Worksheet_Calculate doesn't have the Target object for to reference. I've tried the following code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim updatedCell As Range
Set updatedCell = Range(Target.Dependents.Address)
If Not Intersect(updatedCell, Range("A:A")) Is Nothing Then
updatedCell.Interior.ColorIndex = 3
End If
End Sub
The range I actually need evaluated is A7:R104 but I've been trying to get anything to work when linked to another sheet.
This works fine if formula of target cell is pointing to another cell on same sheet. The moment you point to one on another sheet it doesn't work. I've tried most of the solutions on here with no success. I've even tried putting the Worksheet_Change on the extract sheet and see if I can trigger it that way with no luck.
Is there a recommended solution to triggering a change to a cell for a formula linked to another sheet?
so I just saw this post, I don't know if you've found the solution or are still looking, but:
if you select a cell in sheet 3, you can then go to the home tab, go to "conditional formatting" -highlight cell rules - more rules (at the bottom) - and "use formulas to determine which cells to format" and then put your cursor in the formula box. now, select a cell in sheet 1 (click the sheet1 tab, and click a cell) and you'll notice it should populate the address for sheet1, and the cell u selected. now type <> after that cells address, then select sheet2 and a cell. then click format, and choose a fill color. then ok. if you go to conditional formatting and manage rules it will show there the rule / formula and which cells it applies to.
doing this i was able to select cell D10 in sheet 3, and make it an ugly green if cells in sheet1 and 2 didnt match (I picked which cells) you can also select a range of cells.
thusly, you can apply this rule to whatever dells you want, and if you record a macro of you setting this conditional formatting, you can manitpulate that macro to apply it to a bunch of different cells, and change the ranges. (using loops / variables)

I have created a macro using the if function but it doesn't seem to work. What am I misisng?

I created a macro to show the following:
If (I3<>0,I3*G3,H3*G3) and this repeats itself for cell N3, R3, V3, Z3 etc.
Option Explicit
Sub Eg()
Range("J3, N3,R3, V3,Z3,AD3,AH3,AL3,AP3,AT3,Ax3,BB3,XF3,BJ3").Formula = "=IF(RC[-1]<>0,RC[-1]*RC[-3],RC[-2]*RC[-3])"
End Sub
However this doesn't seem to work.
Let me explain a bit more how this should work:
This report needs to be downloaded from an application.
The macro needs to be attached to this report so that when I download the report the macro automatically runs this formula in the appropriate columns.
Also I'll have to populate the spreadsheet for all the rows with this formula.
The columns where the formula should sit are not blank but this needs to be catered for in the report automatically once the macro is run.
What am I missing here?
When you use Range.Formula = formulaAsString, and the range refers to multiple cells, you specify the exact formula string as required by (only) the first cell in the range, while appropriately using relative vs. absolute cell references because the assignment to multiple (succeeding) cells will occur as if you were pasting the first cell in the range into the others, exactly as if you'd done a copy & paste without VBA -- you use absolute addressing A1 vs. A$1 vs. $A$1 vs. $A1 etc... as desired to achieve the right alteration of the formula for the succeeding cells.
For example,
Range ("A1, C1, E1").Formula = "=A2+$A2"
will have the same result as
Range ( "A1" ).Formula = "=A2+$A2"
Range ( "C1" ).Formula = "=C2+$A2"
Range ( "E1" ).Formula = "=E2+$A2"
You are mixing up .Formula with .FormulaR1C1! Your string is R1C1 style, but you assign it to the A1 style formula.
Therefore, simply, change it to:
Range("J3, N3,R3, V3,Z3,AD3,AH3,AL3,AP3,AT3,Ax3,BB3,XF3,BJ3").FormulaR1C1 = _
"=IF(RC[-1]<>0,RC[-1]*RC[-3],RC[-2]*RC[-3])"
or
Range("J3, N3,R3, V3,Z3,AD3,AH3,AL3,AP3,AT3,Ax3,BB3,XF3,BJ3").Formula = _
"=IF(I3<>0,I3*G3,H3*G3)"
As Erik points out in his answer, also the later will work and adjust the formula for each cell in the same way (which is not necessary in R1C1 as the formula stays the same anyway...)