VBA unique formula with variable - excel

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])"

Related

Select multiple ranges with VBA

I need to select multiple ranges in a worksheet to run various VBA code on them. The ranges will always begin on row 84 but the end depends on how far down the data goes. I've been selecting these ranges separately using code like this:
Sub SelectRange()
Dim LastRow As Integer
LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Range("A84:B" & LastRow).Select
End Sub
That works fine, but I can't figure out how to select multiple ranges at once. I've tried everything I can think of:
Range("A84:B", "D84:E", "H84:J" & LastRow).Select
Range("A84:B,D84:E,H84:J" & LastRow).Select
Range("A84:B & LastRow,D84:E & LastRow,H84:J & LastRow").Select
Nothing works. I get a run-time error when running any of those.
Use UNION:
Dim rng as Range
With ActiveSheet
set rng = Union(.Range("A84:B" & LastRow),.Range("D84:E" & LastRow),.Range("H84:J" & LastRow))
End With
rng.select
But if you intend on doing something with that range then skip the .Select and just do what is wanted, ie rng.copy
Put your dis-continued range address in the first argument of Range object.
For example, Range("A:A,D:D").Select will select column A and column D.
In your case, you may try:
Dim str As String, LastRow As Integer
LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
str = "A84:B" & LastRow & ",D84:E" & LastRow & ",H84:J" & LastRow
Range(str).Select
Range("A84:B & LastRow & "," & "D84:E & LastRow & "," & "H84:J & LastRow").Select

VBA Formula that finds lact active cell and place an input 3 rows down

I have a variable data set, so I set up a Variable "LastRow" that finds the last active cell of a column.
I then have to input some data two rows down the last active cell of said column and write the number
with the following code, nothing happens after .Select
please see the formula I wrote
Dim LastRow As Long
LastRow = Cells(Rows.Count, "L").End(xlUp).Select
Range("L" & LastRow + 3).Value = "1"
2 rows down the last active cell it should have the number 1.
You do not want to use .Select in that statement.
It looks like you want to get the LastRow as a Long. Therefore, you want to get the row
With sht ' assuming you have a worksheet set as sht
LastRow = .Range("L" & .Rows.Count).End(xlUp).row
.Range("L" & LastRow + 3).Value = "1"
End With

How can I iterate through a row, while using the sum formula for the column above in a for-loop?

I would like to iterate through a row, while summarizing the value of the columns above in a formula. At this point I have data from column 'A' to column 'AV'
The code below does what I want it do, but I'd like to get it into a for-loop that runs preferably to the last column with data. So that I don't have to write 40+ lines of repetitive code.
Dim LastRow As Long
LastRow = ThisWorkbook.Worksheets("Planteleveranse trær 2019").Range("F3").End(xlDown).Row
ThisWorkbook.Worksheets("Planteleveranse trær 2019").Cells(LastRow + 1, "F").Formula = "=SUM(F3:F" & LastRow & ")"
ThisWorkbook.Worksheets("Planteleveranse trær 2019").Cells(LastRow + 1, "L").Formula = "=SUM(L3:L" & LastRow & ")"
ThisWorkbook.Worksheets("Planteleveranse trær 2019").Cells(LastRow + 1, "M").Formula = "=SUM(M3:M" & LastRow & ")"
I think my questions was poorly asked, thanks for pointing that out. The code does what I want it do to, but I wish to extend it. I need to summarize the entire row from 'F' as I have done in my third line of code all the way column 'AV', and I expect more columns of data to be added in the future. I would like this done in a for loop like For char c = 'F' to c = 'AV' ThisWorkbook.Worksheets("Planteleveranse trær 2019").Cells(LastRow + 1, "c").Formula = "=SUM(c3:L" & LastRow & ")" But I do not know the syntax for that in vba. – Glenn Bergstrøm 11 mins ago
In that case I would recommend to find the last row and the last column. Identify your range and enter the formula in that range in one go as shown below. I have commented the code but if you still have a problem understanding it, feel free to ask :)
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim LastRow As Long, LastCol As Long
Dim myFormula As String
'~~> Set this to the relevant worksheet
Set ws = Sheet1
With ws
'~~> Find last row
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
'~~> Find last column in row 1
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
'~~> Construct your formula
myFormula = "=Sum(A2:A" & LastRow & ")"
'~~> Enter formula in the entire range in 1 go
.Range(.Cells(LastRow + 1, 1), _
.Cells(LastRow + 1, LastCol)).Formula = myFormula
End With
End Sub
Screenshot:

How to enter a variable value decided by the user on vba?

I have to do a calculation on using several excel sheets and I need the user to enter the number of lines except that this number varies each year so I want this variable to be variable and entered by the user who will use the macro. Unfortunately I can't find a way to do it.
Range("B2").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[7],RIGHT(RC[-1]))"
Range("B2").Select
Selection.AutoFill Destination:=Range("B2:B95145"), Type:=xlFillDefault
Range("B2:B95145").Select
Range("J1").Select
for example, when I want to perform a calculation I want the B2:B95145 to be variable, the numbers that must be entered before the macro is launched.
You can automatically find the last used row in column A for example using
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
So this code should fill as many rows in column B as there is data in column A
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1") 'define your sheet name
Dim LastRow As Long
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
ws.Range("B2").FormulaR1C1 = "=CONCATENATE(RC[7],RIGHT(RC[-1]))"
ws.Range("B2").AutoFill Destination:=ws.Range("B2:B" & LastRow), Type:=xlFillDefault
or even just write the formula without using Autofill into all cells directly:
ws.Range("B2:B" & LastRow).FormulaR1C1 = "=CONCATENATE(RC[7],RIGHT(RC[-1]))"
If you have another formula eg in column C then just add it like
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1") 'define your sheet name
Dim LastRow As Long
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
ws.Range("B2:B" & LastRow).FormulaR1C1 = "=CONCATENATE(RC[7],RIGHT(RC[-1]))"
ws.Range("C2:C" & LastRow).FormulaR1C1 = 'your formula here

Filling columns with formula based on another Cell

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

Resources