VBA Change Entire Columns with specific header to Value instead of Formulas

LNG2013

Active Member
Joined
May 23, 2011
Messages
466
Hello,

I need help with a bit of VBA code. I have columns AIFinal01-AIFinal20 throughout my sheet. Each column contains a formula going to the bottom. I need to change that formula to a value for all 20 columns. The columns are not in direct order and their are columns between them. Any help is always appreciated. Thank you.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Are all the column headers in row 1?
Are there any column headers that begin with "AIFinal..." that you do NOT want included in this?
 
Upvote 0
Assuming my assumptions above are correct (header in row 1, change ALL columns starting with "AIFinal", then this code should do what you want:
Code:
Sub MyCopyValueMacro()

    Dim c As Long
    Dim lc As Long
    Dim lr As Long
    
    Application.ScreenUpdating = False
    
'   Find last column in row 1 with headers
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
    
'   Loop through all columns
    For c = 1 To lc
'       Check header
        If Left(Cells(1, c), 7) = "AIFinal" Then
'           Find last row in column with data
            lr = Cells(Rows.Count, c).End(xlUp).Row
'           Convert all formulas to values
            Range(Cells(2, c), Cells(lr, c)).Value = Range(Cells(2, c), Cells(lr, c)).Value
        End If
    Next c
    
    Application.ScreenUpdating = True
    
    MsgBox "Copy complete!"
    
End Sub
 
Last edited:
Upvote 0
You are welcome.
Glad I was able to help!
:)
 
Upvote 0

Forum statistics

Threads
1,223,703
Messages
6,173,941
Members
452,539
Latest member
delvey

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