2016-06-30 15 views
0

我正在尋找一個循環代碼,爲我提供了一個「名稱」的第一和最後一行在列A迴路的決定列不同的進入的第一個和最後行A

A 
1 Phill 
2 Phill 
3 Phill 
4 Phill 
5 Phill 
6 Phill 
7 Matthew 
8 Matthew 
9 Matthew 
10 Matthew 
11 Hendry 
12 Hendry 
13 Hendry 
etc. etc. 

的結果應該是這樣的其他表:

A   B   C 
1 Name  Start_Row End_Row 
2 Phill  1   6 
3 Matthew  7   10 
4 Hendry  11   13  
5 etc.  etc.  etc.   

我嘗試了不同的循環,但似乎無法得到它讓我開始了良好的循環代碼。 這是我有:

If wsData.Cells(i + DOF, 1) <> curName Then 

wbMain.Activate 

For i = 1 To LastRow 
curName = wsData.Cells(i + DOF, 1).Value 
NameCount = NameCount + 1 
wbWellsTable.Sheets("Sheet1").Cells(NameCount + 1, 1) = wbMain.Sheets("Data").Rows(i + DOF) 
Start_Row = wsData.Cells(i + DOF, 1).Value 
Counter = Counter + 1 
wbWellsTable.Sheets("Sheet1").Cells(Counter + 1, 2) = wbMain.Sheets("Data").Rows(i + DOF) 
End_Row = wsData.Cells(i + DOF, 1).Value 
Bounter = Bounter + 1 
wbWellsTable.Sheets("Sheet1").Cells(Bounter + 1, 3) = wbMain.Sheets("Data").Rows(i + DOF) 
Next i 
End If 

希望你們能幫幫我!

+0

這將有可能公式,即'= MIN(IF(A1:A100 = 「菲爾」,行(A1:A100))'爲另一個是數組公式和Max。 –

+0

@Nathan_Sav:我不是很熟悉vba,因爲你可以看到,但我需要一個循環方法,因爲我想用更多的變量來擴展這個循環,這些變量來自B列.....這意味着我可以更容易地展開循環。 – Kickk05

+0

嗨,沒有真正需要的VBA,你可以有一個公式來獲得獨特的名字,然後找到最小和最大公式,在數組中你可以添加儘可能多的標準,只要你喜歡 –

回答

0

用VBA:

Option Explicit 

Sub rowfinder() 

Dim ws As Worksheet 
Dim rng As Range 
Dim cell As Range 
Dim currentName As String 
Dim currentMin As Integer 
Dim startRow As Integer 
Dim startColumn As Integer 
Dim outputColumn As Integer 
Dim outputRow As Integer 

Set ws = ThisWorkbook.Worksheets(1) 
startRow = 2 
startColumn = 1 
outputColumn = 2 
outputRow = 2 
ws.Cells(startRow + 1, startColumn).End(xlDown).Select 


Set rng = ws.Range(ws.Cells(startRow + 1, startColumn), ws.Cells(startRow + 1, startColumn).End(xlDown)) 
currentName = ws.Cells(startRow, startColumn).Value 
currentMin = Cells(startRow, startColumn).Row 
ws.Cells(outputRow, outputColumn).Value = currentName 
ws.Cells(outputRow, outputColumn + 1).Value = currentMin 
For Each cell In rng 

    If cell.Value <> currentName Then 

     ws.Cells(outputRow, outputColumn + 2).Value = cell.Row - 1 
     currentName = cell.Value 
     currentMin = cell.Row 
     outputRow = outputRow + 1 
     ws.Cells(outputRow, outputColumn).Value = currentName 
     ws.Cells(outputRow, outputColumn + 1).Value = currentMin 

    End If 

Next cell 
Set cell = rng.End(xlDown) 
ws.Cells(outputRow, outputColumn + 2).Value = cell.Row 

End Sub 

Output example

+0

我剛剛試過一個新的工作簿,它工作正常。順便說一句,您可以刪除該行,我當時只是測試一些東西,它什麼都不做。 你在模塊或工作表中有宏嗎?嘗試把它放在一個模塊中。它雖然適用於我。 –

1

我不打算寫爲輸出等整個代碼,但這裏有一個很好的通用函數返回第一&最後一行爲您提供:

Function FindRow(sht As Worksheet, Col As String, str As String, Direction As Long) As Long 
     FindRow = sht.Columns(Col).Cells.Find(str, SearchOrder:=xlByRows, LookIn:=xlFormulas, SearchDirection:=Direction).Row 
End Function 

你可以把它在你的常規子像這樣/功能:

Dim FirstRow As Long, LastRow As Long 
FirstRow = FindRow(sht:=YourWorkSheetObject, Col:="A", str:="Text To Find", Direction:=xlNext) 
LastRow = FindRow(sht:=YourWorkSheetObject, Col:="A", str:="Text To Find", Direction:=xlPrevious) 

根據不同的方向,它只是返回指定列,你想要的文字匹配的第一個或最後一個行的行數。有了這些值,你應該能夠將它們分解到代碼的其餘部分。

1

沒有VBA,請在B列中填寫名稱。在C1輸入:

=MATCH(B1,A:A,0) 

,並複製下來,並在D1輸入:

=LOOKUP(2,1/(A:A=B1),ROW(A:A)) 

抄下:

enter image description here

0

使用工作表名稱

Dim wsData as Worksheet 
Dim wsMain as Worksheet 

Set wsData = wbMain.Sheets("Data") 
Set wsMain = wwbWellsTable.Sheets("Sheet1") 

' Get first value 
i = 1 
lastName = wsData.Cells(i, 1).Value 

i = i + 1 
curName = wsData.Cells(i, 1).Value 
startRow = i 

NameCount = 1 

Do until curName = "" 
    if curName <> lastName then 
     With wksMain 
      NameCount = NameCount + 1 ' increment row to skip first header line 
      .Cells(NameCount, 1) = lastName 
      .Cells(NameCount, 2) = startRow 
      .Cells(NameCount, 3) = i - 1 ' last Row 
     End With 
     lastName = curName 
     startRow = i 
    endif 

    i = i + 1 
    curName = wsData.Cells(i, 1).Value 
Loop 

' Write out lst record 
With wksMain 
    NameCount = NameCount + 1 
    .Cells(NameCount, 1) = lastName 
    .Cells(NameCount, 2) = startRow 
    .Cells(NameCount, 3) = i - 1 ' last Row 
End With 
+0

我已經試過了這段代碼,但它只爲列表中的姓氏做了工作。 – Kickk05

+0

你把它放在工作簿或模塊中的一個子文件夾中嗎?沒有錯誤?它是否編譯?最終輸出是什麼樣的? – dbmitch

相關問題