?
Solved

VBA Access: Repeated Formatting - How do I create a public function for this?

Posted on 2014-10-10
5
Medium Priority
?
184 Views
Last Modified: 2014-10-10
Hi there!
I have some VBA code that involves a lot of formatting. My code is super long because of this, which is...distracting.

I would like to put the formatting part of the code into a public function so that I can just call the formatting with one word, but I am not sure who to do this with formatting. It isn't really a function (no "sub" at the front and no "end sub" at the end). It is just a chunk of code that formats. And I use it repeatedly, so it just be so great if I could make this work.

Any ideas?
0
Comment
Question by:Megin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 48

Expert Comment

by:Dale Fye
ID: 40373628
Can you give more details about what it is formatting?

Generally, you would simply cut the code that you want to create a procedure from and paste it into a new code module.  But depending on what you are formatting, you might also have to pass some objects.  I do this with Excel all of the time, one subroutine that accepts objects for XL (Excel application), WBK (workbook), and SHT (worksheet).  It then loops through the header row looking at the column names and formats the columns according to a predefined parameters.

Then you simply call that subroutine from your original code, making sure you pass in all of the objects that are needed to perform whatever formatting you are doing.
0
 

Author Comment

by:Megin
ID: 40373675
This is the type of chunk that I would like to shorten:
With C
    .Font.Name = "Arial"
    .Font.Bold = True
    .Font.Size = 12
    .Interior.ColorIndex = 15
    .Borders(xlEdgeTop).LineStyle = xlContinuous
    .Borders(xlEdgeTop).Weight = xlThin
    .Borders(xlEdgeTop).ColorIndex = xlAutomatic
    .Borders(xlEdgeBottom).LineStyle = xlContinuous
    .Borders(xlEdgeBottom).Weight = xlThin
    .Borders(xlEdgeBottom).ColorIndex = xlAutomatic
    .Borders(xlEdgeRight).LineStyle = xlContinuous
    .Borders(xlEdgeRight).Weight = xlThin
    .Borders(xlEdgeRight).ColorIndex = xlAutomatic
    .Borders(xlEdgeLeft).LineStyle = xlContinuous
    .Borders(xlEdgeLeft).Weight = xlThin
    .Borders(xlEdgeLeft).ColorIndex = xlAutomatic
    .Borders(xlInsideVertical).LineStyle = xlContinuous
    .Borders(xlInsideVertical).Weight = xlThin
    .Borders(xlInsideVertical).ColorIndex = xlAutomatic
    .Borders(xlInsideHorizontal).LineStyle = xlContinuous
    .Borders(xlInsideHorizontal).Weight = xlThin
    .Borders(xlInsideHorizontal).ColorIndex = xlAutomatic
    .WrapText = True
    .RowHeight = 54.75
End With

Open in new window


I have this all over my code.

It is formatting an Excel spreadsheet.
0
 
LVL 11

Accepted Solution

by:
Dany Balian earned 2000 total points
ID: 40373736
if all the linestyles are the same, and all colorindexes are the same within the same block, you can use something like this:

public sub formatItem(byref obj, byval fontName as string, byval fontbold as boolean, byval fontsize as integer, byval interiorcolorindex as integer, byval colorindex as integer, byval linestyle as integer, byval weight as integer, byval wraptext as boolean, byval rowheight as double)
With obj
    .Font.Name = fontname
    .Font.Bold = fontbold
    .Font.Size = fontsize
    .Interior.ColorIndex = interiorcolorindex
    .Borders(xlEdgeTop).LineStyle = linestyle
    .Borders(xlEdgeTop).Weight = weight
    .Borders(xlEdgeTop).ColorIndex = colorindex
    .Borders(xlEdgeBottom).LineStyle = linestyle
    .Borders(xlEdgeBottom).Weight = weight
    .Borders(xlEdgeBottom).ColorIndex = colorindex
    .Borders(xlEdgeRight).LineStyle = linestyle
    .Borders(xlEdgeRight).Weight = weight
    .Borders(xlEdgeRight).ColorIndex = colorindex
    .Borders(xlEdgeLeft).LineStyle = linestyle
    .Borders(xlEdgeLeft).Weight = weight
    .Borders(xlEdgeLeft).ColorIndex = colorindex
    .Borders(xlInsideVertical).LineStyle = linestyle
    .Borders(xlInsideVertical).Weight = weight
    .Borders(xlInsideVertical).ColorIndex = colorindex
    .Borders(xlInsideHorizontal).LineStyle = linestyle
    .Borders(xlInsideHorizontal).Weight = weight
    .Borders(xlInsideHorizontal).ColorIndex = colorindex
    .WrapText = wraptext
    .RowHeight = rowheight
End With

Open in new window


and you call it using:
call formatitem(c, "Arial", True, 12, 15, xlAutomatic, xlContinuous, xlThin, True, 54.75)

Open in new window

0
 

Author Closing Comment

by:Megin
ID: 40373791
That was exactly what I was looking for.

Thank you!
0
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40373850
Glad i could help :)
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question