I want to add two variable into my vba's range function.
For exemple I would like to set a range like this but I don't know if it is possible :
Range("BY" & FirstVariable &":BY" & SecondVariable)
In order to use WorksheetFunction. Median Method :
Application.Median(Range("BY" & FirstVariable &":BY" & SecondVariable))
I tried this :
Range("BY" & ActiveCell.Row - CptM & ":BY" & ActiveCell.Row)
Where CptM is a Number
But I got the error : "Execution code 91: Object Variable Or Bloc Variable With not defined.
What i want to do :
I am using a For Loop to Browse J Column and to check Value Of J Cells
For Cpt = 4 To Cells(Rows.Count, 10).End(xlUp).Row
MyString= Cells(Cpt - 1, 10).Value
If Cells(Cpt, 10).Value = MyString Then
CptM = CptM + 1
End If
If Cells(Cpt, 10).Value <> Metier Then
MyVar= Application.Median(Range("BY" & ActiveCell.Row - CptM & ":BY" & ActiveCell.Row))
Cells(Cpt, 81).Value = MyVar
CptM = 0
End If
Next Cpt
There i except to get the Median of the range R1 , R2 ect ...
Thanks for your time and consideration.
This will work:
Application.Median(Range("BY" & ActiveCell.Row - CptM & ":BY" & ActiveCell.Row))
You can put this as a value in any variable.
The problem was due to ActiveCell.Row because i didn't have Active Cells.
To handle this i just used Cpt in stead of ActiveCell.Row
The full line code is :
Application.Median(Range("BY" & Cpt - CptM & ":BY" & Cpt))
Related
I have a short code about inserting a formula to the cell instead of obtaining values but when I tried to use that with VLOOKUP and/or IFERROR, I get error 1004. I did it with SUB, SUBTOTAL before but couldn't achieved with these functions. If you may help, that would be amazing.
GerçekStok = 0
Set StokBook = ActiveWorkbook
Set BticinoBook = Workbooks("BTICINO.xlsm")
NoS = BticinoBook.Sheets.Count
For k = 1 To NoS
If BticinoBook.Sheets(k).Name = "DRAFT" Then
DNumber = k
Exit For
End If
Next k
TotalRow = BticinoBook.Sheets(DNumber).Cells(Rows.Count, 4).End(xlUp).Row
If GerçekStok = 0 Then
For i = 2 To TotalRow
BticinoBook.Sheets(DNumber).Cells(i, 2) = "=IFERROR(VLOOKUP(D" & i & ";'[" & StokBook.Name & "]Sheet2'!$A:$E;3;FALSE);" & Chr(34) & Chr(48) & Chr(34) & Chr(41)
Next i
Else
For i = 2 TotalRow
BticinoBook.Sheets(DNumber).Cells(i, 2) = "=IFERROR(VLOOKUP(D" & i & ";'[" & StokBook.Name & "]Sheet2'!$A:$E;4;FALSE);" & Chr(34) & Chr(48) & Chr(34) & Chr(41)
Next i
End If
You need to use .Formula to enter a formula into a cell.
Range("B10").Formula = "=SUM(B4:B9)"
If your string contains another string with double quotes, then you need to type the double quote twice, like this:
... whatever... ;FALSE);""0"")"
Using Chr() instead of the typed characters doesn't really fix the issue.
BticinoBook.Sheets(DNumber).Cells(i, 2).formula = "=IFERROR(VLOOKUP(D" & i & ";'[" & StokBook.Name & "]Sheet2'!$A:$E;3;FALSE);""0"")"
Of course, there is no reason to put a zero inside of double quotes, so you could simply use
BticinoBook.Sheets(DNumber).Cells(i, 2).formula = "=IFERROR(VLOOKUP(D" & i & ";'[" & StokBook.Name & "]Sheet2'!$A:$E;3;FALSE);0)"
I have an Excel sheet in which I want to add an Conditional Formula by VBA. While I'm trying to do so, Excel is throwing "Invalid procedure call or argument" and I can't find why.
The problem is with exactly this line:
Set cf = shG.Range("E" & (i - 3) & ":AI" & (i - 2)).FormatConditions.Add(type:=xlExpression, Formula1:="=OR(ISNUMBER(SEARCH(""holiday"",E$" & i & ")),ISNUMBER(SEARCH(""l4"",E$" & i & ")))")
What it should do?
It should change font color to white for 1st and 2nd rows in case if 4th row will contain "holiday" or "l4"
Full Sub:
Sub AddCondForm(shG As Worksheet)
Dim r As Range
Dim cf
Set r = shRoles.Cells(2, 1)
Do
For i = 8 To 204 Step 4
If Not IsEmpty(shRoles.Cells(r.row, 2)) Then
Set cf = shG.Range("E" & i & ":AI" & i).FormatConditions.Add(type:=xlTextString, String:=r.Value, _
TextOperator:=xlBeginsWith)
Else
Set cf = shG.Range("E" & i & ":AI" & i).FormatConditions.Add(type:=xlCellValue, Operator:=xlEqual, _
Formula1:=r.Value)
End If
cf.Interior.Color = r.Next.Next.Next.Interior.Color
cf.Font.Color = r.Next.Next.Next.Font.Color
cf.SetFirstPriority
'HERE IS PROBLEM
Set cf = shG.Range("E" & (i - 3) & ":AI" & (i - 2)).FormatConditions.Add(type:=xlExpression, Formula1:="=OR(ISNUMBER(SEARCH(""holiday"",E$" & i & ")),ISNUMBER(SEARCH(""l4"",E$" & i & ")))")
'END OF PROBLEM
cf.Font.ColorIndex = 2
'Coloring C Column
Set cf = shG.Range("C" & (i - 3) & ":C" & i).FormatConditions.Add(type:=xlExpression, Formula1:="=$A$" & i - 3 & "=1")
cf.Interior.ThemeColor = xlThemeColorAccent6
Set cf = shG.Range("C" & (i - 3) & ":C" & i).FormatConditions.Add(type:=xlExpression, Formula1:="=$A$" & i - 3 & "=3/4")
cf.Interior.ThemeColor = xlThemeColorAccent6
cf.Interior.TintAndShade = 0.2
Set cf = shG.Range("C" & (i - 3) & ":C" & i).FormatConditions.Add(type:=xlExpression, Formula1:="=$A$" & i - 3 & "=1/2")
cf.Interior.ThemeColor = xlThemeColorAccent6
cf.Interior.TintAndShade = 0.4
Set cf = shG.Range("C" & (i - 3) & ":C" & i).FormatConditions.Add(type:=xlExpression, Formula1:="=$A$" & i - 3 & "=1/4")
cf.Interior.ThemeColor = xlThemeColorAccent6
cf.Interior.TintAndShade = 0.6
Next
Set r = shRoles.Cells(r.row + 1, 1)
Loop Until IsEmpty(r.Value)
End Sub
PS. I checked the formula itself and it seems working properly. Other lines are also working well. Only this one and I don't know why :'(
If you're not English user, this may be important for you.
In case of FormatCondition.Add you need to add formula exactly the same as if you were entering it manually. So if separator in your language is not a comma, like in Poland (we have semicolon) you would have to use that separator and translate your formulas to that language.
In my case it means I had to change formula to
=LUB(CZY.LICZBA(SZUKAJ.TEKST(""holiday"";E$" & i & "));CZY.LICZBA(SZUKAJ.TEKST(""l4"";E$" & i & ")))"
I have a cell in that contains the following:
=$H$10+1&","&B5+I10&","&(2*$D$2+$E$2)/2
The result of this formula is in this format:
14649,28.25,5.5
I want to use the formula VBA code. I want the number 1 in the $H$10+**1**&" to be the i of the for loop and the I10 in "&B5+I10&" to also change with the loop.
For i=1 to lastrow
.Range("X" & 13+i & "").Formula = "=$H$10+" & i & "" & "," & "B5+I" & i + 10 & "" & "," & "(2*$D$2+$E$2)/2"
Next i
Here code that can be used:
Sub mySub()
'=$H$10+1&","&B5+I10&","&(2*$D$2+$E$2)/2
Dim myRange As Range
Set myRange = Range("H:H").SpecialCells(xlCellTypeLastCell)
Dim myStr As String
'A=10 is for assigning first row in H as your data, hope no data upward from row 9 to row 1
For Baris = 10 To myRange.Row
On Error Resume Next
myStr = "=$H$10+" & Baris - 9 & "&"",""&B5+I" & Baris & "&"",""&(2*$D$2+$E$2)/2"
Range("K" & Baris).Formula = myStr
Next
End Sub
I have the following IF statement in VBA, but the "delete" part of the statement, or the bit before the ElseIf, is completely ignored:
For i = 3 To (customerNum + 3)
If Sheets("Reports").Range("L" & i).Value = "A" Then
Sheets("Reports").Range("G" & i & ":M" & i).Select
Selection.Delete Shift:=xlUp
ElseIf Sheets("Reports").Range("L" & i).Value <> "A" Then
Sheets("Reports").Range("J" & i).Value = form1_1 & i & form1_2
Sheets("Reports").Range("J" & i).NumberFormat = "d-mmm-yy"
Sheets("Reports").Range("M" & i).Value = form2_1 & i & form2_2 & i
Sheets("Reports").Range("M" & i).NumberFormat = "_-[$£-809]* #,##0.00_-;-[$£-809]* #,##0.00_-;_-[$£-809]* ""-""??_-;_-#_-"
End If
Next i
The rest runs smoothly. No crashes or bugs.
Any ideas?
This is not (!) supposed to be an additional answer. Yet, I would like to add this "answer" because (occasionally) I encountered the problem of various different "A"s or "C"s (etc.) in one Excel sheet. Working in large corporations I had colleagues working with multiple different character sets at a time (their native character set and the English one). Note that the cyrillic "A" looks like the latin "A". Yet, even with Option Compare Text the two are different. The following code will reveal the unicode of a character and might be a better way of checking if an "A" has been entered into a particular cell:
Public Sub CheckUnicode()
Dim lngRowNumber As Long
Debug.Print AscW("A")
With Sheets("Reports")
For lngRowNumber = 1 To 100000
If Trim(.Range("L" & lngRowNumber).Value2) = vbNullString Then Exit For
Debug.Print AscW(Left(.Range("L" & lngRowNumber).Value2, 1))
Next lngRowNumber
' The following will append a cyrillic a to the bottom of the sheet in Column "A"
.Range("A" & lngRowNumber + 1).Value2 = ChrW(1072)
End With
End Sub
I am merely addition this "solution" for others who might encounter a similar problem. As mentioned before there are several characters which look alike but are not. Also note, that this is only the cyrillic alphabet. There might be other character sets out there which seem to look alike.
Maybe you need to start from the bottom then go up.
Sub Button1_Click()
Dim sh As Worksheet
Dim Rws As Long, rng As Range, nwrw As Long
Set sh = Sheets("Reports")
With sh
Rws = .Cells(.Rows.Count, "L").End(xlUp).Row
For i = Rws To 3 Step -1
If .Cells(i, "L") = "A" Then .Range(.Cells(i, "G"), .Cells(i, "M")).Delete Shift:=xlUp
If .Cells(i, "L") <> "A" Then
.Cells(i, "J") = "something" & i & "something else"
.Cells(i, "M") = "something" & i & "something else"
End If
Next i
End With
End Sub
In my worksheet some cells values are based on other cells
Info worksheet
A1: 5
B1: =A1
Design worksheet
A1:
Is there a way to copy and read the value in B1? I'm trying to use the value in a for loop, with no luck.
Sheets("Info").Select
For i = 1 to 5
If Range("B" & i).Value <> 0 Then
Range("B" & i).Copy Destination:=Sheets("Design").Range("A" & x)
'Sheets("Design").Range("A" & x).Value = Sheets("Offerte").Range("B" & i).Value
x = x + 1
End If
Next i
Your example doesn't seem to match the code well. The line
If Range("B" & i).Value = 1 Then
means that nothing will be copied in your example. It's looking for a cell with 1 in it. Why do you need that If statement at all?
EDIT I am guessing you're just checking that there's something in the cell to copy? I would probably do it this way:
If Trim(Range("B" & i).Value) <> "" Then
Also - did you miss out setting x=1?
There is more than one way to do it. One of them is using 'offset', which is a function that really worth understand. It basically points to a amount of rows / columns from the original cell.
Sub test()
Dim oCell As Excel.Range
Dim i As Integer
Dim x As Integer
Set oCell = Sheets("Info").Range("B1")
x = 1
For i = 1 To 5
If oCell.Offset(i, 0).Value = 1 Then
oCell.Offset(i, 0).Copy Destination:=Sheets("Design").Range("A" & x)
x = x + 1
End If
Next i
End Sub
Besides, you can assert the value instead of using the copy property. Notice it won't work unless x is an integer > 0.
Sub test2()
Sheets(3).Select
x = 1
For i = 1 To 5
If Range("B" & i).Value = 1 Then
Sheets(4).Range("A" & x).Value = Range("B" & i).Value
'Sheets("Design").Range("A" & x).Value = Sheets("Offerte").Range("B" & i).Value
x = x + 1
End If
Next i
End Sub