简体   繁体   English

在一张表中循环遍历范围,并为每个单元格设置自定义公式。 将数据放入新工作表 [excel][vba][bloomberg]

[英]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.我不确定我的标题的描述性如何,但下面我将尝试解释我正在尝试做的事情。

  • I have a list of company Bloomberg tickers => maybe more than 100我有一份公司 Bloomberg 代码列表 => 可能超过 100
  • This list of tickers is saved in say "Sheet1" "A:A"此代码列表保存在说“Sheet1”“A:A”中
  • For each ticker I have a Bloomberg (BDS) formula that returns a certain number of shareholders对于每个代码,我都有一个彭博 (BDS) 公式,可以返回一定数量的股东
  • The number of shareholders needs to be dynamic股东数量需要动态
  • The ticker of each company needs to be copied alongside the info gathered from BBG (as this is not provided by their formula)每个公司的代码需要与从 BBG 收集的信息一起复制(因为他们的公式没有提供)
  • This whole data should be in a new sheet let's say "Sheet2"这整个数据应该在一个新的工作表中,比如“Sheet2”

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.

相关问题 Excel VBA - 循环遍历范围并在每个单元格中设置公式 - Excel VBA - Loop through range and set formula in each cell Excel VBA:For Loop,用于遍历图纸范围 - Excel VBA: For Loop for iteration through Sheet range 使用VBA遍历每个Excel工作表 - Loop through each excel sheet using vba 每个新工作表的Excel VBA复制数据 - Excel VBA copy data for each new sheet 范围A中每个单元格的Excel VBA,单元格B中的值复制到新工作表 - Excel VBA for each cell in range A, copy value in cell B, to new sheet VBA Excel尝试创建一个从文件导入数据的宏,然后如果数据等于特定值,则将一个单元格放到新文件的工作表中 - VBA excel trying to create a macro that imports data from file then if the data is equal to a specific value put one cell into a sheet on the new file 使用 Excel 中的 VBA 将基于单元格值的工作表中的数据提取到新工作表中 - Extracting data from one sheet based on a cell value into a new sheet using VBA in Excel For 在工作簿中循环工作表 – 工作表名称等于单元格范围 - Excel VBA - For Loop through sheets in a workbook – sheet names equal to a cell range - Excel VBA Excel VBA-遍历工作簿并用每个工作表的名称标记单元格 - Excel VBA - Loop through workbook and label a cell with each sheet's name 通过单元格引用在另一张表中循环输入值,然后复制公式值并使用 vba 将其粘贴到另一张表中 - Loop input values in another sheet through cell reference then Copy formula value and paste it to another sheet using vba
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM