我是Excel VBA的新手,我不确定我需要什么代码

问题描述 投票:0回答:2

我对VBA比较陌生,并且遇到了“不知道我需要什么代码”部分。我可能在我的解释中使用了错误的术语,我提前道歉。

JCX表上的金色和绿色是手动格式化以便澄清。

对于初学者来说,50的黄金单元格在正确的列(CFM)中,但是错误的行。根据08-属性表中的关系,50 CFM需要在AHU-2的行上(以及AHU-2的其他值)。我在VBA中创建这种“关系”时遇到了问题。

在加载第一个CFM值后,如果某些内容与AHU-2有关,并进入SP,RPM或Motor HP列......那么该数值需要进入AHU-2行的相应列。不是正确列中的下一行。

如果它不是那4个“类别”中的一个,那么它将进入“UDF”列,从第20列开始。再次,保持与“标记”的关系,我们现在将保留给AHU-2。

当这4个主要类别中的一个失败时,VBA也会返回####,它只是一个压缩的#REF!。在拍摄截图之前我没有展开它。

08-属性表

JCX表

TagRowCounter = 2
AttributeRowCounter = 2
TagNumberRow = 2
TagValueNameColumn = 20
TagValueColumn = 21

TagRowCounter = 2
Do
    Sheets("JCX").Cells(TagRowCounter, 14).Value = Application.VLookup(Sheets("JCX").Cells(TagRowCounter, 1), Sheets("08-Attribute").Range("A:K"), 1, 0)

        If Sheets("08-Attribute").Cells(AttributeRowCounter, 9) = "CFM" Then
                Sheets("JCX").Cells(TagNumberRow, 14).Value = Sheets("08-Attribute").Cells(AttributeRowCounter, 11).Value

            ElseIf Sheets("08-Attribute").Cells(AttributeRowCounter, 9) = "SP" Then
                Sheets("JCX").Cells(TagNumberRow, 15).Value = Sheets("08-Attribute").Cells(AttributeRowCounter, 11).Value

            ElseIf Sheets("08-Attribute").Cells(AttributeRowCounter, 9) = "RPM" Then
                Sheets("JCX").Cells(TagNumberRow, 16).Value = Sheets("08-Attribute").Cells(AttributeRowCounter, 11).Value

            ElseIf Sheets("08-Attribute").Cells(AttributeRowCounter, 9) = "Motor_HP" Then
                Sheets("JCX").Cells(TagNumberRow, 17).Value = Sheets("08-Attribute").Cells(AttributeRowCounter, 11).Value

            Else
                'Tag Value Name
                Sheets("JCX").Cells(TagNumberRow, TagValueNameColumn).Value = Sheets("08-Attribute").Cells(AttributeRowCounter, TagValueNameColumn).Value
                'Tag Value Number
                Sheets("JCX").Cells(TagNumberRow, TagValueColumn).Value = Sheets("08-Attribute").Cells(AttributeRowCounter, TagValueColumn).Value
            End If
                TagValueNameColumn = TagValueNameColumn + 2
                TagValueColumn = TagValueColumn + 2
                TagRowCounter = TagRowCounter + 1
                TagNumberRow = TagNumberRow + 1
                AttributeRowCounter = AttributeRowCounter + 1

Loop Until Sheets("08-Attribute").Cells(AttributeRowCounter, 1) = False
excel vba excel-vba
2个回答
0
投票

您的循环已关闭,您的查找已关闭。

将工作表JCX上的标签号与工作表08-Attribute上的AttributeID相匹配的方式不起作用,但您的计数器依赖于此,因此您将数据放在错误的位置。尝试下面这个重构的清理代码,并尝试逐步解决它的工作。

Dim TagRowCounter As Long
Dim AttributeRowCounter As Long
Dim ws08Attribute As Worksheet
Dim wsJCX As Worksheet

Set ws08Attribute = ThisWorkbook.Worksheets("08-Attribute")
Set wsJCX = ThisWorkbook.Worksheets("JCX")

'First loop goes down the list of the attributes on the 08-Attributes sheet
For AttributeRowCounter = 2 To ws08Attribute.Cells(2, 1).End(xlDown).Row
    'Within the first loop the second loop goes down the list of Tag numbers on the JCX sheet
    For TagRowCounter = 2 To wsJCX.Cells(2, 10).End(xlDown).Row
        'If an attributeID from sheet 08-Attributes matches with a Tag number on the JCX sheet then record the value
        If wsJCX.Cells(TagRowCounter, 10) = ws08Attribute.Cells(AttributeRowCounter, 1) Then
            Select Case ws08Attribute.Cells(AttributeRowCounter, 9)
                Case "CFM"
                    wsJCX.Cells(TagRowCounter, 14) = ws08Attribute.Cells(AttributeRowCounter, 11)
                    Exit For
                Case "SP"
                    wsJCX.Cells(TagRowCounter, 15) = ws08Attribute.Cells(AttributeRowCounter, 11)
                    Exit For
                Case "RPM"
                    wsJCX.Cells(TagRowCounter, 16) = ws08Attribute.Cells(AttributeRowCounter, 11)
                    Exit For
                Case "Motor HP"
                    wsJCX.Cells(TagRowCounter, 17) = ws08Attribute.Cells(AttributeRowCounter, 11)
                    Exit For
                Case Else
                    'If no UDF value has been recorded simply record value in col 20 and 21
                    If wsJCX.Cells(TagRowCounter, 20) = Empty Then
                        wsJCX.Cells(TagRowCounter, 20) = ws08Attribute.Cells(AttributeRowCounter, 9)
                        wsJCX.Cells(TagRowCounter, 21) = ws08Attribute.Cells(AttributeRowCounter, 11)

                    'Otherwise look for the first empty cell behind col 20 and 21
                    Else
                        wsJCX.Cells(TagRowCounter, 20).End(xlToRight).Offset(0, 1) = ws08Attribute.Cells(AttributeRowCounter, 9)
                        wsJCX.Cells(TagRowCounter, 20).End(xlToRight).Offset(0, 1) = ws08Attribute.Cells(AttributeRowCounter, 11)
                    End If
            End Select
        End If
    Next TagRowCounter
Next AttributeRowCounter

0
投票

看起来您正试图将“查找”表中的值复制到“报表”表单上的单行中。

您从函数获得#REF结果的原因是vLookup中没有符合您的条件 - 请参阅vlookup的Microsoft文档。 SilentRevolution为此提供了修复。

加速计算过程可以做的一件事是使用带有索引/匹配工作表函数的Excel表。您需要将工作表08-Attribute上的表定义为Excel表 - jkp-adsthespreadsheetguru

要输入下面的索引公式,请将其复制并粘贴到CFM列中。然后同时按下Contrl键,Shift键和Enter键。 (CTL-SHFT - 输入)。这将公式保存为Excel Array Formulae。最重要的是,如果要维护列名,则无法拖动公式。你必须复制然后通过公式。如果拖放列名称移动到下一列,就像复制单元格引用一样。

索引/匹配公式:

=IFERROR(INDEX(tbl80Attrib[AttributeValue],MATCH($J5&N$2,tbl80Attrib[AttributeID]&tbl80Attrib[AttributeName],0)),"")

按Ctrl-Shift-Enter后,公式将围绕它显示{},看起来像qazxsw poi注意:您无法输入{},Ctl-Shft-Enter会导致Excel执行此操作创建和数组公式。

然后你所要做的就是处理未定义的值。

© www.soinside.com 2019 - 2024. All rights reserved.