Link to home
Start Free TrialLog in
Avatar of Naresh Patel
Naresh PatelFlag for India

asked on

Column Formatting

I had this question after viewing TT Master Button.

Hi Experts,

will anyone help me with piece of code which change format for particular sheet.

In attached there is 2 sheets Import and Setting ..need format change in sheet Import ...there is 73 column ..each column name mention in sheet setting column D and its required format in column E...code which change format accordingly.

See Attached

Thanks
Formate-change.xlsm
Avatar of Zack Barresse
Zack Barresse
Flag of United States of America image

Hi there,

It depends on when you want this formatting to happen. You don't really state. My assumption was you wanted this to take affect every time you deselect the 'Settings' sheet. The below code goes into the worksheet module of 'Settings' (right click sheet tab, select 'View Code', paste). It will apply the formats specified. I think it's what you want. If not, please be specific as to what you need.

Private Sub Worksheet_Deactivate()

    Dim ColumnMatch As Long
    Dim ColumnIndex As Long
    Dim ColumnNames As Variant
    Dim ColumnFormats As Variant

    ColumnNames = Me.Range("D2", Me.Cells(Me.Rows.count, "D").End(xlUp)).Value
    ColumnFormats = Me.Range("E2", Me.Cells(Me.Rows.count, "E").End(xlUp)).Value
    
    For ColumnIndex = LBound(ColumnNames) To WorksheetFunction.Min(UBound(ColumnNames), UBound(ColumnFormats))
        
        ColumnMatch = 0
        On Error Resume Next
        ColumnMatch = WorksheetFunction.Match(ColumnNames(ColumnIndex, 1), Sheets("Import").Range("1:1"), 0)
        If ColumnMatch > 0 Then
            Sheets("Import").Columns(ColumnMatch).NumberFormat = ColumnFormats(ColumnIndex, 1)
        End If
        On Error GoTo 0
        
    Next ColumnIndex
    
End Sub

Open in new window


HTH

Regards,
Zack Barresse
Avatar of Naresh Patel

ASKER

Frankly Speaking i need to add this task before one Sub going to execute which is Module M4_Formulas - Sub CopyFormulas() in attached WB.
i have one module called M0_Master which execute all Sub one by one ...need to call above procedure before Sub CopyFormula.  

Because of file size increases i had uploaded WB with related sheet for this question.

do i copy above code and past to one more module called M4_ColumnFormatting  ? it will gona work ?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Zack Barresse
Zack Barresse
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect