0

I'm working on this Excel report. It's a bit hard to explain without attaching an example here. So, I will try to put it in manually...Let's call this worksheet A:

ENV    SYSTEM    ACTION     CRQ
--------------------------------
PROD   RAC2       PATCH     12345
DEV    te2ds      STAGE     34672
DEV    te2ds      PATCH     34294
PROD   PRAC5      STAGE     47382
PROD   hq2/DBNAME PATCH     47389

etc....

That's just a small piece of the Excel table that I have. And there is another worksheet X:

SYSTEM   DBNumber
--------------------
RAC2       30
te2ds      10
PRAC5      12
hq2        3 

etc...

So, what I'm trying to accomplish is, I want some sort of a code (preferably, excel formula..which I doubt helps much in this case) to automatically 'Go in --> Read the cell from Worksheet A, SYSTEM column --> find that name from Worksheet X --> put the relative number under DBNumber in to a cell under a new Worksheet( or a worksheet that I specify), next to its right SYSTEM name. --> If that SYSTEM name/number was already copied over then skip that cell. Does it make sense? Thank for all the inputs! I really need some help on this. :)

4

2 回答 2

2

以下应该适合您:

  1. 首先浏览工作表A中的“系统”列列表
  2. 创建唯一系统名称的数组
  3. 在工作表 x 中查找值
  4. 复制 Sheet z 中的相对值

您可以使用以下代码:

Sub unique()

      Dim systemNameCollection As New Collection
      Dim systemNames() As Variant
      Dim i As Long
      Dim rRng As Range

      'Define the range  and assign Values to Array

      ThisWorkbook.Sheets("Sheet1").Select
      Set rRng = ThisWorkbook.Sheets("Sheet1").Range("A2", Range("A65536").End(xlUp))
      systemNames() = rRng.Value

      On Error Resume Next

      'Add values in Collection

      For Each a In systemNames
         systemNameCollection.Add a, a
      Next

      'run following code to take the unique value from Sheet1 - Lookup for value in Sheet2 
      'and copy relative number in sheet2 to sheet3

      ThisWorkbook.Sheets("Sheet2").Select
      Set rRng = Range("A2", Range("A65536").End(xlUp))

      ThisWorkbook.Sheets("Sheet3").Select

      For i = 1 To systemNameCollection.Count
          For Each Value In rRng
            If systemNameCollection(i) = Value Then
                Range("A65536").End(xlUp).Select
                ActiveCell.Offset(1, 0).Value = systemNameCollection(i)
                ActiveCell.Offset(1, 1).Value = Value(1, 2)
                Exit For
            End If
          Next
      Next    
End Sub
于 2013-05-16T10:42:53.413 回答
0

由于您需要数字的唯一系统名称在 SheetX 中,因此这是限制因素。您可以将SheetX中的整个System列复制到一个新的工作表中,然后使用VLOOKUP函数结合IF语句来获取相应的数字。

例子:

  1. 复制 SheetX,系统列并粘贴到新的 SheetY
  2. 在 SheetY、System 列的右侧,添加另一个名为 Number 的列。
  3. 将 SheetY 中的信息设为表格(在此示例中假设为 Table1)。
  4. 对于 SheetY, Number 列中的每个单元格,插入公式:

=IF(ISNA(VLOOKUP(Table1[System],SheetA!$B,1,False)),"",VLOOKUP(Table1[System],SheetX!$A:$B,2,False))

这个公式说...

  • 首先查看SheetY中的System值是否在SheetA的列表中。
  • 如果不是,则“跳过”该单元格。
  • 如果是,则从 SheetX 中查找对应的编号。

最后一步:如果您担心 SheetY 中存在重复的键值对,请执行步骤 1-4,然后删除 SheetY 中数据的重复项。

于 2015-11-10T01:42:26.400 回答