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 :)
Related
I'm writing a routine to insert some rows, and to copy over some values.
The amount and position of the rows that need to be inserted are dynamic and stored in variables.
I know I can insert rows by using Activesheet.rows("2:12").Insert shift:=xlDown
Now as the 2 and 12 are dynamic I declared variables of datatype Long to hold those values. For example:
Dim Startvalue as Long
Startvalue = 2
Dim Lengthofinsert as Long
Lengthofinsert = 10
Dim Endvalue as Long
Endvalue = Startvalue + Lengthofinsert
Then I wrote the following: Activesheet.rows("""" & Startvalue & ":" & Endvalue & """").Insert shift:=xlDown which results in a type mismatch.
Using debug.print("""" & Startvalue & ":" & Endvalue & """") the result of this is "2:12", exactly the same as I entered in my first insert() statement above.
I thought perhaps first storing the string as a string variable might help, but that unfortunately also results in the same type mismatch.
Now I figure rows should take some sort of Array or Range as argument, but how do I convert my two Long's into that?
just use
ActiveSheet.Rows(Startvalue & ":" & Endvalue).Insert shift:=xlDown
You don't need the "" because Startvalue & ":" & Endvalue is already a string (the long values automatically cast into a string due to the concatenation with &).
Same like
Dim MyRows As String
MyRows = Startvalue & ":" & Endvalue
ActiveSheet.Rows(MyRows).Insert shift:=xlDown
Or as #Chronocidal pointed out in the comment:
Compare the outputs
Debug.print("""" & Startvalue & ":" & Endvalue & """") 'output: "2:12"
Debug.Print("2:12") 'output: 2:12
Have you created Thisworksheet? It's not mentioned in your declarations.
Assuming you have,
Try
ThisWorkSheet.rows(Startvalue & ":" & Endvalue).Insert shift:=xlDown
Otherwise try:
Activesheet.rows(Startvalue & ":" & Endvalue).Insert shift:=xlDown
I have the goal to write a formula in a set of rows. Some references in the formula have to change each row.
I implemented the following script:
Dim i As Integer
Dim formcolM As String
Dim temprng As String
For i = 0 To 100
formcolM = "NUMBERVALUE(IF(Q" & i & "=""Bedarf kum."";A" & i & ";IF(Q" & i & "=""Ist"";OFFSET(A" & i & ";-1;0);IF(Q" & i & "=""Lz."";OFFSET(A" & i & ";-2;0);IF(Q" & i & "=""Ist+Lz.-Bedarf"";OFFSET(A" & i & ";-3;0);)))))"
Let temprng = "M" & i
Range(temprng).Select
ActiveCell.Value = "\=" & formcolM
next i
With this script I am writing a string each row in my excel table at column M.
I noticed that if the formula hasn't the symbol "\" , you can find an error .
In order to avoid the error I thought to leave the symbol "\" and to use a trick deleting it after (because I don't know how to solve with R1C1 formula. I read some answers on Stackoverflow, but unfortunately I did not understand )
The replacing script after the for cycle:
Columns("M:M").Replace What:="\=", Replacement:="=", LookAt:=xlPart
The strange thing is that the macro doesn't delete it.
Infact when the script finishes , it seems that nothing happened, without errors. But if I want substitute "\=" with another symbol, for example "*", the replacing script works.
I did not understand if the problem is :
the replace method did not recognized the symbol "=" to search
I cannot use the replace method because the symbol "=" disturbs in some way , I don't know in what.
OR, is there another simplest way to get this task done?
Someone could help me in order to fix? I should have the formula working in the column M , automatically with vba (not with another formula in the excel sheet) .
Thanks in advance for your time.
We can apply the formula directly. The issue is that vba is very US-EN Centric and all formula when using the .Formula needs to be in that format.
Also since your formula refers to values in a row 3 above the one in which it is put we need to start the loop at 4 not 0. There is no row 0
There are two ways, in US-En format with English functions and , as the deliminator using .Formula:
Dim i As Integer
For i = 4 To 100
Range("M" & i).Formula = "=NUMBERVALUE(IF(Q" & i & "=""Bedarf kum."",A" & i & ",IF(Q" & i & "=""Ist"",OFFSET(A" & i & ",-1,0),IF(Q" & i & "=""Lz."",OFFSET(A" & i & ",-2,0),IF(Q" & i & "=""Ist+Lz.-Bedarf"",OFFSET(A" & i & ",-3,0),)))))"
Next i
Or using .FormulaLocal and the formula as you would write it in your native tongue.
Dim i As Integer
For i = 4 To 100
Range("M" & i).FormulaLocal = "=NUMERO.VALORE(SE(Q" & i & "=""Bedarf kum."";A" & i & ";SE(Q" & i & "=""Ist"";SCARTO(A" & i & ";-1;0);SE(Q" & i & "=""Lz."";SCARTO(A" & i & ";-2;0);SE(Q" & i & "=""Ist+Lz.-Bedarf"";SCARTO(A" & i & ";-3;0);)))))"
Next i
By the time I got this worked out, Scott already had an answer. I just wanted to post your original code modified to work. I would suggest his method.
Sub TestScript()
Dim i As Integer
Dim formcolM As String
Dim temprng As String
For i = 4 To 100
formcolM = "NUMBERVALUE(IF(Q" & i & "=" & "Bedarf kum." & ";A" & i & ";IF(Q" & i & "=" & "Ist" & ";OFFSET(A" & i & ";-1;0);IF(Q" & i & "=" & "Lz." & ";OFFSET(A" & i & ";-2;0);IF(Q" & i & "=" & "Ist+Lz.-Bedarf" & ";OFFSET(A" & i & ";-3;0);)))))"
temprng = "M" & i
Sheets("Sheet1").Range(temprng).Select
ActiveCell.Value = " = " & formcolM
Next i
End Sub
I have this code:
LastLine = Range("C" & Rows.Count).End(xlUp).Row
LastLine = "C" & LastLine
Range(LastLine).Select
ActiveCell.FormulaR1C1 = "=SUM(R4C3:R[-1]C)"
Range("E13").FormulaR1C1 = "=if(R12C5 - " & LastLine & " <> 0,R12C5 - " & LastLine & ","""")"
everything works expect for the formula the outcome for that is
"=IF($E$12 - $Z:$Z <> 0,$E$12 - $Z:$Z,"")"
Can anyone see what I'm doing wrong? also when I try
LastLine = Range(LastLine).address
It gives me an error
The variable LastLine is not typed and you change its type throughout your code. First, it is a number (the number of the row), then you assign it text, the letter C combined with the row number, for example "C3". Next you use that in an R1C1 reference, where C3 means Column 3, so the row number from the first LastLine assignment will end up as the column in the formula.
Your code would be cleaner if your variables were dimmed to specific types and you would not mix A1 notation with R1C1.
Sub test()
Dim LastLine As Long
Dim LastLineCell As String
LastLine = Range("C" & Rows.Count).End(xlUp).Row
LastLineCell = "C" & LastLine
Range(LastLineCell).Select
ActiveCell.FormulaR1C1 = "=SUM(R4C3:R[-1]C)"
Range("E13").FormulaR1C1 = "=if(R12C5 - R" & LastLine & "C26 <> 0,R12C5 - R" & LastLine & "C26,"""")"
End Sub
Since you are using R1C1 notation you have to translate CsomeRow to RsomeRowC3
Range("E13").FormulaR1C1 = "=if(R12C5 - R" & LastLine & "C3 <> 0,R12C5 - R" & LastLine & "C3,"""")"
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
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