2014-11-28 1 views
0

주어진 조건에 따라 워크 시트에서 문자열을 검색하는 매크로를 찾고 있습니다. 그리고 다른 워크 시트의 문자열과 일치하는 그림을 해당 텍스트에 붙여 넣습니다. 문자열이 발견되지 않으면 그 문자열을 검색하고 다음 문자열을 검색해야합니다. 내가 검색 문자열을하고 그것을 PDF 파일로 변환 할 필요가있는 것처럼. 여기 문자열을 검색하고 열려있는 다른 워크 시트의 문자열과 일치하는 매크로가 필요합니다.

는 ABC가 다음 문자열 XYZ로 검색을 이동 발견되지 않은 상기 실시 예에서의 샘플 코드

Sub EXCELTOPDF() 

    Dim strPath As String 
    Dim strFile, A As String 
    Dim NextRow As Long 

    strPath = "C:\Users\919944\desktop\xyz" 
    If Right(strPath, 1) <> "\" Then strPath = strPath & "\" 
    strFile = Dir(strPath & "*.xls", vbNormal) 
Do While strFile <> "" 

Workbooks.Open strPath & strFile 




    On Error Resume Next 

If (Cells.Find(What:="ABC", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
     MatchCase:=False, SearchFormat:=False).Activate) Then 
    ActiveCell.Value = ActiveCell.Value() 
    ActiveCell.Select 
    Windows("Image_S.xlsx").Activate 
    Cells.Find(What:="ABC", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
     MatchCase:=False, SearchFormat:=False).Activate 
    ActiveSheet.Shapes.Range(Array("Picture 123")).Select 
    Selection.Copy 
    Windows(strFile).Activate 
    ActiveCell.Offset(0, 1).Select 
    ActiveSheet.Paste 
End If 



    If (Cells.Find(What:="XYZ", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
     MatchCase:=False, SearchFormat:=False).Activate) Then 

    ActiveCell.Value = ActiveCell.Value() 
    ActiveCell.Select 
    Windows("Image_S.xlsx").Activate 
    Cells.Find(What:="XYZ", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
     MatchCase:=False, SearchFormat:=False).Activate 
    ActiveSheet.Shapes.Range(Array("Picture 638")).Select 
    Selection.Copy 
    Windows(strFile).Activate 
    ActiveCell.Offset(0, 1).Select 
    ActiveSheet.Paste 
End if 




If (Cells.Find(What:="EFGH", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
     MatchCase:=False, SearchFormat:=False).Activate) Then 
     On Error Resume Next 
    ActiveCell.Value = ActiveCell.Value() 
    ActiveCell.Select 
    Windows("Image_S.xlsx").Activate 
    Cells.Find(What:="EFGH", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
     MatchCase:=False, SearchFormat:=False).Activate 
    ActiveSheet.Shapes.Range(Array("Picture 24")).Select 
    Selection.Copy 
    Windows(strFile).Activate 
    ActiveCell.Offset(0, 1).Select 
    ActiveSheet.Paste 
End If 



If (Cells.Find(What:="PQRS", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
     MatchCase:=False, SearchFormat:=False).Activate) Then 
    ActiveCell.Value = ActiveCell.Value() 
    ActiveCell.Select 
    Windows("Image_S.xlsx").Activate 
    Cells.Find(What:="PQRS", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
     MatchCase:=False, SearchFormat:=False).Activate 
    ActiveSheet.Shapes.Range(Array("Picture 23")).Select 
    Selection.Copy 
    Windows(strFile).Activate 
    ActiveCell.Offset(0, 1).Select 
    ActiveSheet.Paste 
End If 

iPtr = InStrRev(ActiveWorkbook.FullName, ".") 
If iPtr = 0 Then 
    sFileName = ActiveWorkbook.FullName & ".pdf" 
    Else 
    sFileName = Left(ActiveWorkbook.FullName, iPtr - 1) & ".pdf" 
End If 

    sFileName = Application.GetSaveAsFilename(InitialFileName:=sFileName, fileFilter:="PDF Files (*.pdf), *.pdf") 

If sFileName = "False" Then Exit Sub 

    ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, filename:=sFileName, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False 
    strFile = Dir 
Loop 
End Sub 

이다. image_s는 해당 이름과 관련된 그림 목록이 들어있는 워크 시트입니다. 친절하게 필요한

+0

하이 pnuts을, 내 위의 코드에서 ABC 후 자동으로 검색 워크 시트에서 제시하고 사진을 수정 해 다음 활성 셀에 붙여 넣기합니다. ABC가 없으면 디버그 오류가 표시됩니다. 이제 "오류가 발생하면 다음 번에"코드를 삽입하십시오. 다른 검색으로 건너 뛰지 만 시트에있는 모든 이름과 관련된 그림을 선택하여 내 워크 시트에 붙여 넣으십시오. 불필요한 작업을 수행하십시오. – vicky

+0

두 번째'Find()'의 목적은 무엇입니까? 왜 그냥 선택하지 않아도 복사해야합니까? –

+0

"매크로 검색 중"이라고 말합니다. 그것은 당신이 실제로 어떤 프로그래밍을하고 있지 않다는 것을 의미합니까? 원하는 코드와 비슷한 코드를 찾고 원하는 사양으로 여기에 게시하고 다른 사람들에게 코드를 작성하도록 요청합니까? –

답변

0

은 컴파일하지만 테스트하지 않습니다

Sub EXCELTOPDF() 

    Dim strPath As String 
    Dim strFile, A As String 
    Dim NextRow As Long 
    Dim wb As Workbook, shtImg As Workbook 
    Dim f As Range 
    Dim arrFind, arrPic, i 

    'array of values to search for 
    arrFind = Array("ABC", "DEF", "GHI") 
    'array of corresponding shape names 
    arrPic = Array("Picture1", "Picture2", "Picture3") 

    'get a reference tothe sheet with the images 
    Set shtImg = Workbooks("Image_S.xlsx").Sheets("Images") 

    strPath = "C:\Users\919944\desktop\xyz" 
    If Right(strPath, 1) <> "\" Then strPath = strPath & "\" 

    strFile = Dir(strPath & "*.xls", vbNormal) 

    Do While strFile <> "" 

     'open the workbook and get a reference to it 
     Set wb = Workbooks.Open(strPath & strFile) 

     'loop over the array of values to search for 
     For i = LBound(arrFind) To UBound(arrFind) 

      Set f = wb.Sheets(1).Find(What:=arrFind(i), After:=ActiveCell, _ 
             LookIn:=xlFormulas, LookAt:=xlPart) 

      'test to see if value was found (f will not be Nothing) 
      If Not f Is Nothing Then 
       f.Value = f.Value 
       'copy required image... 
       shtImg.Shapes(arrPic(i)).Copy 
       f.Offset(0, 1).PasteSpecial 
      End If 

     Next i 

     'your export code here.... 

     strFile = Dir() 
    Loop 

End Sub 
+0

안녕하세요, Tim 님, 친절한 도움에 감사드립니다. 하지만 난이 줄에 오류가 발생했습니다 "설정 f = wb.Sheets (1) .Find (What : = arrFind (i), After : = ActiveCell, _ LookIn : = xlFormulas, LookAt : = xlPart)"친절하게 불필요한 – vicky

+0

무슨 오류입니까? 나는 짐작할 수 없다. ... –

+0

http://en.wikipedia.org/wiki/Do_the_needful –

관련 문제