2015-04-22 25 views
0
Source Port # Source Port Description Source port Pwwn   Switch Port  Switch Port Description Flolgi port Flogi pwwn 
fc1/1 Test_port_description_1 21:00:00:00:12:34:56:78   fc1/1 Test_port_description_1  
fc1/2 Test_port_description_2 21:00:00:00:12:34:56:79   fc1/2 Test_port_description_2 fc1/0,abde 21:00:00:00:12:34:56:78 
fc1/3 Test_port_description_3 21:00:00:00:12:34:56:80   fc1/3 Test_port_description fc1/2,abde 21:00:00:00:12:34:56:79 
fc1/4 Test_port_description_4 21:00:00:00:12:34:56:81   fc1/4 Test_port_description_4 fc1/1,abde 21:00:00:00:12:34:56:80 
fc1/5 Test_port_description_5 21:00:00:00:12:34:56:82   fc1/5 Test_port_description_5 fc1/4,abde 21:00:00:00:12:34:56:81 
          fc1/5,abde 21:00:00:00:12:34:56:82 

下面是我的輸入文件中的數據示例。我有一個帶有命令按鈕的Excel工作表,點擊它將激活有我的輸入的工作表2。在同一工作表中查找單元格值多次並找到值旁邊的值

A列是我的來源。我想檢查該列中的每個元素(A1,A2等),並檢查它是否在工作表中找到。如果找到了,我需要檢查選擇下一個單元格值並將其與下一個找到的單元格值進行比較。如果它們都相同,則應將A2,B2值都複製到sheet3。
我必須在sheet2中再次搜索,看看它是否被找到,那麼這次我需要檢查當前元素右側的2個單元格。將它們複製到sheet3。

例如。
A2 = fc1/1,B2 = Test_port_description_1,c2 = 21:00:00:00:12:34:56:7
首先,我想要選擇A2值並嘗試在表格中找到它。
當在f2 = fc1/1中發現A2 = fc1/1時,現在我需要檢查是否 B2值等於G2值。如果它們都相同,則將A2和B2值同時複製到sheet3。

我再次想要繼續搜索A2 = fc1/1 H5 = fc1/1,abde。現在我需要檢查C2 = 21:00:00:00:12:34:56:78是否與H6 = 21:00:00:00:12:34:56:78相同,然後複製值C3下一列,其中值被發現,如果沒有找到給定的顏色編碼的細胞..像紅色爲第二列和黃在表3 3column ...

回答

0
Option Explicit 
Private Sub FindingPortPwwn_Click() 

Dim SourcePort As Range 
Dim firstaddress As String 
Dim i As Long, j As Long, N As Long 
Dim counter As Integer 
Dim WorkingSheet As Worksheet 

'Stopping Application Alerts 
Application.DisplayAlerts = False 

'OR You can mention the Sheet name 
On Error GoTo SheetNotFound: 
Sheets("Final Results Sheet").Delete 

'Creating a New Results Worksheet. 
ThisWorkbook.Worksheets.Add(After:=Worksheets("Input_Worksheet")).Name = "Final Results Sheet" 



'Enabling Application alerts once we are done with our task 
Application.DisplayAlerts = True 




Set WorkingSheet = ActiveWorkbook.Worksheets(2) 
WorkingSheet.Activate 
N = WorkingSheet.Cells(Rows.Count, "A").End(xlUp).Row 

'MsgBox (N) 
For i = 1 To N 
    'MsgBox (WorkingSheet.Cells(i, "A").Value) 
    'MsgBox (WorkingSheet.Cells(i, "A").Offset(0, 1).Value) 
    Set SourcePort = WorkingSheet.Cells.Find(WorkingSheet.Cells(i, "A").Value, WorkingSheet.Cells(i, "A"), LookIn:=xlValues) 
    'MsgBox (SourcePort.Address) 
    firstaddress = SourcePort.Address 
    Do 
    If (InStr(SourcePort.Value, ",")) Then 
     If WorkingSheet.Cells(i, "A").Offset(0, 2).Value = SourcePort.Offset(0, 1).Value Then 
      ThisWorkbook.Worksheets(3).Cells(i, "A").Value = WorkingSheet.Cells(i, "A").Value 
      ThisWorkbook.Worksheets(3).Cells(i, "A").Offset(0, 2) = WorkingSheet.Cells(i, "A").Offset(0, 2).Value 

     Else 
      ThisWorkbook.Worksheets(3).Cells(i, "A").Value = WorkingSheet.Cells(i, "A").Value 
      ThisWorkbook.Worksheets(3).Cells(i, "A").Offset(0, 2).Value = WorkingSheet.Cells(i, "A").Offset(0, 2).Value 
      ThisWorkbook.Worksheets(3).Cells(i, "A").Offset(0, 2).Interior.Color = RGB(255, 126, 135) 
     End If 


    Else 
     If WorkingSheet.Cells(i, "A").Offset(0, 1).Value = SourcePort.Offset(0, 1).Value Then 
     ThisWorkbook.Worksheets(3).Cells(i, "A").Value = WorkingSheet.Cells(i, "A").Value 
     ThisWorkbook.Worksheets(3).Cells(i, "A").Offset(0, 1) = WorkingSheet.Cells(i, "A").Offset(0, 1).Value 

     Else 
     ThisWorkbook.Worksheets(3).Cells(i, "A").Value = WorkingSheet.Cells(i, "A").Value 
     ThisWorkbook.Worksheets(3).Cells(i, "A").Offset(0, 1).Value = WorkingSheet.Cells(i, "A").Offset(0, 1).Value 
     ThisWorkbook.Worksheets(3).Cells(i, "A").Offset(0, 1).Interior.Color = RGB(255, 0, 0) 
     End If 
    End If 

    Set SourcePort = WorkingSheet.Cells.FindNext(SourcePort) 
    Loop While Not SourcePort Is Nothing And firstaddress <> SourcePort.Address 
    MsgBox (SourcePort.Value) 
Next i 
    Exit Sub 
    SheetNotFound: 
     MsgBox (" Final Results Worksheet not Found") 
    End Sub 
+0

我能發送正確的數據到Sheet,但是我無法爲未找到的值設置顏色代碼。我無法根據邏輯改變顏色。 – pradeep

相關問題