2014-12-21 2 views
0

VBA 코딩을 처음 사용했습니다. 검색 한 값과 동일한 행에 셀을 복사하고 싶습니다.검색된 값 옆에 값을 복사하는 VBA 코드

나는 질려서 접속할 때 오류가 발생합니다. 예를 들어 내 xl 시트에서 "abcd"를 찾고 싶습니다. "abcd"가 셀 c12에서 발견되었다고 가정하면 셀 E12, F12에 값을 복사하려고합니다.

오류가 발생합니다. 오프셋 속성을 사용하려고합니다.

Private Sub FindingValues() 
    Dim val As Variant 
    val = InputBox(" Please Enter the value you want to search for") 
    Set c = Cells.Find(val, LookIn:=xlValues, MatchCase:=False) 
    If Not c Is Nothing Then 
    firstaddress = c.Address 
    Do 
    MsgBox "Value of val is found at " & c.Address 
    Set c = Cells.FindNext(c) 
    Loop While Not c Is Nothing And c.Address <> firstaddress 
    End If 

End Sub 

위의 코드에서 검색 한 셀의 주소를 가져올 수 있습니다. 행 셀 값 옆에있는 값을 찾고 싶습니다. 우리가 "ABCD는"세포 C12에서 발견되는 가정 해 봅시다 같은 , 나는, D12의 값에 원하는 E12 ..

답변

0

고려 :

Public Sub FindingValues() 
    Dim val As Variant 
    val = InputBox(" Please Enter the value you want to search for") 
    Set c = Cells.Find(val, LookIn:=xlValues, MatchCase:=False) 
    If Not c Is Nothing Then 
     firstaddress = c.Address 
     Do 
      MsgBox "Value of val is found at " & c.Address & vbCrLf & c.Offset(0, 1).Value & vbCrLf & c.Offset(0, 2).Value 
      Set c = Cells.FindNext(c) 
     Loop While Not c Is Nothing And c.Address <> firstaddress 
    End If 
End Sub 
0

첫째, 당신은 항상 option explicit와 함께 작동해야이 도움이 오류가 발생로 정보.

두 번째로 유형을 알고있는 경우 Variant을 사용하지 마십시오. 그러나 은이 규칙에 대한 예외는입니다. 그러나 간단한 프로그램의 경우에는이를 준수하는 것이 좋습니다.

코드에 - offset 함수를 사용할 수 있습니다. 첫 번째 인수는 행을 오프셋하고 두 번째 인수는 열을 오프셋합니다.

Option Explicit 

Private Sub FindingValues() 
    Dim val As Variant 
    Dim c As Range 
    Dim firstaddress As String 

    val = InputBox(" Please Enter the value you want to search for") 
    Set c = Cells.Find(val, LookIn:=xlValues, MatchCase:=False) 
    If Not c Is Nothing Then 
    firstaddress = c.Address 
    Do 
    MsgBox "Value of val is found at " & c.Address 
    MsgBox "Value found at +1 column offset is " & c.Offset(0, 1).Value 
    Set c = Cells.FindNext(c) 
    Loop While Not c Is Nothing And c.Address <> firstaddress 
    End If 

End 
시도
관련 문제