How to Center Formula Result Cell in VBA

aryaden

Board Regular
Joined
Jun 9, 2021
Messages
101
Office Version
  1. 2019
Platform
  1. Windows
I currently have this code which puts an X in a selected range based on a COUNTIF Formula. I would like these Xs to be centered in the cells

VBA Code:
Set Rng = Range(Cells(r + 1, "C"), Cells(r + counter, "C"))
Cells(r, "C").Formula = "=IF(COUNTIF(" & Rng.Address(0, 0) & ", ""X""), ""X"", "" "")"
Cells(r, "C").Copy Range(Cells(r, "D"), Cells(r, lc))
Application.CutCopyMode = False

Currently I have tried: Rng.HorizontalAlignment = xlCenter

Thank you for the help in advance!
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I think you are gonna need to provide a bit more code than that.
 
Upvote 0
I think you are gonna need to provide a bit more code than that.
This is the post/code I am referring to:

Thank you again for the help!
 
Upvote 0
Off the top, I would say you might try:

VBA Code:
Range("C:D").EntireColumn.HorizontalAlignment = xlCenter
 
Upvote 0
I think you are gonna need to provide a bit more code than that.
This is the fixed code from that post that works :
VBA Code:
Sub Integrate()

    Dim lr As Long
    Dim r As Long
    Dim lc As Long
    Dim counter As Integer
   
    Application.ScreenUpdating = False
   
'   Find last row with data in column A
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
   
'   Loop through data backwards
    For r = lr To 3 Step -1
'   Create counter
    counter = counter + 1

'       Check to see if column A is not equal to row above it
        If (Cells(r, "A") <> "") And (Cells(r, "A") <> Cells(r - 1, "A")) Then
'           Insert blank row
            Rows(r).Insert
'           Copy value to column B
            Cells(r, "B") = Cells(r + 1, "A")
'           Copy formatting
            Cells(r + 1, "A").Copy
            Range(Cells(r, "B"), Cells(r, lc)).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
                SkipBlanks:=False, Transpose:=False        
'           Enter formula in selected cell range and define range for COUNTIF ***New Code from Joe4
          Set rng = Range(Cells(r + 1, "C"), Cells(r + counter, "C"))
          Cells(r, "C").Formula = "=IF(COUNTIF(" & rng.Address(0, 0) & ", ""X""), ""X"", "" "")"
          Cells(r, "C").Copy Range(Cells(r, "D"), Cells(r, lc))
            Application.CutCopyMode = False
            ' Set counter = 0 for next set
            counter = 0
        End If
    Next r
   
'   Delete column A
    Columns("A:A").Delete
'   Autofit columns
    Cells.EntireColumn.AutoFit
    Application.ScreenUpdating = True
   
End Sub
 
Upvote 0
Off the top, I would say you might try:

VBA Code:
Range("C:D").EntireColumn.HorizontalAlignment = xlCenter
I am trying to get all columns until "lc" to be centered. Thank you!
 
Upvote 0
Again off the top:

VBA Code:
Range("C:" & lc).EntireColumn.HorizontalAlignment = xlCenter
 
Upvote 0
yes, I know, Lemme play a bit to see what is going on.
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top