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
Excel SpecialCells(xlCellTypeBlank) includes all cells in a merged range, regardless of whether they are blank. I consider this to be a bug, and I desperately need a workaround. Steps to duplicate:
(1) Create a worksheet with some text in cells A1 and B3. Now activesheet.usedrange.cells.count = 6 and
activesheet.usedrange.specialcells(xlcelltypeblanks).cells.count = 4
These values are as expected.
(2) Now merge cells A1:A2, and
activesheet.usedrange.cells.count = 6 and
activesheet.usedrange.specialcells(xlcelltypeblanks).cells.count = 5
Both A1 and A2 are now considered blank.
I need to find a way to select just cells A3, B1, and B2 in this case. I'm actually doing this using .NET Excel Interop, so I do not want to have to actually retrieve the cell value as a part of the test, since this is a huge performance penalty in Excel Interop; so I'm restricted to an internal cell selection function such as SpecialCells.
I got this to work by selecting smaller ranges at various places in the code, usually just before doing any cell merging. I consider this to be a workaround, not really a solution. Thanks to those who commented - you helped inspire this workaround.
I am trying to write an array formula via VBA but getting an error " unable to set the FormulaArray property of the range class". I have tried both ' Range.FormulaArray' and '.Range.selection.FormulaArray' but nothing works. Thanks for helping me with this.
Worksheets("sheet1").Range("Table2[[" & Me.cboSec.value & " Row Score Max]]").FormulaArray = formulae
This minimal snippet works for me:
Worksheets("sheet1").Range("A1:B4").FormulaArray = "=AVERAGE(C1:C5)"
This will have the cells from A1 to B4 showing the average of the numbers in cells C1 to C5, and being bound to a formula array.
But the question mentions structured references to a Table. As Rory mentioned, in a Table are no formula arrays possible. You can dig to the ground if you try to insert an array into the Table manually by <CTRL><SHIFT><ENTER>, which is the usual not programmatic way. Excel answers with: "Multi-cell array formulas are not allowed in tables."
But in order to at least fill the cells of a Table with a set of equal formulas, this minimal snippet works for me:
Worksheets("sheet1").ListObjects("Table2").DataBodyRange.Formula = "=SUM($F$1:$G$1)"
or
Worksheets("sheet1").ListObjects("Table2").DataBodyRange.Cells.Formula = "=SUM($F$1:$G$1)"
Here is one more inspiring hint: http://www.myonlinetraininghub.com/excel-2007-tables
For the Table, recording macros won't help as recording sticks to the lowest complex description necessary.
If I take a range over merge and centered cells in excel, are each of them addressed as single cells or the group of cells constituting them? For example, if the cells A1 to A10 are merged and I do a
Worksheets(1).Range("A5")
Would this return the range of a single cell among those constituting the merged cell or a range consisting the merged cell itself? And what value would it contain if I get its value?
In other words, how would you represent range over merged cells in excel VBA?
Also, how can I get the range of the non merged cells adjacent to the length of this merged cell?
In other words, how would you represent range over merged cells in excel VBA?
By addressing it with the top left cell. Ex: "A1" in this case.
When in doubt, check it yourself first. If still in doubt, search google or whatever search engine you use. Still if something is unclear, ask :)
Would this return the range of a single cell among those constituting the merged cell or a range consisting the merged cell itself? And what value would it contain if I get its value?
It would return a single cell A5 which doesn't have anything in it because when you merge cells the data from the top left cell is kept and rest discarded. The reason why I say discarded is that if you now unmerge the cells, you will not get your values back.
Best way to check:
Let's say A1 to A10 had 1,2,3..10. Merge them. The cell will now have only 1
Try this in Immediate window
?Range("A5").Value
You will get nothing. Similarly if you want to write to it, you cannot use Range("A5").Value = "Blah". You have to address it with the top left cell. For example
Range("A1").Value = "Blah"
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...)