如何在Sheet公式VBA中引用已找到的列

时间:2017-02-02 08:05:15

标签: excel vba excel-vba

我遇到以下VBA问题。我有一个代码,它找到一个列,然后它在另一列中插入工作表公式。该公式包含对先前找到的列的引用。

Dim intBB As Integer
Dim rngBB As Range
Dim controlBB As Integer

intBB = 1

Do While Worksheets("Sheet2").Cells(2, intBB) <> ""
        If Worksheets("Sheet2").Cells(2, intBB).Value = "BbCode" Then
            With Worksheets("Sheet2")
                Set rngBB = .Range(.Cells(2, intBB), .Cells(2, intBB))
                controlBB = intBB
             End With
         Exit Do

        End If
          intBB = intBB + 1
    Loop


Worksheets("Sheet2").Range("W3:W2500").Formula = "= _
IF (controlBB="""","""",BDP(controlBB&"" Equity"",""ID_ISIN""))"

然而,这不起作用。我怎样才能正确引用找到的列?

2 个答案:

答案 0 :(得分:1)

由于您有列号,而不是字母,我建议使用R1C1格式:

Worksheets("Sheet2").Range("W3:W2500").FormulaR1C1 = "= _
IF (RC" & controlBB & "="""","""",BDP(RC" & controlBB & "&"" Equity"",""ID_ISIN""))"

答案 1 :(得分:0)

我就是这样做的

 SELECT requester_id, email, username, is_dashboard_user, SUBMITTED_ON 
 FROM testing_a, aspusers a 
 WHERE requester_id=a.user_id 
 AND AD_MAIL_ATTRIBUTE IS NOT NULL 
 AND IS_DASHBOARD_USER = 'y' 
 AND YEAR(SUBMITTED_ON) = $year
 GROUP BY MONTH(SUBMITTED_ON)