Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
I want to format a worksheet based on values in another worksheet
I have a worksheet whcih has several table.1.csv,2.csv,3.csv upto
12.csv. In the main sheet, driver.csv, I want to define the different formatting options, something like tabname column no Atrrib1Name Attrib1Value Attrib2Name Arrib2 value Attr3nm attr3val 1.csv A WIDTH 10 DATATYPE DATE(MM/DD/YYY) font romanbold 1.csv b WIDTH 10 DATATYPE number(00.) font romanbold 2.csv a WIDTH 8 DATATYPE number(00.) font arial I am able to do this if i create a macro and run it but now I want to generalise it , any approaches are welcome. Thanks in anticipation. Ajay |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
I want to format a worksheet based on values in another worksheet
I would revise your table slightly, as follows:
TabName Column Width Format Font Bold 1.csv A 10 mm/dd/yyyy Times New Roman TRUE 1.csv B 10 ="00." Times New Roman TRUE 2.csv A 8 ="00." Arial FALSE If you are willing to do that, then you can try the following code. It loops through each row in the table, then loops across each column, finds the value, then uses a Select Case statement to decide what to do. If you encounter any errors, just set a breakpoint at the bottom of the routine on (or right after) the line label "ErrUnknownError:". Then check the Locals window to what the variables are. If you decide to add any additional columns, just make sure that the TabName comes first, then the Column. After that, you can probably put things in any order. Be sure to add code to the Select Case statement to handle the new instruction (column on the "Drivers" worksheet). Option Explicit Const strMsgBoxTitle = "Format Data Worksheets" '---------------------------------------------------------------------- Public Sub FormatDataWorksheets() 'Variables that all refer to the "Drivers" worksheet. Dim wbData As Workbook 'Workbook with Formatting & Data. Dim wsDriver As Worksheet 'Worksheet with formatting instructions. Dim rngAttributes As Range 'Top row of Driver worksheet. Dim rngAttribute As Range 'A single attribute on the top row. Dim lngNumInstructions As Long 'Total rows of formatting instructions. Dim lngInstruction As Long 'Single row of formatting instructions. Dim varCell As Variant 'Single cell on the "Drivers" worksheet. 'Variables that refer to the data worksheet being formatted. Dim wsData As Worksheet Dim rngColumn As Range Dim strErrorMsg As String On Error GoTo ErrUnknownError Application.ScreenUpdating = False Set wbData = ActiveWorkbook Set wsDriver = wbData.Worksheets("Driver") With wsDriver.UsedRange Set rngAttributes = .Resize(1) 'Top row of labels. lngNumInstructions = .Rows.Count - 1 'Top row of labels doesn't count. End With For lngInstruction = 1 To lngNumInstructions Set wsData = Nothing Set rngColumn = Nothing For Each rngAttribute In rngAttributes varCell = wsDriver.Cells(lngInstruction + 1, rngAttribute.Column) Select Case rngAttribute Case "TabName" Set wsData = wbData.Worksheets(varCell) Case "Column" Set rngColumn = wsData.Columns(varCell) Case "Width" rngColumn.ColumnWidth = varCell Case "Format" rngColumn.NumberFormat = varCell Case "Font" rngColumn.Font.Name = varCell Case "Bold" rngColumn.Font.Bold = varCell Case Else GoTo ErrUnkownAttribute End Select Next rngAttribute Next lngInstruction GoTo ExitSub ErrUnkownAttribute: strErrorMsg = "Unkown format attribute." MsgBox strErrorMsg, vbCritical + vbOKOnly, strMsgBoxTitle GoTo ExitSub ErrUnknownError: strErrorMsg = "Unknown error." MsgBox strErrorMsg, vbCritical + vbOKOnly, strMsgBoxTitle GoTo ExitSub ExitSub: End Sub -- Regards, Bill Renaud |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
I want to format a worksheet based on values in another worksheet
I forgot to mention that the values in the "Format" column should probably
be entered as formulas of strings (i.e. ="00."), instead of just entering the value. Remember that Excel normally truncates leading zeroes from numbers, so trying to specify a number format that will display leading zeroes will be difficult or even impossible, without using a formula to force the value to a string. -- Regards, Bill Renaud |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Return filtered values into report worksheet based on filtered valueon the data worksheet | Excel Worksheet Functions | |||
How do I copy cells from one worksheet to another based on values | Excel Worksheet Functions | |||
Insert Rows Based on values in another worksheet | Excel Discussion (Misc queries) | |||
Changing cell values based on a worksheet name | Excel Programming | |||
Macro to translate values based upon data in another worksheet? | Excel Programming |