2016-08-18 60 views
-1

我需要工作在不同的表像这样一个宏(让我们称之为XY和Z):检查是否值在不同的表已经存在,并且创造了成绩表将它们混合

x 
------------ 
a 0 
b 1 
c 2 

    y 
------------ 
a 3 
b 1 
c 2 

    z 
------------ 
a 3 
b 1 
c 0 

,我需要一个结果这样的桌子(表已经制作好了,只是填充):

x y z 
------------ 
a 0 1 2 
b 3 1 2 
c 3 1 0 

我需要什么?扫描表格时,将重复“a b和c”。我需要打印每个结果而不在该列中创建额外的一组数据。

我不想:

x y z 
------------ 
a 0  
b 1 
c 2  
a 3 
b 1 
c 2 
a  3 
b  1 
c  0 

我怎样才能做到这一点?

要在正确的行赋值我得到这个循环的当前行的控制:

Sub LoopRange() 

Dim rCell As Range 
Dim rRng As Range 

Set rRng = Hoja1.Range("B17:B30") 

For Each rCell In rRng.Cells 
    Debug.Print rCell.Address, rCell.Value 
    'MsgBox rCell.row' 

Next rCell 

End Sub 

我怎样才能提取的TE值(A,B和C),所以我可以比较字符串我的结果表?

最终的Excel结构应该是这样的:

Excel estructure

+0

为什么有'x'列是不是在你的输出数组'3's你输入的'x'表格? – pnuts

+0

我试过扫描范围并检查值是否存在,但没有工作,将用例子更新我原来的问题。 @pnuts数据是灵活的,在这种情况下仅仅是一个例子。 – Pablo

+0

易于使用数据透视表*,因为Q代表目前*。 – pnuts

回答

1

这看起来是为我工作。如果你逐步使用调试器,你将能够看到它在做什么,并修改它,以防我做出一些错误的假设。

我从名为Hoja1的工作表中提取数据。我写了一个名为xyz_result的工作表。我假定x,y和z列标题已经在B1:D1中。我还假设A,B,C行的名称均发生在A2:A4

Sub LoopRange() 

    Dim rCell As Range 
    Dim rRng As Range 
    Dim rRng2 As Range 

    Dim Wbk As Workbook 

    Set Wbk = ActiveWorkbook 


    Worksheets("Hoja1").Activate 
    Dim currentTable As String 
    Dim abcValue As String 
    Dim currentValue As String 

    Dim rowNum As Integer 
    Dim colNum As Integer 



    'Set rngMyRange = .Range(.Range("a1"), .Range("A65536").End(xlUp)) 
    Set rRng = Range("B17:B30") 

    For Each rCell In rRng.Cells 

     If Len(Trim(rCell.Offset(0, -1).Value)) = 0 Then ' It might be to the left of text such as x, y, or z 
      If Len(Trim(rCell.Value)) > 0 Then ' It is text such as x, y, or z 
       currentTable = rCell.Value 
      End If 
     End If 

     If Len(Trim(rCell.Offset(0, -1).Value)) > 0 Then 
      abcValue = rCell.Offset(0, -1).Value 
      currentValue = rCell.Value 
     End If 


     If Len(currentTable) > 0 And Len(abcValue) > 0 Then 
      Worksheets("xyz_Result").Activate 
      Set c = Range("A1:D4").Find(abcValue, LookIn:=xlValues) 
      rowNum = c.row 
      Set r = Range("A1:D4").Find(currentTable, LookIn:=xlValues) 
      colNum = r.Column 
      Cells(rowNum, colNum).Value = currentValue 
     End If 

     Worksheets("Hoja1").Activate 


    Next rCell 

End Sub 

enter image description here

+0

工作表示感谢。因为我可以根据需要调整它,所以我会在晚些时候接受答案。这里的主要问题是我需要宏在“xyz_Result”中手动编写“abc”,因为“abc”可以在将来是“abcdefg”(动态数据)。另外一个小的澄清,X,Y Z应该在不同的工作表,检查我原来的帖子,看看我的意思。 – Pablo

相关问题