2012-06-29 2 views
0

좋아요, 그래서 Excel에서 userform을 가지고 있고, 폼에 "IMEITextBox"라는 텍스트 박스가 있습니다. Coloum B에 IMEI 번호가있는 재고 목록이 있습니다. IMEITextBox에 IMEI 번호를 입력 할 때 저장할 때 "Inventory"시트에서 해당 IMEI 번호를 포함하는 행을 삭제하려고합니다. 나는 이것을 며칠 동안 계속 연구했다. 캔트는 저에게 효과가있는 것을 찾는 것 같습니다. 도와 주실 수 있습니까? 코드를보고 난 후에 그것을 ... 이해에 문제가되지 않도록값이 양식의 텍스트 상자 값과 일치하면 시트에서 행을 삭제 하시겠습니까?

Sub DeleteRows(IMEI) 

Dim ws As Worksheet 
Dim lastRow As Long, i As Long 
Dim strSearch As String 
Dim aCell As Range 

On Error GoTo Err 
Set ws = Sheets("Inventory") 
lastRow = ws.Range("IMEIRange" & Rows.Count).End(xlUp).Row 
strSearch = IMEITextBox.Value 
Set aCell = ws.Range("IMEIRange" & lastRow).Find(What:=strSearch, LookIn:=xlValues, _ 
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
MatchCase:=False, SearchFormat:=False) 
If Not aCell Is Nothing Then 
    ws.Rows(lastRow).Delete 
End If 
Exit Sub 
Err: 
MsgBox Err.Description 

End Sub 
+0

사용'.Find 'Coloum B에서 IMEI 번호를 찾고 발견되면 간단히 삭제하십시오. 이 링크의 섹션 1을 참조하십시오. http://siddharthrout.wordpress.com/2011/07/14/find-and-findnext-in-excel-vba/ –

+0

.find를 사용해야 함을 알고 있습니다. VB에서 프로그램하지 않으므로 코드를 제공 할 수 있습니까? – Jason

+0

위의 링크를 확인하십시오. .Find에 대한 코드 + 전체 자습서가 있습니다. –

답변

1

내가 코드를 댓글을 달았, 당신은 당신이 매우 가까웠다 것을 알게 될 것이다)

Option Explicit 

Sub DeleteRows() 
    Dim ws As Worksheet 
    Dim strSearch As String 
    Dim aCell As Range 

    On Error GoTo Err 

    '~~> Set the sheet where you want to search the IMEI 
    Set ws = Sheets("Inventory") 

    With ws 
     '~~> Get the value which you want to search 
     strSearch = IMEITextBox.Value 

     '~~> Column A is Column 1 so Column B is 2. This is where we are searching 
     '~~> xlWhole is used in the code below so that we find a complete match 
     '~~> xlPart is supposed to be used when you are finding a partial match. 
     Set aCell = .Columns(2).Find(What:=strSearch, LookIn:=xlValues, _ 
     LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
     MatchCase:=False, SearchFormat:=False) 

     '~~> Check if we found the value 
     If Not aCell Is Nothing Then 
      '~~> get the row of the cell where we found the match and delete it 
      .Rows(aCell.Row).Delete 
     Else '<~~ If not found 
      MsgBox "IMEI Number not Found" 
     End If 
    End With 

    Exit Sub 
Err: 
    MsgBox Err.Description 
End Sub 
+0

@ Jason : 'IMEirange' 범위를 구체적으로 검색하려면'.Columns (2) .'를'.Range ("IMEIRange")로 변경하십시오.' '' –

+0

그래, 모든 것을 붙여 넣고 사용했습니다. 범위 ("IMEIRange"). 필자는 "Option Explicit"을 코드 위에 놓았습니다. 코드가 선 위로 이동하고 다른 코드에 영향을줍니다. 오류가 발생하지 않으며 IMIE "12345"가있는 행을 테스트로 삭제하지 않습니다. – Jason

+0

'Option Explicit'은 코드 창의 맨 위에 있어야합니다. 위의 코드를 실행하면 "IMEI 번호를 찾을 수 없습니다"라는 메시지 상자가 나타 납니까? –

관련 문제