This question already has answers here:
How do I put double quotes in a string in vba?
(5 answers)
Closed last year.
I cannot find the error(s) in this code. What I am trying to do is insert the formula in the column S, when "CN Equity" is used in the column A. I want the macro to run from row 6 to 69.
Sub fx()
Dim x As Long
x = 6
Do
If InStr(1, (Range("A" & x).Value), "CN Equity") > 0 Then
Sheets("Sheet1").Range("S" & x).Formula = "=BDP("CADUSD BGN Curncy","LAST_PRICE")" & x
End If
x = x + 1
Loop Until x = 70
End Sub
Consider:
Sub fx()
Dim x As Long
x = 6
Do
If InStr(1, (Range("A" & x).Value), "CN Equity") > 0 Then
Sheets("Sheet1").Range("S" & x).Formula = "=BDP(""CADUSD BGN Curncy"",""LAST_PRICE"")+" & x
End If
x = x + 1
Loop Until x = 70
End Sub
NOTE
Replace the + in the formula with * or & if necessary.
Related
I have a loop that runs until a condition is met in Row 8.
Sub Button1_Click()
Dim i As Double
Dim x As Double
t = Range("K3").Value
i = Range("C8").Value
x = Range("E8").Value
b = Range("B8").Value
Do Until i > (x + t)
i = i + 0.2
Loop
i = i - 0.2 - b
Range("G8").Value = i
End Sub
This does what I require for Row 8 but I need to run on multiple rows. The only value that will be static is cell 'K3'.
i.e.: Once it is done on row 8, run on Row 9 ('C9','E9','B9','G9') and so on. Either this will need to run until row 500, or a count of rows entered.
There will be a whole heap of people here that will give you a much more complete answer but I'm here to answer your direct question.
If you need to apply other enhancements then that can come with other questions. This will help you learn and progress without throwing a heap of code at you that you're not sure of.
Sub Button1_Click()
Dim i As Double
Dim x As Double
Dim lngRow As Long
t = Range("K3").Value
For lngRow = 8 To 500
i = Range("C" & lngRow).Value
x = Range("E" & lngRow).Value
b = Range("B" & lngRow).Value
Do Until i > (x + t)
i = i + 0.2
Loop
i = i - 0.2 - b
Range("G" & lngRow).Value = i
Next
End Sub
That's a simple way to loop through each row.
Considerations you should make on performance are to do with looping, updating cells constantly, calculations and events when updating cells, etc.
But that answers your question.
I am new to excel and VBA so apologies for silly question or mistake.
i have some 2000 excel data in sheet2 and the data req in sheet 1
I need to know how many ticket which starts with INC and priority P2 P3 are there and same way how many tickets which starts with SR are there. also out of them how many are in closed state and how many are active.
Sub US_Data()
Dim z As Long
Dim C As Range
z = Sheet2.Cells(Rows.Count, 1).End(xlUp).Row
For Each C In Sheet2.Range(Sheet2.Cells(2, 1), Sheet2.Cells(z, 1))
If Left(C.Value, 3) = "INC" Then
Sheet1.Cells(8, 6) = Sheet1.Cells(8, 6) + 1
End If
Next C
End Sub
Thank you
Why use VBA at all? This can be done with simple formulas. If you don't want to use pivot tables, manually create the headings (Blue in the screenshot), then put this formula into cell H3, copy across and down.
=COUNTIFS($A:$A,$G3&"*",$B:$B,H$1,$C:$C,H$2)
Change the layout if you want. The point is that you don't need VBA for that. Formulas will be a lot faster than re-inventing a CountIfs with VBA.
Sub US_Data()
Dim z As Long
Dim HighCount as Long
Dim ModerCount as Long
Dim LowCount as Long
Dim OpenCount as Long
Dim ClosedCount as Long
Dim C As Range
z = Sheet2.Cells(Rows.Count, 1).End(xlUp).Row
For Each C In Sheet2.Range(Sheet2.Cells(2, 1), Sheet2.Cells(z, 1))
If Left(C.Value, 3) = "INC" Then
If C.Offset(0,1).Value = "2 - High" Then HighCount = HighCount + 1
If C.Offset(0,1).Value = "3 - Moderate" Then ModerCount = ModerCount + 1
If C.Offset(0,1).Value = "4 - Low" Then LowCount = LowCount + 1
If C.Offset(0,2).Value = "Closed" Then ClosedCount = ClosedCount + 1
If C.Offset(0,2).Value = "Open" Then OpenCount = OpenCount + 1
End If
Next C
MsgBox "I have counted " & HighCount & " times High, " & ModerCount & " times Moderate, " & LowCount & " times Low, and respectively " & OpenCount & " and " & ClosedCount & " open and closed instances.", vbOkOnly, "FYI"
Sheet1.Cells(8, 6) = HighCount
End Sub
This would be one way of doing it, you can fill the cells necessary with those variables.
Hello I recently started to code in VBA and would like to implement the function 'AverageIF'.
In the link you can see that in column B there are values after 00:00 and I would like to get the Average of the column B, starting with B2 through B32.
Furthermore I would like to put the answer in B33.
The first code that i am working is seen here, this wil get the AverageIF:
noxGem = WorksheetFunction.averageif(Sheet2.Range("B" & beginRow & ":" & "B" & offsetCellNum2), ">0")
In the above code I assumed that Row B2 till Row B32 will be checked for values bigger than 0. I don't know if that is the correct notation, because I am getting an error :
My second code is seen here, this will put the averageIF in B33:
Worksheets("Sheet2").Range("B" & offsetCellNum).Value = noxGem
I think this code is working, because i have putted the data (B2:B32) with this code in a loop.
Do you guys maybe have any suggestion to what the problem would be?
Much appreciated!
EDIT
I have posted my whole code, My apologies!
The Error message = Error 424 Object Required
Sub averageif_1()
Dim noxValue As Double 'De waarde die je gaat nemen moet een decimaal
getal worden. Single kan ook gebruikt worden
Dim noxCellNumm As String 'Je gaat in de kolom I zoeken per rij van 24 dus
de waarde moet steeds veranderen I2 I3 I4 etc etc..
Dim x As Integer 'Dit geeft voor de 'GEM_Sheet0' aan, in welke rij de data
wordt opgeslagen
counter = 0
offsetCellNum = 2
beginRow = 2
eindRow = 745
Dim noxGem As Double
While counter < 24
Select Case counter
Case Is = 0
x = beginRow
x2 = eindRow
For i = x To x2 Step 24
noxCellNumm = "I" & i
noxValue = Worksheets("Sheet1").Range(noxCellNumm).Value
'Debug.Print strValue
Worksheets("Sheet2").Range("B" & offsetCellNum).Value =
noxValue
offsetCellNum = offsetCellNum + 1
Next
Debug.Print "00:00"
offsetCellNum2 = offsetCellNum - 1
noxGem = WorksheetFunction.averageif(Sheet2.Range("B" &
beginRow & ":" & "B" & offsetCellNum2), ">0")
'noxGem = WorksheetFunction.averageif
'noxGem = WorksheetFunction.averageif(Sheet2.Range("B2:B33"),
"=0")
Debug.Print noxGem
Worksheets("Sheet2").Range("B" & offsetCellNum).Value = noxGem
counter = counter + 1
offsetCellNum = 2
beginRow = beginRow + 1
Case Else
counter = 100
End Select
Wend
End Sub
Your code would be easier to debug if you declared all of your variables.
Put Option Explicit at the top of your code to force this.
To have this happen all the time, under Tools/Options select that option:
Had you done that you might find that Sheet2. in your AverageIf line is flagged as an undeclared variable, and that there is no worksheet with that CodeName (different from the WorksheetName) in your active workbook.
I need to loop this section and overwrite the x marked positions with my variable x value but if i use it as it is shown on the pic nothings happen. If i enter any value like 1 or 2 instead of x it works. How can i use a variable instead of a value in these places?
Here is also the code:
For j = 1 To SumPositionen
If Sheets("Datenverarbeitung").Cells(Z, 4) = tempCommodityForm Then
session.findById("wnd[0]/usr/tabsTAB_DETAILS/tabpTAB_MF/ssubTABMF:/HERA/TRDMF10:2023/tbl/HERA/TRDMF10TC_2023/ctxt/HERA/TRDINOUT_S-MATNR[1,x]").Text = Sheets("Datenverarbeitung").Cells(a, 5) 'Eingabe Materialnummer in 1. Position
session.findById("wnd[0]/usr/tabsTAB_DETAILS/tabpTAB_MF/ssubTABMF:/HERA/TRDMF10:2023/tbl/HERA/TRDMF10TC_2023/txt/HERA/TRDINOUT_S-MFQUAN[2,x]").Text = Sheets("Datenverarbeitung").Cells(a, 6) 'Eingabe Menge in 1. Position
session.findById("wnd[0]/usr/tabsTAB_DETAILS/tabpTAB_MF/ssubTABMF:/HERA/TRDMF10:2023/tbl/HERA/TRDMF10TC_2023/ctxt/HERA/TRDINOUT_S-MFU[3,x]").Text = Sheets("Datenverarbeitung").Cells(a, 7) 'Eingabe UoM
x = x + 1
End If
a = a + 1
Next j
session.findById("wnd[0]/usr/tabsTAB_DETAILS/tabpTAB_MF/ssubTABMF:/HERA/" & _
"TRDMF10:2023/tbl/HERA/TRDMF10TC_2023/ctxt/HERA/TRDINOUT_S-MATNR[1,x]").Text
Here your string includes the literal value "x". What you really want is likely something more like:
session.findById("wnd[0]/usr/tabsTAB_DETAILS/tabpTAB_MF/ssubTABMF:/HERA/" & _
"TRDMF10:2023/tbl/HERA/TRDMF10TC_2023/ctxt/HERA/TRDINOUT_S-MATNR[1," & x & "]").Text
So something like:
Dim s As String, sht As Worksheet
'....
'....
Set sht = Sheets("Datenverarbeitung")
s = "wnd[0]/usr/tabsTAB_DETAILS/tabpTAB_MF/ssubTABMF:/HERA/" & _
"TRDMF10:2023/tbl/HERA/TRDMF10TC_2023/ctxt/HERA/"
For j = 1 To SumPositionen
If sht.Cells(Z, 4) = tempCommodityForm Then
session.findById(s & "TRDINOUT_S-MATNR[1," & x & "]").Text = sht.Cells(a, 5) 'Eingabe Materialnummer in 1. Position
session.findById(s & "TRDINOUT_S-MFQUAN[2," & x & "]").Text = sht.Cells(a, 6) 'Eingabe Menge in 1. Position
session.findById(s & "TRDINOUT_S-MFU[3," & x & "]").Text = sht.Cells(a, 7) 'Eingabe UoM
x = x + 1
End If
a = a + 1
Next j
define data type to variable link string and integer
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
VBA function to convert column number to letter?
I need to display a msgbox that shows the last used column.
Right now the columns numeric value is displayed but can it be converted into its alpha character equivalent?
Ex. column 5 = column E.
Thanks
I managed to get 2 methods. One relies on the excel Cells, so it fails if you use an invalid column:
Function NumToLetter(q As Long) As String
Dim Add As String
'Ignore error so it fails on an invalid cell
On Error Resume Next
Add = Cells(1, q).Address
If Len(Add) Then
NumToLetter = Mid(Add, 2, InStr(2, Add, "$") - 2)
Else
NumToLetter = "Invalid Cell"
End If
End Function
the other uses base 26, and is only limited bu the maximum value for long:
Function Num2Letter(q As Long) As String
Dim r As Long
While q > 0
r = q Mod 26
If r = 0 Then r = 26 ' needed for column Z
Num2Letter = Chr(64 + r) & Num2Letter
q = Int(q / 26) + (r = 26) ' fix for column Z
Wend
End Function
to test these values with a messagebox:
msgbox "Column 2445 is " & NumToLetter(2445) & " or " Num2Letter(2445)
Column 2445 is CPA or CPA
msgbox "Column 42445 is " & NumToLetter(42445) & " or " Num2Letter(42445)
Column 42445 is Invalid Cell or BJTM
msgbox "Column -1 is " & NumToLetter(-1) & " or " Num2Letter(-1)
Column -1 is Invalid Cell or
This works for columns A through ZZ (as pointed out in the comments by Sean Cheshire):
Function ColLetter(ColNumber As Integer) As String
ColLetter = Left(Cells(1, ColNumber).Address(False, False), 1 - (ColNumber > 26))
End Function
To use: MsgBox ColLetter(oRangeObject.Column)