# How to Center Text Using VBA (With Example)

You can use the HorizontalAlignment and VerticalAlignment properties in VBA to center the text in specific cells in Excel horizontally and vertically, respectively.

Here are three common ways to use these properties in practice:

Method 1: Center Text Horizontally Using VBA

```Sub CenterText()
Range("A2:A11").HorizontalAlignment = xlCenter
End Sub```

Method 2: Center Text Vertically Using VBA

```Sub CenterText()
Range("A2:A11").VerticalAlignment = xlCenter
End Sub```

Method 3: Center Text Both Horizontally & Vertically Using VBA

```Sub CenterText()
Range("A2:A11").HorizontalAlignment = xlCenter
Range("A2:A11").VerticalAlignment = xlCenter
End Sub```

The following examples show how to use each method in practice with the following dataset in Excel: ## Example 1: Center Text Horizontally Using VBA

We can create the following macro to center the text in each cell in the range A2:A11 horizontally:

```Sub CenterText()
Range("A2:A11").HorizontalAlignment = xlCenter
End Sub```

When we run this macro, we receive the following output: Notice that the text in each cell in the range A2:A11 has been centered horizontally.

## Example 2: Center Text Vertically Using VBA

We can create the following macro to center the text in each cell in the range A2:A11 vertically:

```Sub CenterText()
Range("A2:A11").VerticalAlignment = xlCenter
End Sub```

When we run this macro, we receive the following output: Notice that the text in each cell in the range A2:A11 has been centered vertically.

## Example 3: Center Text Both Horizontally & Vertically Using VBA

We can create the following macro to center the text in each cell in the range A2:A11 both horizontally and vertically:

```Sub CenterText()
Range("A2:A11").HorizontalAlignment = xlCenter
Range("A2:A11").VerticalAlignment = xlCenter
End Sub```

When we run this macro, we receive the following output: Notice that the text in each cell in the range A2:A11 has been centered both horizontally and vertically.
{"ft_lang_label":"__label__en","ft_lang_prob":0.74954575,"math_prob":0.4268418,"size":2109,"snap":"2023-40-2023-50","text_gpt3_token_len":489,"char_repetition_ratio":0.16104513,"word_repetition_ratio":0.54662377,"special_character_ratio":0.22000948,"punctuation_ratio":0.12532637,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9944675,"pos_list":[0,1,2,3,4,5,6,7,8],"im_url_duplicate_count":[null,1,null,1,null,1,null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-22T19:13:22Z\",\"WARC-Record-ID\":\"<urn:uuid:43a239cb-3cd2-4c20-b862-495fc559e941>\",\"Content-Length\":\"48645\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8318f230-e5c3-4f82-951f-279c2ec91763>\",\"WARC-Concurrent-To\":\"<urn:uuid:9f574112-0b57-4a47-8b6b-fb269ca162e5>\",\"WARC-IP-Address\":\"34.120.190.48\",\"WARC-Target-URI\":\"https://www.statology.org/vba-center-text/\",\"WARC-Payload-Digest\":\"sha1:VUJIFN6FRPA52ZEZA4JMD2G77FRRXWE5\",\"WARC-Block-Digest\":\"sha1:KMEPR2A7LMJSY2MRN6DQZWESYEE7RI5B\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233506421.14_warc_CC-MAIN-20230922170343-20230922200343-00810.warc.gz\"}"}