[英]Loop through range in one sheet and have custom formula for each cell. Put data into a new sheet [excel][vba][bloomberg]
I'm not sure how descriptive my title is but below I will try to explain what I am trying to do.我不确定我的标题的描述性如何,但下面我将尝试解释我正在尝试做的事情。
Below is the code I am using.下面是我正在使用的代码。 It actually does what I need, however I am not able to make the output of my FOR loop to be in a new sheet.
它实际上满足了我的需要,但是我无法将我的 FOR 循环的 output 放在新工作表中。 I also believe that the writing is not the most efficient so any help there would be great.
我也相信写作不是最有效的,所以任何帮助都会很棒。
My current excel spreadsheet: How it looks now我当前的 excel 电子表格:现在的样子
Sub Macro1()
'
' Macro1 Macro
'
Dim ticker As Range
Dim cell As Range
Dim start_row As Integer
Dim row As Integer
Dim top_investors As Integer
top_investors = 5
start_row = 2
Range("K2:U999999").ClearContents
Range("L" & start_row).Select
For Each ticker In Range("A2:A" & Cells(Rows.Count, 2).End(xlUp).row)
If Not IsEmpty(ticker) Then
ActiveCell.Formula = "=BDS(" & Chr(34) & ticker.Value & Chr(34) & ",""TOP_20_HOLDERS_PUBLIC_FILINGS"",""Endrow""," & Chr(34) & top_investors & Chr(34) & ",""Endcol"",""9"")"
For Each cell In Range("K" & start_row & ":" & "K" & start_row + top_investors)
row = start_row
Range("K" & row).Select
cell.Value = ticker.Value
row = row + 1
Next cell
start_row = start_row + top_investors
Range("L" & start_row).Select
End If
Next ticker
End Sub
Well, I guess you need to do something like:好吧,我想你需要做这样的事情:
Sub Macro1()
'
' Macro1 Macro
'
Dim ticker As Range
Dim cell As Range
Dim start_row As Integer
Dim row As Integer
Dim top_investors As Integer
Dim mySheet as Worksheet
top_investors = 5
start_row = 2
Sheets.Add.Name = "myNewSheet"
mySheet = ThisWorkbook.Sheets("myNewSheet")
Range("K2:U999999").ClearContents
Range("L" & start_row).Select
For Each ticker In Range("A2:A" & Cells(Rows.Count, 2).End(xlUp).row)
If Not IsEmpty(ticker) Then
mySheet.Range("define your Range").Formula = "=BDS(" & Chr(34) & ticker.Value & Chr(34) & ",""TOP_20_HOLDERS_PUBLIC_FILINGS"",""Endrow""," & Chr(34) & top_investors & Chr(34) & ",""Endcol"",""9"")"
For Each cell In (mySheet?.)Range("K" & start_row & ":" & "K" & start_row + top_investors)
row = start_row
mySheet.Range("K" & row).Select
cell.Value = ticker.Value
row = row + 1
Next cell
start_row = start_row + top_investors
Range("L" & start_row).Select
End If
Next ticker
End Sub
That won't work yet, but I guess you got the idea of working with "mySheet" and know better than me were to add it in your code.这还行不通,但我猜你已经有了使用“mySheet”的想法,并且比我更了解将它添加到你的代码中。 Maybe you need to google a little bit about the correct syntax.
也许你需要谷歌一下正确的语法。
If you have a recent Excel version (that has the LET spreadsheet function) then VBA is not strictly necessary for what you want to achieve.如果您有最新的 Excel 版本(具有 LET 电子表格功能),那么 VBA 并不是您要实现的目标所必需的。
For example, if you tickers are in the range A4:A7 on 'Sheet 1', you can collect all the data in 'Sheet 2':例如,如果您的代码在“工作表 1”上的 A4:A7 范围内,您可以收集“工作表 2”中的所有数据:
The formula in Sheet2:A4: =Sheet1!A4
Sheet2:A4 中的公式:
=Sheet1!A4
The formula in Sheet2:B4: =LET(data,BDS(A4,$B$2,"Array=TRUE"),s,SEQUENCE(1,$B$1),INDEX(data,s,1))
Sheet2:B4中的公式:
=LET(data,BDS(A4,$B$2,"Array=TRUE"),s,SEQUENCE(1,$B$1),INDEX(data,s,1))
Then fill the formulae down as needed.然后根据需要填写公式。 The dynamic number of investors is in cell Sheet2,B1.
投资者的动态数量在单元格 Sheet2,B1 中。 but this can be hard-coded.
但这可以是硬编码的。
The main 'trick' is to use the "Array=TRUE"
option in the BDS()
call.主要的“技巧”是在
BDS()
调用中使用"Array=TRUE"
选项。 This returns the data in a dynamic array, which can be indexed into.这将返回动态数组中的数据,可以对其进行索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.