【问题标题】:Excel macro to compare values [closed]Excel宏来比较值[关闭]
【发布时间】:2019-03-13 00:18:51
【问题描述】:

我需要你的帮助 我想知道是否有任何 vba 脚本可以让我更轻松地完成工作 截至目前,我正在使用 IF 和 AND 条件 我想用宏来改变它 所以我可以选择范围并转换为“1”和“0”

我需要做的就是将每一列中的值与每一列的 1 个单元格中的正确答案进行比较 标记 1 表示匹配 0 表示不匹配

谢谢 任何帮助都会很棒

【问题讨论】:

    标签: excel vba if-statement compare cell


    【解决方案1】:

    我认为您正在寻找类似下面的 VBA 代码的内容。该代码是动态的,因此您的表格可以在大小上有所不同。您可以在以下位置设置数据表(候选 R1)的起始列:

    Const ColStart As Integer = 2
    

    还有最后一栏(候选 R10)

    Const ColEnd As Integer = 11
    

    VBA 代码

    Sub FormulaToCompare()
    
    Dim lrow As Integer
    Dim lrowCandidate As Integer
    Dim NextFree As Integer
    
    Const ColStart As Integer = 2 'Which column your data table start at (R1). R1 is in Column B = Column 2
    Const ColEnd As Integer = 11 'Which column your data table start at (R1). R1 is in Column B = Column 2
    
    
    lrowCandidate = Cells(Rows.Count, 1).End(xlUp).Row 'Find the last row of the Column Candidate (Column A)
    
    Range(Cells(1, 1), Cells(2, ColEnd)).Offset(lrowCandidate + 2, 0).Value = Range(Cells(1, 1), Cells(2, ColEnd)).Value 'Copy Headers to 2nd table
    Range(Cells(3, 1), Cells(lrowCandidate, 1)).Offset(lrowCandidate + 2, 0).Value = Range(Cells(3, 1), Cells(lrowCandidate, 1)).Value 'Copy Candidate numbers to 2nd table
    
    For i = ColStart To ColEnd 'Loop from start column to end column of data, Column i
        NextFree = Range(Cells(1, i), Cells(Rows.Count, i)).Cells.SpecialCells(xlCellTypeBlanks).Row - 1 'Find the last row starting from above in Column i EXCLUDING 2nd table.
        lrow = Cells(Rows.Count, i).End(xlUp).Row 'Find the last row starting from above in Column i INCLUDING 2nd table.
            For j = 3 To NextFree 'Loop from row 3 (2 rows are headers, therefore we start at row 3) to last row excluding 2nd table
                Cells(lrow + j - 2, i).FormulaR1C1 = "=IF(AND(R[-" & NextFree + 2 & "]C<>0, R[-" & NextFree + 2 & "]C=R1C),1,0)" 'Find the cell to paste the formula into. The formula is dynamic
            Next j
    Next i
    End Sub
    

    【讨论】:

    • 嗨,wizhi,谢谢你的解决方案,你让我的一天变得更轻松.. 像你这样的人指导需要知识的人非常有帮助,继续努力,
    • Wizi 有什么方法可以将更改附加到同一个表中,而无需在下面生成新表,因为您的宏做得很好,----我所有的队友都表达了他们的感谢你
    • 如果您将答案标记为已解决(Mark Answer as Approved),我将不胜感激。它可以帮助其他人知道问题已解决,并为花时间回答的人提供一些积分。我会等它;)
    • 感谢您的客气话。是的,它应该是可能的,你想要它怎么做?参考你问题中的例子,你想在哪一行追加表格?你想让表格直接从 13 开始吗?
    • 嗨wizhi,谢谢你,问题已经解决了,按照你的指示我已经标记了,这个解决了,再次感谢你,干杯
    猜你喜欢
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多