땡큐엑셀vba & 엑셀매크로

사용자 정의 함수를 이용하여 글자수 세기

 

 

 

module1에 사용자 정의함수 작성

 

'http://kin.naver.com/qna/detail.nhn?d1id=1&dirId=102020101&docId=278816827&qb=VkJB&enc=utf8&section=kin.qna.all&rank=28&search_sort=8&spq=0
'엑셀 vba 사용자 정의 함수로 글자수를 파악하고자 합니다.
'(공백 포함, 공백 비포함으로 구분)
'
'텍스트 파일을 열지 않고 정보만 가져오면 됩니다 .
'
'A1셀에
'파일 전체 경로가 입력되어 있고
'
'A2셀에
'=wordcount(a1) 이라고 입력하면 글자수가 나오면 됩니다



'
'source by 땡큐엑셀vba & 엑셀매크로
'
'공백포함 글자수
Function wordCount(strFile)
  On Error GoTo errorMessage
  
  Dim s As String, s2 As String, fileHandle As Integer

  fileHandle = FreeFile ' 사용 가능한 파일 핸들 번호 구하기
  Open strFile For Input As fileHandle ' 파일오픈

  '파일의 끝까지 읽기
  Do While Not EOF(fileHandle) ' 파일의 끝까지 반복
    Line Input #fileHandle, s  ' 읽은 1줄을 변수 s 에 대입
    fulltxt = fulltxt & s      ' 결과를 저장할 변수에 차곡차곡 추가
  Loop

  wordCount = Len(fulltxt)


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

quitSub:
  Close fileHandle '파일닫기
  Exit Function


errorMessage:
  ' 에러 메시지 대화상자 출력
  MsgBox Err.Description, vbOKOnly + vbCritical, "에러 코드: " & Err.Number
  Resume quitSub

End Function



'A2셀에
'=wcNotSpace(a1) 이라고 입력하면 글자수가 나오면 됩니다
'공백제외 글자수
Function wcNotSpace(strFile)
  On Error GoTo errorMessage
  
  Dim s As String, s2 As String, fileHandle As Integer

  fileHandle = FreeFile ' 사용 가능한 파일 핸들 번호 구하기
  Open strFile For Input As fileHandle ' 파일오픈

  '파일의 끝까지 읽기
  Do While Not EOF(fileHandle) ' 파일의 끝까지 반복
    Line Input #fileHandle, s  ' 읽은 1줄을 변수 s 에 대입
    fulltxt = fulltxt & s      ' 결과를 저장할 변수에 차곡차곡 추가
  Loop

  '공백을 널로 변환
  fulltxt = Replace(fulltxt, " ", "")
  wcNotSpace = Len(fulltxt)


  MsgBox "완료되었습니다", vbInformation, "땡큐엑셀vba & 엑셀매크로"
  
quitSub:
  Close fileHandle '파일닫기
  Exit Function


errorMessage:
  ' 에러 메시지 대화상자 출력
  MsgBox Err.Description, vbOKOnly + vbCritical, "에러 코드: " & Err.Number
  Resume quitSub



End Function

 

 

글자수세기-땡큐엑셀vba.xlsm
0.02MB