I am trying to calculate the sum of changing cell range in vba. Unfortunately the cell values are variables. I can't seem to get the following formula to work.
Private Sub calcOverheadRate(startCell As Integer, endCell As Integer)
Total = endCell + 1
Range("D" & Total).Formula = "=SUM("D" & startCell & ":" & "D" & endCell)"
End Sub
I get compile error: "Expected: end of statement
To solve this problem I changed the function to,
Private Sub calcOverheadRate(startCell As Integer, endCell As Integer)
Dim start As String
Dim endC As String
start = "D" & CStr(startCell)
endC = "D" & CStr(endCell)
Total = endCell + 1
Range("D" & Total).Formula = "=SUM(start:endC)"
End Sub
The function compiles fine, when I run it, the value in the cell is "#NAME" where it references SUM(start:endC) not SUM(D5:D23)....
Any thoughts on how to solve this would be appreciated.
The quotes are the issue:
Range("D" & Total).Formula = "=SUM(" & startCell & ":" & endCell & ")"
I have figured out the problem the & needs to be inside the quotation for string literals
Range("D" & Total).Formula = "=SUM(" & start & ":" & endC & ")"
How about you try using a table?
Here is a 1 min video on how to make a table in Excel:
http://www.screenr.com/VvZ8
Related
I have:
Set QuanityRange = Sheets("Raw_Data").Range("F2:F" & lastDataRow)
MsgBox "This is the Data Range: " & vbNewLine & "Quanities: " & QuanityRange.Address()
This displays $F$2:$F$1838 in the message box.
Then if I have:
Total = WorksheetFunction.Sum(QuanityRange)
MsgBox "Total is: " & Total
This displays 15170 in the message box.
My problem is that I currently have:
Range("B2", Cells(lastMatrixRow, lastMatrixCol)).Formula = "=Sum(Raw_Data!$F$2:$F$10000)"
This works, it puts 15170 in each cell from B2 to BO114 which happens to be the location of (lastMatrixRow, lastMatrixCol) in this case.
However, what I want to do is:
Range("B2", Cells(lastMatrixRow, lastMatrixCol)).Formula = "=Sum(QuanityRange)"
instead of:
Range("B2", Cells(lastMatrixRow, lastMatrixCol)).Formula = "=Sum(Raw_Data!$F$2:$F$10000)"
but it doesn't work. How can I do this?
Can you try with:
Range("B2", Cells(lastMatrixRow, lastMatrixCol)).Formula = "=Sum(" & QuanityRange.Address() & ")"
If you are trying to put the address of a range variable in a formula - this is one solution:
Public Sub TestMe()
Dim varRange As Range
Set varRange = Range("F1:F5")
Range("A1", Cells(3, 3)).Formula = "=Sum(" & varRange.Address & ")"
End Sub
The idea is that the formula to the right should be "translatable" to a string.
I want to insert a vlookup into a range of cells that is defined by variables.
My problem is that the search criteria (I gave the variable the name x) in the vlookup needs to be in " ", else the vlookup doesnt work.
But if I insert those " " into the formula in any way VBA thinks I'm trying to let it take x as a value.
Does anyone know how I can solve this problem?
If there is anything else wrong with the code, please tell me too, I'm new to this.
Sub FindExchange()
n = Worksheets.Count
For k = n To 6 Step -1
Dim ws As Worksheet
Set ws = Worksheets(k)
Dim lColumn As Long
lColumn = ws.Cells(1, Columns.Count).End(xlToLeft).Column
For i = lColumn To 1 Step -4
Dim lrow As Long
lrow = ws.Cells(Rows.Count, i).End(xlUp).Row
x = Cells(1, i).Value
ws.Range(Cells(2, i + 2), Cells(lrow, i + 2)).FormulaLocal = "=vlookup(" & x & ";Sheet1!$B$2:$C$832;2;FALSE)"
Next i
Next k
End Sub
You can try this solution ,
"=vlookup(""" & x & """,Sheet1!$B$2:$C$832,2,FALSE)"
"=vlookup(" & """" & x & """" & ";Sheet1!$B$2:$C$832;2;FALSE)"
to get the double quotes " just add Chr(34).
change your FormulaLocal string to:
"=VLookup(" & chr(34) & x & chr(34) & ";Sheet1!$B$2:$C$832;2;FALSE)"
I want to create a macro that check all the cells of a column and if the first two characters of a cell is "BB" then i want the macro to extract three characters from the cell and paste it to the next column but a the corresponding row.
But my formula after the if clause is not working.
this is what i have done since:
Sub test()
Dim lmid As String
Dim srange, SelData, ExtBbFor As String
Dim lastrow As Long
Dim i, icount As Integer
lastrow = ActiveSheet.Range("B30000").End(xlUp).Row
srange = "G1:G" & lastrow
SelData = "A1:G" & lastrow
Range(srange).Formula = "=mid(E1,1,3)"
For i = 1 To lastrow
If InStr(1, LCase(Range("E" & i)), "bb") <> 0 Then
Range("G" & i).Formula = "=mid("E & i", 4, 3)"
End If
Next i
End Sub
thanks in advance
Try with below. It will work
Range("G" & i).Value = Mid(Range("E" & i), 4, 3)
If the cell is not limited to 7 then you need as below
Range("G" & i).Value = "=Mid(E" & i & ", 3, " & Len(E & "& i & ") & ")"
It will extract from the 3rd character up to the last character in a cell.
Your syntax is wrong where you're trying to concatenate strings, I think you mean to use:
Range("G" & i).Formula = "=MID(E" & i & ",4,3)"
Based on your code I think this will do the exact same thing without having to loop or declare any variables:
Sub test()
With Range("G1:G" & Cells(Rows.Count, 2).End(xlUp).Row)
.FormulaR1C1 = "=IF(UPPER(LEFT(RC[-2],2))=""BB"",MID(RC[-2],4,3),"""")"
.Value = .Value
End With
End Sub
I have the following Dims
Dim PlayersStartAt As Integer
Dim PlayersEndAt As Integer
PlayersStartAt = 101
PlayersEndAt = PlayersStartAt + 50
Selection.AutoFill Destination:=Range("B & PlayersStartAt:B & PlayersEndAt"), Type:=xlFillDefault
and want to execute the following line of code.
It with great with Range("B101:B151");
What is wrong with my syntax?
Change this
Range("B & PlayersStartAt:B & PlayersEndAt")
to
Range("B" & PlayersStartAt & ":B" & PlayersEndAt)
PlayersStartAt and PlayersEndAt are variables. Anything that you put between quotes will be taken as a string :)
I am trying to select a cell in Excel VBA 2007
Example in row 2, cells A through H have some numbers but cell B2 has the highest value. is there a formula that I could use to get the address of the cell B2 ?
Based on this, is there a way I could use a variable to select a Range(":") ?
I am a newbie to VBA so any help would be much appreciated.
Thanks
=CELL("address",INDEX(A2:H2,MATCH(MAX(A2:H2),A2:H2,0)))
EDIT.
Sub max_value_address()
Dim i As Long
i = 2
'This example assigns to A1 cell the address of max value in the range a2:h2
Range("a1").Formula = "=CELL(""Address"",INDEX(A" & i & ":H" & i & ",MATCH(MAX(A" & i & ":H" & i & "),A" & i & ":H" & i & ",0)))"
End Sub
EDIT 2.
This version is a little bit more concise.
Sub max_value_address()
Dim i As Long
Dim str As String
i = 2
str = "a" & i & ":h" & i 'assign to str a2:h2
Range("a1").Formula = "=CELL(""address"",INDEX(" & str & ",MATCH(MAX(" & str & ")," & str & ",0)))"
End Sub
The below code might help you to reach your goal. Let us know if it's unclear.
Sub GetHigherValueCellAddress()
Dim oCell As Excel.Range
Dim oRange As Excel.Range
Dim vPrevValue As Variant
Dim sAddress As String
Set oRange = Sheets(1).Range("A1:C2")
For Each oCell In oRange
If oCell.Value > vPrevValue Then
sAddress = oCell.Address
vPrevValue = oCell.Value
End If
Next oCell
MsgBox sAddress
End Sub