땡큐엑셀vba & 엑셀매크로

data 시트

 

 

5월 검색

 

핸드폰 검색

 

'http://kin.naver.com/qna/detail.nhn?d1id=1&dirId=102020101&docId=281810490
'아래처럼 'data' 시트에 자료가 있고 '조회'시트에서 검색을 하면 검색조건에 맞는 결과를 출력하는걸 만드려고 합니다. 어떻게 만들어야할지 모르겠어......엑셀가계부로 검색해봤는데 너무 어려워서 좀 도와주세요.


' AdvancedFilter,WorksheetFunction.Sum
'
' source by 땡큐엑셀vba & 엑셀매크로
'
Sub 고급필터()
'

    '결과영역 삭제
    Range("A10").CurrentRegion.Clear
    
    
    'data시트 A:I열에서 조건에 맞는 값을 찾아 A10부터 출력한다
    Sheets("data").Columns("A:I").AdvancedFilter Action:=xlFilterCopy, _
        CriteriaRange:=Range("A1:I2"), CopyToRange:=Range("A10"), Unique:=False
        
        
 
    Set 마지막셀 = Cells(Rows.Count, "E").End(xlUp)
    
    '누적금액
    '마지막셀 다음행에 누적금액 출력
    마지막셀.Offset(1, 0) = Application.WorksheetFunction.Sum(Range("E11:E" & 마지막셀.Row))
    
    
    MsgBox "완료되었습니다", vbInformation, "땡큐엑셀vba & 엑셀매크로"
    
End Sub


 

가계부-고급필터사용하여 데이터검색-AdvancedFilter,WorksheetFunction.Sum-땡큐엑셀vba.xlsm
0.05MB

 

 

실행전

 

실행후

 

'http://kin.naver.com/qna/detail.nhn?d1id=1&dirId=102020101&docId=286888397

'엑셀에서 그림과 같이 특정 데이터를 다른 필드로 이동시키고 싶은데
'고수님들의 조언 부탁드립니다


'[시트소스]
'
'Source by 땡큐엑셀vba & 엑셀매크로
'
Sub 값추출()
   
    
    For Each ea In Selection
    
        추출값1 = thankqStatus추출(ea.Value)
        추출값2 = thankqUserNam추출(ea.Value)
    
        ea.Offset(0, 1) = 추출값1
        ea.Offset(0, 2) = 추출값2
            
    Next
    
 
    MsgBox "완료되었습니다.", vbInformation, "땡큐엑셀vba & 엑셀매크로"
    
End Sub








'정규식을 사용하여
'Status추출
Function thankqStatus추출(param1 As String)
    Dim strPattern As String: strPattern = "<STATUS>(.+)</STATUS>"
    
    Dim rex As Object
    Dim obj
    
    
    Set rex = CreateObject("vbscript.regexp")
    
    With rex
        .Pattern = strPattern
        .Global = True
        .ignorecase = True
    End With
    
    'If rex.test(param1) = True Then
        'thankqStatus추출 = rex.Execute(param1)(0)
    'End If


    If rex.test(param1) Then
        Set matches = rex.Execute(param1)
        GetStringInParens = matches(0).SubMatches(0)
    End If
    
    
    thankqStatus추출 = GetStringInParens

End Function


'정규식을 사용하여
'UserNams추출
Function thankqUserNam추출(param1 As String)
    Dim strPattern As String: strPattern = "<USERNAM>(.+)</USERNAM>"
    
    Dim rex As Object
    
    Set rex = CreateObject("vbscript.regexp")
    
    With rex
        .Pattern = strPattern
        .Global = True
        .ignorecase = True
    End With
    
    'If rex.test(param1) = True Then
    '    thankqUserNam추출 = rex.Execute(param1)(0)
    'End If

    If rex.test(param1) Then
        Set matches = rex.Execute(param1)
        GetStringInParens = matches(0).SubMatches(0)
    End If
    
    thankqUserNam추출 = GetStringInParens

End Function

정규식-문자추출-regexp-땡큐엑셀vba.xlsm
0.40MB

 

 

5월, 6월 시트

 

검색시트

 

검색 결과

 

'http://kin.naver.com/qna/detail.nhn?d1id=1&dirId=102020101&docId=277652259
'
'각기 다른셀에 있는 내용 중 마지막셀에
'빌린날짜와 차량이 중복되는 사람이 있는지 없는지 확인하는 방법좀 알려주세요 ㅠㅠㅠㅠㅠ
'그날짜에 그 차량에 누가 빌렸는지를 알고싶어요 서류정리를 해야하는데, 미치겠어요
'고수님들 도와주세요



'해당월 시트에서 차번호를 검색하여 이름찾기
'
'source by 땡큐엑셀vba & 엑셀매크로
'
Sub 찾기()

    마지막행 = Cells(Cells.Rows.Count, "A").End(xlUp).Row
    
    
    '마지막행까지 돌면서 처리
    For i = 2 To 마지막행
        월 = Month(Cells(i, "A")) & "월"
        차번호 = Cells(i, "B")
        날짜 = Cells(i, "A")
        
        
        '해당 월시트의 차번호에서
        Set 범위 = Sheets(월).Range("B:B")
        
              
         '1.검색
         Set Rng = 범위.Find(what:=차번호, lookat:=xlWhole)
    
        
         If Not Rng Is Nothing Then '검색결과가 있다면
            strAddr = Rng.Address  '첫 위치 주소
            
            '2.검색결과가 없을때 까지 루프
            Do
                '있으면 이름출력
                If 날짜 = Rng.Offset(0, -1) Then
                    Cells(i, "C") = Rng.Offset(0, 1)
                End If
                
                Set Rng = 범위.FindNext(Rng) 'Next
            Loop While Not Rng Is Nothing And Rng.Address <> strAddr
         End If
         
         
    Next i

    
    MsgBox "완료되었습니다.", vbInformation, "땡큐엑셀vba & 엑셀매크로"

End Sub

날짜와번호가 같은사람찾기-find_findnext-땡큐엑셀vba.xlsm
0.05MB

 

전체보기

 

업체명칭으로 검색
품명으로 검색

 

업체명칭과 품명으로 검색

'http://kin.naver.com/qna/detail.nhn?d1id=11&dirId=1128&docId=279404024
'
'회사에서 사용하는 프로그램을 자체적인 문제로 인해 더이상 사용할수 없게되어...여차여차 하여
'여튼 네이버를 뒤지다가 VBA를 통해서  엑셀 내에서 검색이 가능한로 확인하였습니다.
'근게 도통 알수가 없네요.... 혹시 초고수님들은 쉽게 하시지 않을까해서
'도움 요청 드립니다.

'대략 파일은 이러한데....제가 원하는건 업체명과 품명을 검색하면 모든정보가 나올수 있게가 가능한지
'궁금하며, 혹시 만들어 주실수 있으신지 입니다. ㅜㅜ



'고급필터를 이용한 검색
'데이터는 4행부터 있고 검색조건은 B1:C2에 있다는 가정하에서 아래의 코드를 수행해 보세요.
'B1:업체명칭,C1:품명
'B2:업체명입력, C2:품명입력
'
'source by 땡큐엑셀vba & 엑셀매크로
'
Sub 검색()
    
    '검색범위(A4:L20)는 알맞게 변경하시면 됩니다.
    '검색조건(B1:C2)
    Range("A4:L20").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("B1:C2"), Unique:=False
    
    
    MsgBox "완료 되었습니다.", vbInformation, "땡큐엑셀vba & 엑셀매크로"
End Sub



Sub 전체보기()

    On Error Resume Next

    ActiveSheet.ShowAllData
    
End Sub

고급필터검색-AdvancedFilter_ShowAllData-땡큐엑셀vba.xlsm
0.05MB

 

실행전

 

실행후

'http://kin.naver.com/qna/detail.nhn?d1id=1&dirId=102020101&docId=279455003
'
'첨부한 파일의 B2:D5 테이블에 입력된 값을 C10:G32 붙여 넣기 하고 싶습니다.
'단, 실행버튼을 클릭한 날짜와 A10:A32에 입력된 날짜가 일치할 경우 해당 열에 입력이 되어야 하는데요, VBA를 통해 가능할지 문의드립니다.


'
'source by 땡큐엑셀vba & 엑셀매크로
'
Sub 주간데이터()

    '일자를 먼저 선택하세요
    'Selection의 위치는 일자가 되고 offset(행,열)을 사용하여 위치를 잡습니다.
    
    Selection.Offset(0, 1) = Cells(2, "C") 'a
    Selection.Offset(0, 2) = Cells(3, "C") 'b
    Selection.Offset(0, 3) = Cells(4, "C") 'c
    Selection.Offset(0, 4) = Cells(5, "C") 'd
    Selection.Offset(0, 5) = Cells(2, "D") 'e
    
    MsgBox "완료되었습니다.", vbInformation, "땡큐엑셀vba & 엑셀매크로"
    
End Sub

 

선택한날짜에 값 붙여넣기-selection_offset-땡큐엑셀vba.xlsm
0.05MB

 

 

 

결과

 

'폴더의 엑셀파일 리스트업 & 하이퍼링크걸기

'
'source by 땡큐엑셀vba & 엑셀매크로
'
Option Explicit


Sub 폴더내_파일리스트업()


    Dim strPath As String                              '폴더의 경로를 넣을 변수
    Dim fileName As String                             '각 파일 이름을 넣을 변수
    Dim i As Integer
    
    '화면 업데이트 (일시) 정지
    'Application.ScreenUpdating = False
    
    '
    Cells.Clear
    
    '폴더선택
    With Application.FileDialog(msoFileDialogFolderPicker)
        '폴더 선택창 띄우기
        .Show
 
        If .SelectedItems.Count = 0 Then          '취소 선택시
            Exit Sub
        Else                                      '폴더를 선택한 경우
            strPath = .SelectedItems(1) & "\"     '폴더 경로를 변수에 넣음
        End If
    End With
    
    

    '해당 폴더의 모든 파일
    fileName = Dir(strPath & "*.xls*")
 
    If fileName = "" Then                         '폴더에 엑셀파일이 없으면
        MsgBox "파일이 없습니다.", vbInformation, "땡큐엑셀vba & 엑셀매크로"               '메시지 출력
        Exit Sub                                  '종료
    End If
            
    '파일이 존재하면
    Do While fileName <> ""
    
        i = i + 1

        'A열에 파일명 출력
        Cells(i, "A") = fileName
        
        '하이퍼링크걸기
        ActiveSheet.Hyperlinks.Add Anchor:=Cells(i, "A"), Address:=strPath & fileName, TextToDisplay:=fileName
        
        '다음 파일
        fileName = Dir
        
    Loop

    '열너비 자동 맞춤
    Columns.AutoFit
    
    '종료 메시지 출력
    MsgBox "완료되었습니다.", vbInformation, "땡큐엑셀vba & 엑셀매크로"
    
End Sub

파일리스트업_하이퍼링크-FileDialog_Dir_Hyperlinks_AutoFit-땡큐엑셀vba.xlsm
0.05MB

 

설정

 

실행전

 

실행후

'http://kin.naver.com/qna/detail.nhn?d1id=1&dirId=102020101&docId=280850592
'
'사진과 같이 기준값 O,X 입력에 따른 자동 색상이 변화되고
'기준값 대비 답안1,2가 맞을 때, 하나라도 틀릴 때, 다 틀렸을 경우를 텍스트 O,X로 설정해서
'색상이 자동으로 변하도록 함수를 만들고 싶습니다.
'가르침 부탁 드립니다 ^^
'가능한 간단한 영상으로 보여주시면 더욱 도움이 될 것 같습니다.
'감사합니다.


'사전에 이미지와 같이 설정시트를 만들고 실행하세요.
'
'
' source by 땡큐엑셀vba & 엑셀매크로
'
Sub 바탕색칠하기()

    Set 검색범위 = Sheets("설정").Range("D:D")

    마지막행 = Cells(Rows.Count, "A").End(xlUp).Row
    
    '2행부터 마지막행까지 루프
    For i = 2 To 마지막행
        찾을값 = Cells(i, "A") & Cells(i, "B") & Cells(i, "C")
        
        
         '1.검색범위에서 동일한값 찾기
         Set Rng = 검색범위.Find(What:=찾을값, LookAt:=xlWhole, LookIn:=xlValues)
    
         If Not Rng Is Nothing Then '검색결과가 있다면
            
            '찾은셀에 바탕색칠하기
            Cells(i, "A").Interior.Color = Rng.Offset(0, -3).Interior.Color
            Cells(i, "B").Interior.Color = Rng.Offset(0, -2).Interior.Color
            Cells(i, "C").Interior.Color = Rng.Offset(0, -1).Interior.Color
         End If
         
    
    Next i

    MsgBox "완료 되었습니다.", vbInformation, "땡큐엑셀vba & 엑셀매크로"

End Sub

 

조건에따른 바탕색칠하기-Find_Offset_Interior-땡큐엑셀vba.xlsm
0.18MB

실행전

 

실행후

 

'http://kin.naver.com/qna/detail.nhn?d1id=1&dirId=102020101&docId=293360786
'
'특정 단어만 색을 바꾸는법 없을까요?
'제가 모두 바꾸기로 특정 단어 색을 바꿔봤는데?그렇게 하면 셀 안에 있는 모든 단어의 색이 바뀌어 안되더군요.


'작성자: 땡큐엑셀vba & 엑셀매크로
'
Sub 컬러입히기()


    Set 검색범위 = Selection
    찾을값 = Cells(1, "G")
        
      
    '범위의 모든 셀을 순환하면서 값을 찾아서 색을 입힌다.
    For Each R In 검색범위
    
        'inStr함수로 찾을값 검색. 검색이 되면 시작위치를 반환합니다.
        pos = InStr(1, R, 찾을값)
                
        If pos > 0 Then
            k = k + 1
            
            '찾은 글자에 색을 입힌다.
            With R.Characters(Start:=pos, Length:=Len(찾을값)).Font
               .Color = -16776961 '빨간색
           End With
    
        End If
        
    Next
    
    
    MsgBox k & "건 완료 하였습니다.", vbInformation, "땡큐엑셀vba & 엑셀매크로"


End Sub

 

찾은단어색칠하기-instr_fontcolor-땡큐엑셀vba.xlsm
0.14MB

 

분리전

 

Aa
B

 

ccc

 

'http://kin.naver.com/qna/detail.nhn?d1id=1&dirId=102020101&docId=282200498
'
'안녕하세요
'업무중 급한질문이 있어 질문드립니다ㅠㅠ
'예를들어 A열에는 업체명이 들어가있고 업체마다 다른양의 상품들이 쭉 나오는데요
'1,2행에 있는 제목란은 공통으로 들어가고 (행높이는 지정된것으로 +셀너비가 글자수에 맞춰 늘어나게끔)
'업체마다 엑셀을 다른이름(업체명)으로 저장해야합니다..
'각각의 파일은 A열에 있는 업체명으로 저장되어야합니다ㅠㅠ
'도움부탁드려요
'예시파일 첨부합니다!


' 시트생성하여 업체별로 분리하기

'
' source by 땡큐엑셀vba & 엑셀매크로
'
Sub 업체별분류()

    Dim srcOpt1 As Range
    Dim dstSht As String
    Dim dstOpt1 As String
    
    Dim 업체명 As String
    Dim 현재시트명 As String
    
    현재시트명 = ActiveSheet.Name
    
    '화면갱신 중지
    Application.ScreenUpdating = False
    
    '마지막행까지 루프돌며 작업
    마지막행 = Cells(Rows.Count, "A").End(xlUp).Row
    For i = 3 To 마지막행
    
        '시트확인및 생성
        업체명 = Cells(i, "A")
        If ExistSheet(업체명) Then
            '시트가 있을경우
        Else
            '시트가 없을경우 생성
            Worksheets.Add after:=Sheets(Worksheets.Count)
            Sheets(Worksheets.Count).Name = 업체명
            Sheets(현재시트명).Activate
            
        
            '헤더 복사
            Range("A1:R2").Select
            dstSht = 업체명
            Call 붙여넣기(dstSht)
        
        End If
    
    
        '업체별 내용 복사
        Range("A" & i & ":R" & i).Select
        dstSht = 업체명
        Call 붙여넣기(dstSht)
        
    Next i
     
    
    MsgBox "완료되었습니다.", vbInformation, "땡큐엑셀vba & 엑셀매크로"

End Sub


' source by 땡큐엑셀vba & 엑셀매크로
'시트의 마지막행에 붙여넣기
'dstSht  : 목적 시트명
Sub 붙여넣기(dstSht As String)

    '복사
    Selection.Copy
    
    마지막행 = Sheets(dstSht).Cells(Rows.Count, "A").End(xlUp).Row
    If 마지막행 = 1 Then
        복사행 = 마지막행
    Else
        복사행 = 마지막행 + 1
    End If
    
    
    '행삽입하며 붙여넣기
    Sheets(dstSht).Range("A" & 복사행).EntireRow.Insert Shift:=xlShiftDown, CopyOrigin:=xlFormatFromLeftOrAbove
    
    '복사모드 해제
    Application.CutCopyMode = flase
        
End Sub


' source by 땡큐엑셀vba & 엑셀매크로
'시트유무 확인
'있을경우 True, 없을경우 False 리턴
Function ExistSheet(strOpt1 As String) As Boolean

    Dim sht As Worksheet
    
    On Error Resume Next
    
    Set sht = Worksheets(strOpt1)
    
    If Err.Number = 0 Then
        ExistSheet = True
    Else
        ExistSheet = False
    End If
    
End Function

 

업체별분류-Worksheets.Add_Worksheets.Name_Copy_CutCopyMode-땡큐엑셀vba.xlsm
0.07MB

 

 

사용자 정의 함수를 이용하여 Sheet2를 Sheet3와 같이 정리

 

'엑셀 함수 이름명단찾기함수 좀 부탁드립니다.
'안녕하세요
'엑셀함수 넣기 질문입니다. 아래 그림과 같이 시트 A 에 있는 것을 시트 B 와같이할려고 합니다.
'자세한 질문은 파일에 올렸습니다.


'범위-항목이 들어간 열의 구간
'항목-감사한금,십일조등
'월-1,2,3,4,5,6,7,8,9,10,11,12
'
'source by 땡큐엑셀vba & 엑셀매크로
'
Function nameList(범위 As Range, 항목 As String, 월 As Integer)

    Dim 명단 As String
    
    On Error Resume Next
    
    '범위내 루프
    For Each r In 범위
    
        'offset(0,1)을 하여 월추출
        날짜 = Int(Format(r.Offset(0, 1), "m"))

        If 항목 = r.Value And 날짜 = 월 Then
            '항목이 같고 월이 같으면 명단에 추가
            명단 = 명단 & " " & r.Offset(0, -1)
        End If
        
    Next
    
    nameList = 명단
    
End Function

 

항목별명단리스트-Int_Format_Foreach-사용자정의함수-땡큐엑셀vba.xlsm
0.15MB