Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Ag Ag is offline
external usenet poster
 
Posts: 4
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 417
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 417
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Return filtered values into report worksheet based on filtered valueon the data worksheet dicko1 Excel Worksheet Functions 1 April 21st 09 12:27 AM
How do I copy cells from one worksheet to another based on values Vickynh Excel Worksheet Functions 2 October 4th 08 05:18 AM
Insert Rows Based on values in another worksheet Jason Excel Discussion (Misc queries) 3 June 24th 07 01:13 PM
Changing cell values based on a worksheet name [email protected] Excel Programming 2 May 7th 07 07:51 PM
Macro to translate values based upon data in another worksheet? Riccardinho Excel Programming 1 July 6th 05 05:37 AM


All times are GMT +1. The time now is 08:48 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"