Filling columns with formula based on another Cell - excel

I am currently sending my formula to column K & L based on a user form. It sends the formula based on Column G's input. I want to send the formula to columns K & L only if G has a value in it. Can any one help me?
picture of datasheet
'Sets Columns K & L to Method 6 Formula and looks for last populated row in the Nominal Column (G)
LastRow = Range("G" & Rows.Count).End(xlUp).Row
Range("K7").Formula = "=M7+(Q7*(1.04-EXP(0.38*(LN(P7))-0.54)))"
Range("L7").Formula = "=N7-(Q7*(1.04-EXP(0.38*(LN(P7))-0.54)))"
If LastRow = 7 Then
Else
Range("K7").AutoFill Destination:=Range("K7:K" & LastRow)
Range("L7").AutoFill Destination:=Range("L7:L" & LastRow)
End If

If you mean only run the code if G has at least one non-blank cell you could use this. You can apply the formulae across the whole range in one go.
Sub x()
Dim LastRow As Long
LastRow = Range("G" & Rows.Count).End(xlUp).Row
Range("K7:K" & LastRow).Formula = "=IF(G7="""", """",M7+(Q7*(1.04-EXP(0.38*(LN(P7))-0.54))))"
Range("L7:L" & LastRow).Formula = "=IF(G7="""", """",N7-(Q7*(1.04-EXP(0.38*(LN(P7))-0.54))))"
End Sub

Related

VBA unique formula with variable

I'm having a really hard time setting the correct formula in VBA to output a list of unique values in a list.
Dim lastRow As Long
With ActiveSheet
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
Range("B1").Select
ActiveCell.Formula2R1C1 = "=UNIQUE(RC[-1]:" & lastRow & ")"
Pretty simple what I'm trying to do. Just use the range from cell A1 to the last row in the that column and push it to the unique formula.
A1 notation:
Range("B1").Formula2 = "=UNIQUE(A1:A" & lastRow & ")"
R1C1 notation:
Range("B1").Formula2R1C1 = "=UNIQUE(RC[-1]:R[" & lastRow & "]C[-1])"

for loop over sheets in a workbook with formulas until the end of a column with data

I have have the following problem with vba and would like to ask you for help.
I hope I haven't overseen a similar question in the froum
I have several sheets in an excel workbook. On each sheet I would like to run the same formulas. The formula gets its data from the cell C3 on the same sheet and should run until the end of the data in column C xxx . The length of the data is different on each sheet. Each sheet has its own data set.
The code I have written works fine in the sense that it works itself through through the sheets starting from sheet 2 until the end of the sheets.
The code gets executed via an icon in the toolbar.
On each sheet I have values what the formulas should use. The values in column C are different long. Some have for example just 15 some have over 300000.
For example, if I press the icon when I'm on sheet 1 with no data in column C the macro takes the first 15 values/range of the column for the formulas in all sheets. Which means I miss all values from for example at sheet 3 with 300000 values. If I am on sheet 3 and press the icon there I takes the 300000 values/range and uses the range of the 300000 values in all the other sheets.
If I run the code without the loop on each sheet it works fine. It selects the right range with values of the column.
There are no empty cells in the column.
Has anyone an idea what is wrong in the code that it does not select the real amount of values in column C for the different sheets?
I work on a mac with excel 14.
The following code is what I have so far:
Sub Start()
Dim i As Long
lastrow = Cells(Rows.Count, 3).End(xlUp).Row
For i = 2 To Worksheets.Count
With Sheets(i)
.Range("K11").Formula = "=COUNT(C3:C" & lastrow & ")"
.Range("K12").Formula = "=MEDIAN(C3:C" & lastrow & ")"
.Range("K13").Formula = "=AVERAGE(C3:C" & lastrow & ")"
.Range("K14").Formula = "=MIN(C3:C" & lastrow & ")"
.Range("K15").Formula = "=MAX(C3:C" & lastrow & ")"
.Range("K16").Formula = "=STDEVP(C3:C" & lastrow & ")"
End With
Next i
End Sub
You have to use .Cells to make it refer to the object in the With statement, otherwise it refers to the current sheet:
Sub Start()
Dim i As Long
For i = 2 To Worksheets.Count
With Sheets(i)
lastrow = .Cells(Rows.Count, 3).End(xlUp).Row
Debug.Print (i)
Debug.Print (lastrow)
.Range("K11").Formula = "=COUNT(C3:C" & lastrow & ")"
.Range("K12").Formula = "=MEDIAN(C3:C" & lastrow & ")"
.Range("K13").Formula = "=AVERAGE(C3:C" & lastrow & ")"
.Range("K14").Formula = "=MIN(C3:C" & lastrow & ")"
.Range("K15").Formula = "=MAX(C3:C" & lastrow & ")"
.Range("K16").Formula = "=STDEVP(C3:C" & lastrow & ")"
End With
Next i
End Sub

How do I move specific valued cells in VBA?

I am working with a dataset that contains both numbers and names. In the dataset, some numbers and names are displayed and instead of manually going through thousands of rows I tried to make a script but it doesn´t happen anything.
Here is the code:
Sub MoveCells()
Dim row As Long
For row = 2 To LastRow
If Range("C" & row).Value Like "*0*" Then
Dim i As Integer
For i = 1 To 2
Range("C" & row).Insert Shift:=xlToRight
Next
End If
Next
End Sub
I am trying to move the cell that has a 0 in it, and the cell to the right of it, one step to right.
E.g. Cells C4 & D4 to D4 & E4.
I've made some adjustments to your code which will acheive the outcome you described.
Private Sub MoveCells()
Dim TargetRow As Long
Dim LastRow As Long
Dim ColumnCValue As Variant
Dim ColumnDValue As Variant
With Sheets("Sheet1")
LastRow = .Cells(.Rows.Count, 3).End(xlUp).row
End With
For TargetRow = 2 To LastRow
If Sheets("Sheet1").Range("C" & TargetRow).Value Like "*0*" Then
ColumnCValue = Sheets("Sheet1").Range("C" & TargetRow).Value
ColumnDValue = Sheets("Sheet1").Range("D" & TargetRow).Value
Sheets("Sheet1").Range("D" & TargetRow).Value = ColumnCValue
Sheets("Sheet1").Range("E" & TargetRow).Value = ColumnDValue
Sheets("Sheet1").Range("C" & TargetRow).ClearContents
End If
Next
End Sub
Now we first assign a value to for LastRow and when the If...Then statement is true, assign the values of Column C and Column D to the respective variables. Then, write those values 1 row to the right and finally clear the contents from Column C.

How to fill in from dynamic last row in one column to the last row in adjacent column?

I have been trying some time now with the following problem: First code (not listed here) drops data in sheet EDD in column B (B45 to be precise), then each cell in col A is populated with number 1 (from A45 to the bottom of column B) - as the code below shows.
Now the problem is that I will be adding another set of data in column B (to the bottom of what has already been added) but this time this new data will have number 2 in each cell of the column A (ps. I cannot overwrite number 1's that I have already entered) - the issue is that the data is dynamic. I do not know how to identify the last row in column A (populated it with value = 2 and autofill it to the bottom of this new data in column B).
Dim EDDx As Worksheet
Dim lastrow As Long
Set EDDx = Sheets("EDD")
lastrow = EDDx.Cells(Rows.Count, "C").End(xlUp).Row
With EDDx
.Range("B45:B" & lastrow).Value = 1
End With
End Sub
Thanks
Try,
with workSheets("EDD")
.range(.cells(.rows.count, "B").end(xlup), _
.cells(.rows.count, "C").end(xlup).offset(0, -1)).filldown
end with
Try:
Option Explicit
Sub Test()
Dim LastrowC As Long
Dim LastrowB As Long
With wsTest
LastrowB = .Range("B" & Rows.Count).End(xlUp).Row
LastrowC = .Range("C" & Rows.Count).End(xlUp).Row
.Range("B" & Lastrow + 1 & ":C" & LastrowC).Value = "2"
End With
End Sub

VBA to use sum formula for cells in same row. Row number will change with each loop

I'm using VBA to extract numbers from a website using a loop. I have these numbers imported into column F and G. I'm trying to include a line in my VBA that would allow me to use the sum formula to add these two cells and post the result in column E.
The numbers will always be in the same row, but in columns F and G.
Can i use the (ActiveCell.Row) function in this way?
or should just use an If function at the end of my loop.
Range("E" & (ActiveCell.Row)).Formula = "=SUM("Range("F" & (ActiveCell.Row)) & Range("G" & (ActiveCell.Row))")"
I have these numbers imported into column F and G.
If you want to insert the formula in Col E then use this. This will enter the formula in all the relevant cells in Col E in one go.
See this example
Sub Sample()
Dim lRow As Long
Dim ws As Worksheet
'~~> Change this to the releavnt sheet name
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> Find Last row in col E which has data
lRow = .Range("E" & .Rows.Count).End(xlUp).Row
'~~> This will put the formula from E1 to E last row
.Range("E1:E" & lRow).Formula = "=SUM(F1+G1)"
End With
End Sub
In the forumla dont use Range() as it will not return a String. And also, you forgot the operator, you need to add "+" or ":" into the Forumla:
Range("E" & (ActiveCell.Row)).Formula = "=SUM(F" & ActiveCell.Row & "+" & "G" & ActiveCell.Row & ")"
Otherwise you'll need to access the Address method on the Ranges in the forumla (which is a waste because you have entered the address anyway)
but for clarity the code below demonstrates what I mean:
Range("E" & (ActiveCell.Row)).Formula = "=SUM(" & Range("F" & (ActiveCell.Row)).Address(False,False) & ":" & Range("G" & (ActiveCell.Row)).Address(False,False) & ")"

Resources