2017-06-21 209 views
0

我在兩個工作簿之間工作。
XL1是這樣在另一個範圍內查找範圍條件

StudentID |From |To 
1   |2 |9 
2   |20 |50 
3   |0 |1 

XL2日常工作簿是另一個工作簿是這樣的:

From| To  
0 | 1.5 'Associate 1 with this as an ID 
2 | 15 'Associate 2 with this as an ID and so on 

我試圖寫在XL1代碼,將讓我

  1. select xl2 workbook

  2. 看0
  3. 查找列從和到

  4. 入住XL1工作簿如果從和爲每位學生中落在和向XL2範圍,然後一個ID 關聯。要清楚(是這樣):

StudentID |From |To |ID 
1   |2 |9 |2 
2   |20 |50 | 
3   |0 |1 |1 

到目前爲止,我已經寫了這個代碼,但我似乎無法弄清楚如何獲得邏輯:

Sub getID() 
Dim wb As Workbook 
Dim ws As Worksheet 
Dim fd As FileDialog 
Dim filename As String 
Dim rng As Integer 
Dim counter As Integer 
Dim frm As Range 
Dim too As Range 
Dim lngCount As Integer 
Set fd = Application.FileDialog(msoFileDialogFilePicker) 
Application.ScreenUpdating = False 
Application.DisplayAlerts = False 


With fd 

If .Show Then 
     FileName = .SelectedItems(1) 
    Else 
     'if user pressed CANCEL - exit sub 
     MsgBox "User pressed CANCEL" 
     Exit Sub 
    End If 
       On Error Resume Next 
       Set wb = Workbooks.Open(FileName) 
       rng = ActiveSheet.UsedRange.Rows.Count 
       frm = ActiveSheet.Range("AA" & rng).Select 'copy from col 
       too = ActiveSheet.Rang("AC" & rng).Select 'copy to col 
       For Each Cell In frm 
        if() 
       Next Cell 

      Next 
End If 
End With 


End Sub 

的例子基本上意味着

xl2.from<xl1.from<xl2.to 

xl2.from<xl1.to<xl2.to 

我希望在得到這個工作

+0

爲什麼sumifs不起作用? – Jeeped

+0

所以for循環中的部分實際上使0有意義。在每本工作簿中是否有多個工作簿和多個工作簿都需要通過?另外我不明白這個例子如何匹配?只有從需要匹配嗎? – UGP

+0

涉及兩個工作簿,每個工作簿都有一個工作表。這個例子基本上意味着xl2.from SQLserving

回答

1

幫助,所以這與第二個文件的第一個文件中的每個數據集進行比較。在D列中給出匹配的ID。你沒有提到任何關於多個匹配的內容,所以它會把所有匹配放在單元格中,並用「;」分隔它們。

Sub getID() 
Dim wb As Workbook 
Dim sht As Worksheet, sht2 As Worksheet 
Dim fd As FileDialog 
Dim lRow As Long, lRow2 as Long 
Dim i as Integer, j as Integer 

Application.ScreenUpdating = False 
Application.DisplayAlerts = False 

Set fd = Application.FileDialog(msoFileDialogFilePicker) 
Set sht = ActiveWorkbook.ActiveSheet 

With fd 
    .AllowMultiSelect = False 
    .Filters.Add "Excel", "*.xl*" 
End With 

If fd.Show = -1 Then 

    Set wb = Workbooks.Open(fd.SelectedItems(1)) 
    Set sht2 = wb.Worksheets(1) 'First Sheet in File 

    lRow = sht.Cells(sht.Rows.Count, 2).End(xlUp).Row 
    LRow2 = sht2.Cells(sht2.Rows.Count, 1).End(xlUp).Row 

    sht.Columns(4).ClearContents 'Clear Old Data in Column "D" 
    sht.Cells(1, 4).Value = "ID" 'Title of Col 

    For i = 2 To lRow 
     For j = 2 To LRow2 
      If sht.Cells(i, 2).Value >= sht2.Cells(j, 1).Value _ 
      And sht.Cells(i, 3).Value <= sht2.Cells(j, 2).Value Then 'Checks if From and To are in Range 
       If sht.Cells(i, 4).Value <> "" Then 'if more than one ID 
        sht.Cells(i, 4).Value = sht.Cells(i, 4).Value & ";" & j - 1 'Seperate ID with ";" ID 
       Else 
        sht.Cells(i, 4).Value = j - 1 'ID 
       End If 
      End If 
     Next j 
    Next i 

    wb.Close 
End If 
End Sub 

第一個文件看起來是這個結果:

enter image description here

第二個文件是這樣的:

enter image description here

(注:我用的是德國的版本,所以有是「,」爲小數而不是「。」)