[VBA] vba string.format, printf 기능

2022. 6. 4. 01:20·[VBA]
반응형

아쉽게도 vba는 문자열을 쉽게 이어 붙일수 있는 함수가 따로 없다...

 

함수 짜기 귀찮아서 구글 여기저기 찾아보다 이상한것만 있길래 간단하게 직접 짰다. 

 

기본적인 예외처리만 해두었음.

Function printf(Input_Text As String, ParamArray Args()) As String

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim strReturn As String

Debug.Print UBound(Split(Input_Text, "{"))

If UBound(Args) <> UBound(Split(Input_Text, "{")) - 1 Then

    printf = "배열 개수 Error"
    GoTo 종료:

End If

strReturn = Input_Text

For i = LBound(Args) To UBound(Args)
    
    If InStr(Input_Text, "{" & i & "}") <> 0 Then
        strReturn = Replace(strReturn, "{" & i & "}", Args(i))
    Else
        printf = i & " index error"
        GoTo 종료:
    End If
Next

printf = strReturn


종료:

Application.ScreenUpdating = False
Application.DisplayAlerts = False

End Function

Sub test()

Debug.Print printf("인수 : {0}, 인수1 : {1}, {2}", "000", "111", "2222")


End Sub

 

 

아래는 위에 예외처리 코드를 삭제함..

 

예를 들어

 

"인수 : {0}, {1}, {0}" 과 같이 한 인수를 중복되게 넣을경우 에러로 return 되어서 이를 삭제함.

Function printf(Input_Text As String, ParamArray Args()) As String

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim strReturn As String

strReturn = Input_Text

For i = LBound(Args) To UBound(Args)
    
    If InStr(Input_Text, "{" & i & "}") <> 0 Then
        strReturn = Replace(strReturn, "{" & i & "}", Args(i))
    Else
        printf = i & " index error"
        GoTo 종료:
    End If
Next

printf = strReturn


종료:

Application.ScreenUpdating = False
Application.DisplayAlerts = False

End Function

Sub test()

Debug.Print printf("인수 : {0}, 인수1 : {1}, {2}", "000", "111", "2222")


End Sub

 

반응형
저작자표시 비영리 변경금지 (새창열림)

'[VBA]' 카테고리의 다른 글

[VBA] 엑셀범위 그림파일로 저장(Range to jpg)  (0) 2023.08.03
[VBA] VBA 정규식 사용하기  (0) 2022.08.24
[VBA] 소수점 자릿수 계산  (0) 2022.02.18
[VBA] 엑셀 기본 저장 format 설정  (0) 2021.08.09
[VBA] 다른 엑셀파일 내용 가져오기 (프론트, 백)  (14) 2021.08.09
'[VBA]' 카테고리의 다른 글
  • [VBA] 엑셀범위 그림파일로 저장(Range to jpg)
  • [VBA] VBA 정규식 사용하기
  • [VBA] 소수점 자릿수 계산
  • [VBA] 엑셀 기본 저장 format 설정
jkod
jkod
  • jkod
    개발린이
    jkod
  • 반응형
    • 분류 전체보기
      • [RPA]
      • [Python]
      • [VBA]
      • [JAVA]
      • [Window]
      • [MAC]
      • [기타]
        • 생활
  • 최근 글

  • 인기 글

  • hELLO· Designed By정상우.v4.10.2
jkod
[VBA] vba string.format, printf 기능
상단으로

티스토리툴바