2017-09-05 96 views
0

有点背景,我的表“数据”包含一个表格,我的宏应该填充该表格。该表的日期包含第一列(P列),以及一些名称作为标题。我的当前宏,如下所示,循环遍历我的所有页面,除了指定不循环的页面,然后在每个页面中循环遍历W7:W200范围内的每个单元格。然后,它将单元格中右侧的10个值与表单“Data”中P列中的日期相匹配(并将该行设置为HdrRow)。同时,为了将该值匹配到表“数据”中的列标题(并将该列设置为HdrCol),它会在它正在循环的任何表中查找A9中的值。在找到行和列(相交单元)后,宏然后将它正在循环的单元的值粘贴到相交单元中。Excel VBA根据两栏中的条件找到行号,没有循环

我遇到了这个下一部分的麻烦,我正在寻找添加另一个查找行的标准。我希望宏不仅能够在列P中找到匹配的日期,而且还可以在列Q中找到与其循环中的任何一个表中的值A1匹配的值;然后将该行设置为HdrRow。如果可能的话,id喜欢不使用循环。

Sub Values() 
    Dim HdrCol As Range 
    Dim Site As String 
    Dim SearchRange As Range 
    Dim HdrRow As Range 
    Dim FinDate As Date 
    Dim ws As Worksheet 
    Dim rng As Range 




    ' Fill in Actual Value 
    Sheets("Data").Range("W2:W100000").ClearContents 

    For Each ws In ActiveWorkbook.Worksheets 
     'Dont Copy Data from these worksheets 
     If ws.Name <> "Portfolio" And ws.Name <> "Master" And ws.Name <> "Template" _ 
      And ws.Name <> "Coal" And ws.Name <> "E&P" And ws.Name <> "Gen" _ 
      And ws.Name <> "Hydro" And ws.Name <> "LNG" And ws.Name <> "Midstream" _ 
      And ws.Name <> "Solar" And ws.Name <> "Transmission" _ 
      And ws.Name <> "Wind" And ws.Name <> "Data" Then 

      For Each cell In ws.Range("W7:W200") 

       If cell <> "   " Then 

        Site = ws.Range("A9").Value 
        FinDate = Right(cell, 10) 

        'Find column ref 
        Set HdrCol = Sheets("Data").Range("P1:W1").find(Site, lookat:=xlPart) 
        If Not HdrCol Is Nothing Then 
        End If 

        'Find row ref 
        Set SearchRange = Sheets("Data").Range("P1", Range("P100000").End(xlUp)) 
        Set HdrRow = SearchRange.find(FinDate, LookIn:=xlValues, lookat:=xlWhole) 


        Application.Goto Reference:=Cells(HdrRow.Row, HdrCol.Column) 

        If IsEmpty(Sheets("Data").Cells(HdrRow.Row, HdrCol.Column)) Then 
         cell.Copy Sheets("Data").Cells(HdrRow.Row, HdrCol.Column) 
        Else 
         cell.Copy Sheets("Data").Cells(HdrRow.Row, HdrCol.Column).End(xlDown).Offset(1, 0) 
        End If 
       End If 
      Next 
     End If 
    Next 
End Sub 

回答

0

我首先想到的非环版本要做到这一点(循环简单得多),是使用匹配(),但如果你有多个值使用A = Q或同日在那里,你可能会遇到一个问题。

Dim i,j as Integer 

i=Application.Match(RefCell1,LookUp1,0).Row 
j=Application.Match(RefCell2,LookUp2,0).Row 

If i=j Then 
    HdrRow=i 
    Else 
End If 

我特别没有使那个匹配场景的If语句条件更容易阅读和编辑。

使用这种方法,您会遇到存在多个相同值的问题。


另一种方法是使用嵌套的if语句:

Dim i as integer 

i=Application.Match(RefCell1,LookUp1,0).Row 

If Application.IfError(i,0)>0 Then 
    If Cells(i,"Q").Value=Cells(RefCell1Row,"A").Value 
     HdrRow=i 
     Else 
    End If 
    Else 
End If 

最后,我还是希望推荐一个循环,这样就可以评估每行线,这将建立在第二种方法。


编辑:每个请求,包括一个循环。

Dim i, j as Integer 

For i = 7 to 200 'Used the range you mentioned in your post, which I think is wrong for this example... these are row numbers for Data sheet 
    For j = 7 to 200 'Row numbers for reference sheets 
     If Sheet(ARRAY).Cells(j,"Q").Value=Sheets("Data").Cells(i,"A").Value Then 
      If Cells(j,"P").Value=Cells(i,"B").Value 'Not sure what column the date is in Data sheet 
       HdrRow=j 
       Else 
      End If 
      Else 
     End If 
    Next j 
Next i 

结束是两个循环,考虑到两个数据表的单元格,每一页你在阵列中引用。确保关闭屏幕更新,因为癫痫是真实的!

+0

使用这种方法,其中HdrRow只是一个数字,您将能够使所有对HdrRow.Row的引用仅仅是Cells()中的HdrRow。 – Cyril

+0

你说得对,我认为最后一个循环将是最好的方法。你能否将你的解决方案包含在循环中? – Edp

+0

@Edp更新;希望这会让你得到你需要的地方! – Cyril