VBA Autofill formula with a fixed cell reference - vba

I am trying to autofill a formula to a range using vba.
What i am trying to do is to calculate a percentage contribution for each item.
Say i have 2 entries
A1= 4
A2= 6
A3= sum of A1 and A2 =10
What i want is
B1= A1/$A$3 = 40%
B2= A2/$A$3 = 60%
but i am not quite sure how i could locate the last row of A and code it into the
Range("B1").Formula= "=A1/?????"
any ideas would help!
Thanks

Please try this..
Range("B1").FormulaR1C1 = "=RC[-1]/INDEX(C[-1],MATCH(1E+99,C[-1]))"
You need to mention the range with circular references by specifying Row and column no from the active cell respectively like RC[-1]/R3C1

Find the last cell first, then sum it and add the formula to column B
Sub Button1_Click()
Dim LstRw As Long, rng As Range, Sm As Range
LstRw = Cells(Rows.Count, "A").End(xlUp).Row
Set rng = Range("A1:A" & LstRw)
Set Sm = Cells(LstRw + 1, 1)
Sm = Application.Sum(rng)
rng.Offset(, 1) = "=a1/" & Sm.Address
End Sub

Related

Excel Vba: Need help to drag formula

I need help placing a formula in row 51 from column A to AE on sheet "COPY". The formula is "Trim(A1)" and needs to be dragged until "Trim(AE1)" while still being in row 51 (A51:AE51)
This is what I have so far, but its pulling up an error on "lascolumn = range..."
Sub INSERT_TRIM_COPY()
Sheets("COPY").Select
Dim Lastcolumn As Long
Lastcolumn = Range("A:AE" & Columns.Count).End(xlToRight).Column
Range("A51:AE51" & Lastcolumn).FORMULA = "=TRIM(A1)"
End Sub
You need to use: Range(Cells(51,1), Cells(51,Lastcolumn).Formula = "=Trim(A1) Because your lastcolumn is variable is numeric you need to use the cells function in the range. The first number is for row number and the second is for the column.
I believe the following will do what you expect it to, the code you used to get the Last Column wasn't right:
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("COPY")
LastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
'get the last Column on Row 1 with data
ws.Range(ws.Cells(51, 1), ws.Cells(51, LastCol)).Formula = "=Trim(A1)"
'add formula from column A to the last Column
End Sub

Loop Through Dynamic Filtered List

how would I loop through visible rows in a filtered list? I have a cell in the first filtered row in let's say column B that is a "Y". I need to be able to change all non-hidden cells in a specific column to be Y. This needs to be dynamic too because the column B range is going to be different everyday.
Essentially, I Need to modify this code:
Range("B2").Select --Where B2 is "Y"
Selection.AutoFill Destination:=Range(*This is where I am unsure*)
Not sure what exactly you are trying to achieve here with Autofill, but this will give you an idea about how to deal with filtered cells...
Dim ws As Worksheet
Dim lr As Long
Dim FillWith As String
Set ws = ActiveSheet 'Change it as per your requirement
lr = ws.UsedRange.Rows.Count
FillWith = ws.Range("B2").Value 'Change it as per your requirement
If ws.FilterMode Then
If ws.Range("B1:B" & lr).SpecialCells(xlCellTypeVisible).Cells.Count > 1 Then
ws.Range("B2:B" & lr).SpecialCells(xlCellTypeVisible).Value = FillWith
End If
End If
You can refer to visible cells only using SpecialCells
Range("A2:A100").SpecialCells (xlCellTypeVisible)

Double if statement then highlight if value <> to % of

What im trying to do is: If a cell on Column D says cars then check the last cell on the right (last cell on the row) and if the value is less than 20% then highlight. (which is the last part of the code):
This is what i have so far but i cant figure out how to solve it. I have to do this for work but i'm confused thanks!
Worksheets("Report").Activate
Call VBA
Dim lrow As Integer
Dim xrow As Integer
Dim FR As Range
Dim MR As Range
Dim cell As Range
lrow = wsr.Cells(Rows.Count, 3).End(xlUp).Row
xrow = wsr.Cells(Rows.Count, 3).End(xlToRight)
Set FR = Range("D12:D" & lrow)
For Each cell In FR
If cell.Value = "Cars" then for cell.xrow if cell.Value > 0.20 then Then cell.Interior.Color = RGB(224, 202, 224)
By Xrow i mean last cell on the row.
Thanks !
To avoid vba you can use Conditional Formatting.
The conditional formatting formula would then be:
=AND($D12="Cars",INDEX(12:12,MATCH(1E+99,12:12))>0.2)
The INDEX(12:12,MATCH(1E+99,12:12)) part is what finds the last column in that row that has a number. It then test whether it is greater than .2
You can apply it to all rows and columns desired to format starting with row 12, I only applied it to D12:D28:
Try this:
For Each cell In FR
If cell.Value = "Cars" Then
If Cells(cell.Row, Columns.Count).End(xlToLeft).Value < 0.2 Then
cell.Interior.Color = vbRed
End If
End If
Next
However Scott Craner's answer is a much more elegant solution.

How to write an "If(And" code with unknown number of cells in column?

Is there a way to check if all cells in a column are less than 1? If there were only a few cells, with the number of cells known up front, I would use the code below.
However, from case to case the number of cells in column A will vary. I need to know if any of the cells in column A is less than 1.
If there is one (or more) cell containing a value less than 1, I need a cell (A1 for example) to show NOT OK. If only ALL the cells' values are greater than 1, I need the cell (A1 for example) to show OK.
If all cells in column A have values greater than 1, I want to continue and check column B for the same thing. Otherwise I want to save and close the workbook and continue with next open workbook...also with vba code.
Any suggestions on how to write this in VBA? Maybe there is way other than If(AND...)?
Sub IfAnd()
IF(AND(A5>1,A4>1,A3>1,A2>1),"OK", "NOT OK")
End Sub
This code will solve all your columns and insert the data in THE FIRST ROW OF EACH COLUMN
Sub Problems()
Dim CurCol, LastRow, LastCol as Long
LastCol = Cells(2, Columns.Count).End(xlToLeft).Column
For CurCol = 1 to LastCol
LastRow = Cells(Rows.Count, CurCol).End(xlUp).Row
If WorksheetFunction.Min(Range(Cells(2, CurCol), Cells(LastRow, CurCol))) < 1 Then
Cells(1, CurCol).Value = "NOT OK"
Else
Cells(1, CurCol).Value = "OK"
End If
Next CurCol
End Sub
Here is a way of doing it without any worksheet functions.
Sub test()
Dim ws As Worksheet
Dim ce As Range
Dim sr, lr, lc As Integer
'worksheet you are working with
Set ws = ThisWorkbook.Sheets(1)
'column you are searching
Set ce = ws.Cells(ws.Rows.Count, 1)
'start row set to 2 so row 1 will contain output
Let sr = 2
'search only the last row
Let lr = ce.End(xlUp).Row
Let lc = ws.Cells(sr, ws.Columns.Count).End(xlToLeft).Column
For c = 1 To lc
For r = sr To lr
If ws.Cells(r, c).Value < 1 Then
ws.Cells(1, c).Value = "NOT OK"
GoTo NotOK
End If
Next r
ws.Cells(1, c).Value = "OK"
NotOK:
Set ce = ws.Cells(ws.Rows.Count, c+1)
Let lr = ce.End(xlUp).Row
Next c
End Sub
This should be faster and more efficient for large data sets. Especially if it is sorted smallest to largest.
Here you are:
=IF(MAX(A:A)<1)
If VBA is not required, here is a worksheet formula that should do the job, and will also ignore blanks and non-numeric entries:
This formula must be array-entered:
=IF(ISNUMBER(MATCH(TRUE,IF(ISNUMBER($A:$A),$A:$A)<1,0)),"NOT OK","OK")
If this formula must be located in A1, change the range references from $A:$A to $A$2:$A$1000 where 1000 represents the highest conceivable row number for the data.
To array-enter a formula, after entering
the formula into the cell or formula bar, hold down
< ctrl-shift > while hitting < enter >. If you did this
correctly, Excel will place braces {...} around the formula.

Change a cell's format to boldface if the value is over 500

I am using Excel 2010 and trying to add a bunch of rows placing the sum of columns A and B in column C. If the sum is over 500 I would then like to boldface the number in column C. My code below works works mathematically but will not do the bold formatting. Can someone tell me what I am doing wrong? Thank you.
Public Sub addMyRows()
Dim row As Integer 'creates a variable called 'row'
row = 2 'sets row to 2 b/c first row is a title
Do
Cells(row, 3).Formula = "=A" & row & "+B" & row 'the 3 stands for column C.
If ActiveCell.Value > 500 Then Selection.Font.Bold = True
row = row + 1
'loops until it encounters an empty row
Loop Until Len(Cells(row, 1)) = 0
End Sub
Pure VBA approach:
Public Sub AddMyRows()
Dim LRow As Long
Dim Rng As Range, Cell As Range
LRow = Range("A" & Rows.Count).End(xlUp).Row
Set Rng = Range("C2:C" & LRow)
Rng.Formula = "=A2+B2"
For Each Cell In Rng
Cell.Font.Bold = (Cell.Value > 500)
Next Cell
End Sub
Screenshot:
An alternative is conditional formatting.
Hope this helps.
Note: The formula in the block has been edited to reflect #simoco's comment regarding a re-run of the code. This makes the code safer for the times when you need to re-run it. :)