I'm trying to create a spreadsheet that could execute a BDH bloomberg code for each equity and within each equity id want it to go through all of the criteria such as "PX_Last", VOL_90D, PX_BID and etc.
Essentially, the ideal output I'd like would be:
Eg:
0665 HK Equity -> PX_Last, PX_BID, PX_ASK, VOL_90D, then,
1098 HK Equity -> PX_Last, PX_BID, PX_ASK, VOL_90D, then,
etc.
Here is a code that I've recently created for my spreadsheet.
Would appreciate any insights about my code!
For x = 1 To i
If x = 1 Then
equity = Worksheets(1).Cells(x + 2, 2)
Worksheets(2).Cells(2, 2).Value = equity
For y = 1 To j
If y = 1 Then
reutersticker = Worksheets(1).Cells(y + 2, 1)
Worksheets(2).Cells(1, 2).Value = reutersticker
For Z = 1 To k
If Z = 1 Then
LastPrice = Worksheets(1).Cells(Z + 1, 8)
Worksheets(2).Cells(3, 1).FormulaR1C1 = "=BDH(""" & equity & """," & LastPrice & ",""" & StartDate & """,""" & EndDate & """," & Weekdays & "," & Fill & ")"
ElseIf Z > 1 Then
LastPrice = Worksheets(1).Cells(Z + 1, 8)
Worksheets(2).Cells(3, 1).Offset(, (2 * Z) - 2).FormulaR1C1 = "=BDH(""" & equity & """," & LastPrice & ",""" & StartDate & """,""" & EndDate & """," & Weekdays & "," & Fill & ")"
End If
Next Z
ElseIf y > 1 Then
reutersticker = Worksheets(1).Cells(y + 2, 1)
Worksheets(2).Cells(1, 2).Offset(, (4 * y) - 2).Value = reutersticker
End If
Next y
ElseIf x > 1 Then
equity = Worksheets(1).Cells(x + 2, 2)
Worksheets(2).Cells(2, 2).Offset(, (8 * x) - 2).Value = equity
End If
Next x
End Sub
Related
from some googling I found this function that will concatenate the data in columns A, B & C based off the value in column D. This code does not work for me for some reason. My data looks like such
Bob Jason 0123456789 Tim
Jim Jason 0123456789 Tim
Fred Jason 0123456789 Tim
Columns, A and B concat fine, but column C concats to
12,345,678,901,234,500,000,000,000,000
How would the VBA be altered so that the code will concatenate properly?
Sub Concat()
Dim x, i As Long, ii As Long
With Cells(1).CurrentRegion
x = .Columns("d").Offset(1).Address
x = Filter(Evaluate("transpose(if(countif(offset(" & x & ",,,row(1:" & .Rows.Count & "))," & x & ")=1," & x & "))"), False, 0)
For i = 0 To UBound(x)
For ii = 1 To 3
Cells(i + 2, ii + 5).Value = Join(Filter(Evaluate("transpose(if(" & .Columns(4).Address & "=""" & _
x(i) & """," & .Columns(ii).Address & "))"), False, 0), ",")
Next
Cells(i + 2, ii + 5).Value = x(i)
Next
End With
End Sub
You need to set the destination cells to a Text format:
Sub Concat()
Dim x, i As Long, ii As Long
With Cells(1).CurrentRegion
x = .Columns("d").Offset(1).Address
x = Filter(Evaluate("transpose(if(countif(offset(" & x & ",,,row(1:" & .Rows.Count & "))," & x & ")=1," & x & "))"), False, 0)
For i = 0 To UBound(x)
For ii = 1 To 3
Cells(i + 2, ii + 5).NumberFormat = "#"
Cells(i + 2, ii + 5).Value = Join(Filter(Evaluate("transpose(if(" & .Columns(4).Address & "=""" & _
x(i) & """," & .Columns(ii).Address & "))"), False, 0), ",")
Next
Cells(i + 2, ii + 5).NumberFormat = "#"
Cells(i + 2, ii + 5).Value = x(i)
Next
End With
End Sub
I need a looping structure that checks a range of cells, then if the cell and a cell that is in the range equal each other then the font should turn red. My problem is that my do until loop won't get entered. This is what I have right now.
`
Dim finalrow As Long
finalrow = Worksheets("Redundancy").Cells(Worksheets("Redundancy").Rows.Count, "D").End(xlUp).Row
Dim z As Long
Dim w As Long
Dim r As Long
w = 2
r = 0
For z = 2 To finalrow
If Range("L" & z) = Range("L" & z + 1) & Range("J" & z) <> Range("J" & z + 1) Then
Do Until Range("L" & z) = Range("L" & z + 1) & Range("J" & z) <> Range("J" & z + 1)
If Cells(w, 4) = Cells(z + 1, 4 + r) Then
Cells(w, 1).Font.ColorIndex = 3
Cells(z + 1, 1).Font.ColorIndex = 3
If r = 4 Then
w = w + 1
End If
End If
r = r + 1
Loop
End If
Next z
`
I changed it to this, but it exits the loop all together right when it is about to enter the do while loop.
`
For z = 2 To finalrow
Do While (Range("L" & z) = Range("L" & z + 1) And Range("J" & z) <> Range("J" & z + 1))
If Cells(w, 4) = Cells(z + 1, 4 + r) Then
Cells(w, 1).Font.ColorIndex = 3
Cells(z + 1, 1).Font.ColorIndex = 3
If r = 4 Then
w = w + 1
End If
End If
r = r + 1
Loop
Next z
`
If you do this;
Range("L" & z) = Range("L" & z + 1) and Range("J" & z) <> Range("J" & z + 1)
you are comparing Range objects. What you instead want to do is to compare the values in those range objects. So use this instead;
Range("L" & z).value = Range("L" & z + 1).value and Range("J" & z).value <> Range("J" & z + 1).value
However when you use the cells(row,column) you don't have this problem.
I am curious though, was it not possible to use conditional formatting instead?
Use the 'and' operator instead of '&'.
I am trying to fill a range with a formula and continue to get a runtime error '1004'. The error occurs at the line I have starred Sheets("Forecast").Range("H125").Formula = formulaTest. The code in my Sub is as follows:
Sub FirmShareFill()
Dim RampUp As Range
Dim RampBas As Range
Dim RampDn As Range
Dim Numbering As Range
Dim Approval As Range
Dim PeakShare As Range
Dim tcount As Byte
Dim bcount As Byte
Dim ubdcount As Byte
Dim yearRange2 As Byte
year = Worksheets("Inputs").Range("B6").Value
cntry = Worksheets("Inputs").Range("B5").Value
bnd = Worksheets("Inputs").Range("B3").Value
typ = Worksheets("Inputs").Range("B2").Value
cat = Worksheets("Inputs").Range("B4").Value
tcount = bnd * cat + bnd
ubdcount = tcount * 2 + 1
yearCount = year * 4 - 1
For ubd = 1 To 3
For t = 1 To typ
For b = 1 To bnd
For c = 1 To cat
For i = 1 To cntry
Set RampUp = Columns(7).Find(What:="Ramp_Up" & i, MatchCase:=True).Offset(0, 1)
Set RampBas = Columns(7).Find(What:="Ramp_Bas" & i, MatchCase:=True).Offset(0, 1)
Set RampDn = Columns(7).Find(What:="Ramp_Dn" & i, MatchCase:=True).Offset(0, 1)
Set Numbering = Sheets("Inputs").Range("B13")
Set Approval = Columns(6).Find(What:="Approval", MatchCase:=True).Offset(i, 2 + ubd)
bcount = c + (cat + 1) * (b - 1)
If t = 1 And b = 1 And ubd = 1 Then
Set PeakShare = Columns(5).Find(What:="Peak Share", MatchCase:=True).Offset(4 + i, c)
ElseIf t = 1 And b > 1 And ubd = 1 Then
Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, bcount)
ElseIf t > 1 And b = 1 And ubd = 1 Then
Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, c + tcount)
ElseIf t > 1 And b > 1 And ubd = 1 Then
Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, tcount + bcount)
ElseIf t = 1 And b = 1 And ubd = 2 Then
Set PeakShare = Columns(5).Find(What:="Peak Share", MatchCase:=True).Offset(4 + i, c + ubdcount)
ElseIf t = 1 And b > 1 And ubd = 1 Then
Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, bcount + ubdcount)
ElseIf t > 1 And b = 1 And ubd = 1 Then
Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, c + tcount + ubdcount)
ElseIf t > 1 And b > 1 And ubd = 1 Then
Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, tcount + bcount + ubdcount)
ElseIf t = 1 And b = 1 And ubd = 3 Then
Set PeakShare = Columns(5).Find(What:="Peak Share", MatchCase:=True).Offset(4 + i, c + 2 * ubdcount)
ElseIf t = 1 And b > 1 And ubd = 3 Then
Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, bcount + 2 * ubdcount)
ElseIf t > 1 And b = 1 And ubd = 3 Then
Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, c + tcount + 2 * ubdcount)
ElseIf t > 1 And b > 1 And ubd = 3 Then
Set PeakShare = Columns(5).Find(What:="Peak Share" & c, MatchCase:=True).Offset(4 + i, tcount + bcount + 2 * ubdcount)
End If
Dim formulaTest As String
formulaTest = "=IF(" & Numbering.Address(False, False) & "<" & Approval.Address & ","", " & PeakShare.Address & " * " & RampUp.Address & ")"
If ubd = 1 Then
**Sheets("Forecast").Range("H125").Formula = formulaTest**
ActiveCell.Offset(1, 0).Select
ElseIf ubd = 2 Then
Range(ActiveCell, ActiveCell.Offset(0, yearRange2)).Formula = "=IF(" & Numbering.Address(False, False) & " < " & Approval.Offset(1, 0).Address & ","", " & PeakShare.Address & " * " & RampBas.Address & ""
ElseIf ubd = 3 Then
Range(ActiveCell, ActiveCell.Offset(0, yearRange2)).Formula = "=IF(" & Numbering.Address(False, False) & " < " & Approval.Offset(1, 0).Address & ","", " & PeakShare.Address & " * " & RampDn.Address & ""
End If
Next i
ActiveCell.Offset(1, 0).Select
Next c
Next b
Next t
Next ubd
End Sub
I believe the error may have something to do with how I declared the range "numbering" range, but as of yet I have been unable to figure it out. I have used this code on the same sheet many times, the only difference being that I have set a range, numbering, on a different sheet.
This should work:
formulaTest = "=IF(" & Numbering.Address(False, False) & "<" & Approval.Address & ",""""," & PeakShare.Address & "*" & RampUp.Address & ")"
As #Comintern pointed out, you need to use """" to include double empty speech marks in your formula. I also removed the spaces either side of the *
change
IF(" & Numbering.Address(False, False) & "<" & Approval.Address & ","", " & PeakShare.Address & " * " & RampUp.Address & ")"
to
IF(" & Numbering.Address(False, False) & "<" & Approval.Address & ","""", " & PeakShare.Address & " * " & RampUp.Address & ")"
Instead of counting how many " you have, you can use Chr(34) inside " to have a Formula check for ".
In your case, use:
"=IF(" & Numbering.Address(False, False) & "<" & Approval.Address & "," & Chr(34) & ", " & PeakShare.Address & " * " & RampUp.Address & ")"
I am trying to get my code to insert a formula into an array based on how many data points there are in my data set. The code below almost works but after the first iteration of X is complete it does not insert the formula into the all the rows in the columns.
Worksheets(" Branded").Range("C3").Formula = "=COUNTIFS(" & r.Address(ReferenceStyle:=xlR1C1) & ",RC2, " & r2.Address(ReferenceStyle:=xlR1C1) & ",R2C)"
For Y = 1 To Column_Limit1 - 1
Range("C3").Offset(0, Y).Formula = "=COUNTIFS(" & r.Address(ReferenceStyle:=xlR1C1) & ",RC2, " & r2.Address(ReferenceStyle:=xlR1C1) & ",R2C)"
For X = 1 To Row_Limit1 - 1
Range("C3").Offset(X, 0).Formula = "=COUNTIFS(" & r.Address(ReferenceStyle:=xlR1C1) & ",RC2, " & r2.Address(ReferenceStyle:=xlR1C1) & ",R2C)"
Next X
Next Y
Change to this:
With Worksheets(" Branded")
.Range(.Cells(3, 3), .Cells(Row_Limit1 + 2, Column_Limit1 + 2)).FormulaR1C1 = "=COUNTIFS(" & r.Address(ReferenceStyle:=xlR1C1) & ",RC2, " & r2.Address(ReferenceStyle:=xlR1C1) & ",R2C)"
.Range(.Cells(Row_Limit1 + 3, 3), .Cells(Row_Limit1 + 3, Column_Limit1 + 2)).FormulaR1C1 = "=SUM(R3C:R[-1]C)"
End With
When using R1C1 there is no need of a loop.
i'm trying to execute these code in my excel sheet
ActiveCell.Offset(0, 3).Formula = "=if(SUM(N" & i + 2 & ":N" & i + 5 & ")>0;MEDIAN(N" & i + 2 & ":N" & i + 5 & ");0)"
and i'm get an #1004 error with no more informations. Can anybody eyplain my failure?
I hav some others formulars insert in the same way...thx
EDIT:
My Tables look like that
This should be a projectmanagement tool - Breitband Delphi Method ;)
So my code goes through all the rows and check in which column the descripton is (level 1,2,3,4).
Next the code is adding the rows 8-12 for example.. here i can enter some informations for the project... and now my script should add the formula at column k-n.
My code is not very nice (as my english :) ) - it is just a prototype..
This is my Loop
i = 5
canSkip = False
Do
' fist first the level
If Not IsEmpty(Range("B" & i).Value) Then
level = 1
If Not IsEmpty(Range("D" & i + 1)) Then
' ye we can - so skip this loop
canSkip = True
End If
ElseIf Not IsEmpty(Range("D" & i).Value) Then
level = 2
If Not IsEmpty(Range("F" & i + 1)) Then
' ye we can - so skip this loop
canSkip = True
End If
ElseIf Not IsEmpty(Range("F" & i).Value) Then
level = 3
If Not IsEmpty(Range("H" & i + 1)) Then
' ye we can - so skip this loop
canSkip = True
End If
ElseIf Not IsEmpty(Range("H" & i).Value) Then
level = 4
canSkip = False
End If
If canSkip = True Then
i = i + 1
Else
' First insert some... and bang it to a group
' Insert Formula
Range("K" & i).Activate
ActiveCell.Formula = "=min(L" & i + 2 & ":L" & i + 5 & ")"
ActiveCell.Offset(0, 1).Formula = "=max(L" & i + 2 & ":L" & i + 5 & ")"
'Range("T1").FormulaLocal = insertMedianFormula
'ActiveCell.Offset(0, 3).Formula = "=WENN(SUMME(N" & i + 2 & ":N" & i + 5 & ")>0;MITTELWERT(N" & i + 2 & ":N" & i + 5 & ");0)"
Range("A" & i + 1).Activate
For x = 1 To 5
ActiveCell.EntireRow.Insert
If x = 5 Then
If level = 1 Then
ActiveCell.Offset(0, 1).Value = "Experte"
ActiveCell.Offset(0, 2).Value = "Aufw."
ActiveCell.Offset(0, 3).Value = "Bemerkung"
ElseIf level = 2 Then
ActiveCell.Offset(0, 3).Value = "Experte"
ActiveCell.Offset(0, 4).Value = "Aufw."
ActiveCell.Offset(0, 5).Value = "Bemerkung"
ElseIf level = 3 Then
ActiveCell.Offset(0, 5).Value = "Experte"
ActiveCell.Offset(0, 6).Value = "Aufw."
ActiveCell.Offset(0, 7).Value = "Bemerkung"
ElseIf level = 4 Then
ActiveCell.Offset(0, 7).Value = "Experte"
ActiveCell.Offset(0, 8).Value = "Aufw."
ActiveCell.Offset(0, 9).Value = "Bemerkung"
End If
' now just bang it to a group
ActiveCell.Resize(5, 10).Rows.Group
End If
Next x
i = i + 6
End If
' are we finshed?
If i > lastUsedRow Then
Exit Do
End If
canSkip = False
Loop
Original formula (MS standard) uses "," instead of ";"
ActiveCell.Offset(0, 3).Formula = "=IF(SUM(N" & i + 2 & ":N" & i + 5 & ")>0,MEDIAN(N" & i + 2 & ":N" & i + 5 & "),0)"
or use:
ActiveCell.Offset(0, 3).FormulaLocal = "=IF(SUM(N" & i + 2 & ":N" & i + 5 & ")>0;MEDIAN(N" & i + 2 & ":N" & i + 5 & ");0)"
Please, refer this:
Formula
FormulaLocal
[EDIT]
First of all...
IsEmpty indicates whether a variable (of variant) has been initialized. So, if you want to check if cell is empty (does not contains any value), use:
Range("B" & i)<>""
Second of all..
Your code has no context. What it means? Using ActiveCell or Range("") or Cell() depends on what workbook (and its sheet) is actually in usage!
You should use code in context:
With ThisWorkbook.Worksheets("SheetName")
.Range("A1").Offset(0,i).Formula = "='Hello Kitty'"
.Cell(2,i) = "123.45"
End With
Third of all...
Review and debug you code and start again using above tips ;)