본문 바로가기
VBA 라이브러리

Space 함수

2023. 7. 22.

지정된 수의 공백으로 구성된 Variant(String)를 반환합니다.

구문은 다음과 같습니다.

Syntax

Space(number)

number : 필수 요소입니다. 공백 문자의 개수를 나타내는 정수 값입니다

Space 함수는 출력 서식 지정 및 고정 길이 문자열에서 데이터를 지우는 데 유용합니다.

다음은 간단한 예제 코드입니다.

예제 코드

Sub FormatData()
    Dim name As String
    Dim age As Integer
    
    name = "John Smith"
    age = 30
    
    ' 이름과 나이를 고정 길이로 맞춰서 출력
    Debug.Print "Name: " & name & Space(20 - Len(name)) & "Age: " & age
End Sub

Sub AlignText()
    Dim productName As String
    Dim productPrice As String
    
    productName = "Apple"
    productPrice = "1200"
    
    ' 공백 문자 추가 (총 길이 10이 되도록)
    Debug.Print productName & Space(10 - Len(productName)) & productPrice
End Sub

Sub IndentText()
    Dim level1, level2, level3 As String
    level1 = "Project"
    level2 = "Milestone"
    level3 = "Task"
    
    Debug.Print level1
    Debug.Print Space(4) & level2
    Debug.Print Space(8) & level3
End Sub

Sub CreateSpaceString()
    Dim spaceString As String
    spaceString = Space(5)
    
    Debug.Print "[" & spaceString & "]"
End Sub

Sub FixedWidthOutput()
    Dim item1 As String, item2 As String, item3 As String
    
    item1 = "Product"
    item2 = "Qty"
    item3 = "Price"
    
    ' 공백 문자를 추가하여 같은 간격으로 정렬
    Debug.Print item1 & Space(15 - Len(item1)) & item2 & Space(10 - Len(item2)) & item3
End Sub

Sub PadString()
    Dim productCode As String
    Dim paddedCode As String
    Dim maxLength As Integer
    
    productCode = "AB12"
    maxLength = 10
    
    ' 공백 문자를 추가하여 문자열을 maxLength 길이로 만듦
    paddedCode = productCode & Space(maxLength - Len(productCode))
    
    Debug.Print paddedCode
End Sub

Sub FillString()
    Dim originalString As String
    Dim targetLength As Integer
    
    originalString = "text"
    targetLength = 10
    
    ' 실제 길이보다 짧은 경우 공백 문자를 추가하여 targetLength에 도달
    If Len(originalString) < targetLength Then
        originalString = originalString & Space(targetLength - Len(originalString))
    Else
    End If
    
    Debug.Print "[" & originalString & "]"
End Sub

Sub FormatItemsAndPrices()
   
    Dim rng As Range
    Dim cell As Range
    Dim item As String
    Dim price As Double
    Dim maxItemLength As Integer
    Dim output As String
    Dim i As Integer
    
    ' 테이블 범위 설정
    Set rng = Range("A2:B30")
    
    ' 품목의 최대 길이 계산
    maxItemLength = 0
    For Each cell In rng.Columns(1).Cells
        If Len(cell.Value) > maxItemLength Then
            maxItemLength = Len(cell.Value)
        End If
    Next cell
    
    ' 출력 생성
    output = "품목" & Space(maxItemLength - Len("품목") + 1) & "가격" & vbCrLf
    output = output & String(maxItemLength + 7, "-") & vbCrLf
    
    For i = 1 To rng.Rows.Count
        item = rng.Cells(i, 1).Value
        price = rng.Cells(i, 2).Value
        output = output & item & Space(maxItemLength - Len(item) + 3) & price & vbCrLf
    Next i
    
    ' 출력
    Debug.Print output
End Sub

'VBA 라이브러리' 카테고리의 다른 글

CreateObject 함수  (0) 2023.08.06
String 함수  (0) 2023.07.22
StrConv 함수  (0) 2023.07.22
StrComp 함수  (0) 2023.07.22
ColorConstants  (0) 2023.07.18