通过筛选另一个工作表来创建Excel工作表

时间:2017-03-07 02:41:21

标签: excel excel-2016

我有一个包含以下值的工作表 Artem's advice 我希望,如果可能的话,使用一个等式,根据它们是低风险,中等风险还是高风险,将这些值重新排列成三列,在另一个工作表中,如此

enter image description here

不注意着色和边框,我可以稍后添加(或使用条件格式)。

问题:有人可以提供最小工作示例(MWE)或用于将一个表转换为三个并排表的公式。

1 个答案:

答案 0 :(得分:1)

我会用VBA来解决这个问题:

Sub Sortdata()

Dim wsRisk As Worksheet, wsThisSheet As Worksheet
Dim colHigh As Long, colMed As Long, ColLow As Long

colHigh = 3
colMed = 3
ColLow = 3

Set wsThisSheet = ActiveSheet

With ThisWorkbook
        .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "Risk"
End With

Set wsRisk = Worksheets("Risk")
wsRisk.Range("A1").Value = "Risk of Responsibility"
wsRisk.Range("A2").Value = "High Risk"
wsRisk.Range("C2").Value = "Medium Risk"
wsRisk.Range("E2").Value = "Low Risk"

lastrow = wsThisSheet.Cells(Rows.Count, "B").End(xlUp).Row

For x = 2 To lastrow
    If wsThisSheet.Cells(x, 4).Value = "High" Then
    wsRisk.Range("A" & colHigh & ":B" & colHigh).Value = wsThisSheet.Range("B" & x & ":C" & x).Value
    colHigh = colHigh + 1
    ElseIf wsThisSheet.Cells(x, 4).Value = "Med" Then
    wsRisk.Range("C" & colMed & ":D" & colMed).Value = wsThisSheet.Range("B" & x & ":C" & x).Value
    colMed = colMed + 1
    ElseIf wsThisSheet.Cells(x, 4).Value = "Low" Then
    wsRisk.Range("E" & ColLow & ":F" & ColLow).Value = wsThisSheet.Range("B" & x & ":C" & x).Value
    ColLow = ColLow + 1
    End If
Next x

End Sub