如何从不同工作表中的匹配列获取值

时间:2019-09-02 15:03:24

标签: excel vba

我有两个工作表(VariáveisIndicadores)。在Variáveis中有14列(列A代码,列B描述,列C:N-月,从一月到十二月),在Indicadores中有17列(列A-ID,列B-说明,C-公式,D-度量单位,E-信息,F:Q-月份(从一月到十二月)。

Variáveis包含每个代码的int每月值。 Indicadores具有由存储在Variáveis(例如(dAA11b+dAA12b)/dAA13b*100)中的代码组成的文本公式(C列),该代码经过转换(以下代码在@tigeravatar的帮助下开发)通过替换其对应的int月度值(例如(742+764)/125*100)的代码将其转化为excel公式。

工作表Variáveis的部分 Variáveis

工作表Indicadores的部分,其公式已转换,并在第一个月产生结果 Indicadores

注意:这只是一个示例,值会有所不同,因为我使用了=RANDBETWEEN()公式以int值填充工作表


    Dim wb As Workbook
    Dim wsLookup As Worksheet
    Dim wsData As Worksheet
    Dim aLookup() As Variant
    Dim aData() As Variant
    Dim lCodesLookupCol As Long
    Dim lCodesConvertCol As Long
    Dim i As Long

    Set wb = ActiveWorkbook
    Set wsData = wb.Worksheets("Indicadores")
    Set wsLookup = wb.Worksheets("Variáveis")

    'This line loads the codes we need to lookup and their associated Int Value into an array
    aLookup = wsLookup.Range("A2:C1309")

    lCodesLookupCol = LBound(aLookup, 2)
    lCodesConvertCol = UBound(aLookup, 2)

    'This is the range containing the Formulas stored as text in data worksheet
    With wsData.Range("C2", wsData.Cells(wsData.Rows.Count, "C").End(xlUp))

        'Loop through the lookup array and convert all codes into their associated Int Values using the Range.Replace method
        For i = 1 To UBound(aLookup, 1)
            .Replace aLookup(i, lCodesLookupCol), aLookup(i, lCodesConvertCol), xlPart, , False
        Next i

        'Now all of the Codes should have been replaced with their associated Int Values, but the formulas are still just text
        'This block will load the formulas as text into an array
        If .Cells.Count = 1 Then
            ReDim aData(1 To 1, 1 To 1)
            aData(1, 1) = .Formula
        Else
            aData = .Formula
        End If

        'Loop through the aData array to prepend an = sign to convert them into formulas
        For i = 1 To UBound(aData, 1)
            'Verifies if the cell isn't blank and that it's not already a formula
            If Len(aData(i, 1)) > 0 And Left(aData(i, 1), 1) <> "=" Then aData(i, 1) = "=" & aData(i, 1)
            wsData.Cells(i + 1, 6) = aData(i, 1)
            If Left(aData(i, 1), 1) <> "=" Then
                MsgBox "Error @ row " & Str(i + 1)
            End If
            'On Error Resume Next
        Next i

    End With

End Sub

现在,代码将Indicadores中的文本公式转换为excel公式,方法是将代码替换为它们的值(存储在Variáveis中)并将结果放在所需的列中(在这种情况下, F F- Jan)

目前,宏一次只能完成一个月的工作。我现在想做的是做相同的事情,但要同时进行所有几个月。感谢所有帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

有点混乱和过度设计,但是我认为这是将您的值实际写入输出表的那一行:

wsData.Cells(i + 1, 6) = aData(i, 1)

在这种情况下,数字“ 6”是要写入的列,这是有道理的,因为这是输出表中的“ January”列。

我要做的是将整个子程序封装在一个For循环中,该循环运行12次(每月一次),并使其在每个循环中递增该数字。像这样:

Sub Looper()

For x = 6 To 18

    LookupMachine x

Next x

End Sub

Sub LookupMachine(MonthNum As Integer)

    Dim wb As Workbook
    Dim wsLookup As Worksheet
    Dim wsData As Worksheet
    Dim aLookup() As Variant
    Dim aData() As Variant
    Dim lCodesLookupCol As Long
    Dim lCodesConvertCol As Long
    Dim i As Long

    Set wb = ActiveWorkbook
    Set wsData = wb.Worksheets("Indicadores")
    Set wsLookup = wb.Worksheets("Variáveis")

    'This line loads the codes we need to lookup and their associated Int Value into an array
    aLookup = wsLookup.Range("A2:N1309")

    lCodesLookupCol = LBound(aLookup, MonthNum - 4)
    lCodesConvertCol = UBound(aLookup, MonthNum - 4)

    'This is the range containing the Formulas stored as text in data worksheet
    With wsData.Range("C2", wsData.Cells(wsData.Rows.Count, "C").End(xlUp))

        'Loop through the lookup array and convert all codes into their associated Int Values using the Range.Replace method
        For i = 1 To UBound(aLookup, 1)
            .Replace aLookup(i, lCodesLookupCol), aLookup(i, lCodesConvertCol), xlPart, , False
        Next i

        'Now all of the Codes should have been replaced with their associated Int Values, but the formulas are still just text
        'This block will load the formulas as text into an array
        If .Cells.Count = 1 Then
            ReDim aData(1 To 1, 1 To 1)
            aData(1, 1) = .Formula
        Else
            aData = .Formula
        End If

        'Loop through the aData array to prepend an = sign to convert them into formulas
        For i = 1 To UBound(aData, 1)
            'Verifies if the cell isn't blank and that it's not already a formula
            If Len(aData(i, 1)) > 0 And Left(aData(i, 1), 1) <> "=" Then aData(i, 1) = "=" & aData(i, 1)
            wsData.Cells(i + 1, MonthNum) = aData(i, 1)
            If Left(aData(i, 1), 1) <> "=" Then
                MsgBox "Error @ row " & Str(i + 1)
            End If
            'On Error Resume Next
        Next i

    End With

End Sub

请注意,我扩展了aLookup数组以覆盖第N列,并将Array查找中的列号从硬编码的“ 2”更改为“ MonthNum-4”,这将增加其使用的数据列以您想要的方式。