Solved

Mark cell in Excel bold using VBA Access

Posted on 2016-08-09
5
25 Views
Last Modified: 2016-08-09
I have the following code and need to mark a specific cell in an excel sheet bold:

With objExcel
  .Visible = True
  .Workbooks.Add (xTemplate)
  .worksheets(1).Activate

  .worksheets(1).Cells(2, 1).Value = "TEST"
  .worksheets(1).Cells(2, 1).HorizontalAlignment = xlCenter
End With

unfortunately this generates an error and I have tried many different versions with no success. Thank you for any help!
0
Comment
Question by:mpim
  • 3
  • 2
5 Comments
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 41749588
First designate a range, then select it, then apply various types of formatting, such as this:
         sht.Range("A1:Q1").Select
         With appExcel.Selection
             .HorizontalAlignment = xlCenter
             .VerticalAlignment = xlCenter
             .MergeCells = True
         End With
         
         With appExcel.Selection.Font
             .Name = "Calibri"
             .Size = 16
         End With
         
         appExcel.Selection.Font.Bold = True

Open in new window


You can generally find the syntax you need by recording a macro.
0
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 41749592
You might find my ebook, Working with Excel, useful.  It is available from Office Watch.  It covers exchanging data between Access and Excel in a variety of ways.
0
 

Author Comment

by:mpim
ID: 41749609
Hi Helen,
thank you and I have adjusted the code to the following but still get the following error "Unable to set HorizontalAlignment property of the range class"?? Thank you for any further help...

With objExcel
  .Visible = True
  .Workbooks.Add (xTemplate)
  .worksheets(1).Activate

  .worksheets(1).Cells(2, 1).Value = "TEST"
  .worksheets(1).Range("A2:A2").SELECT
  With objExcel.Selection
    .HorizontalAlignment = xlCenter
  End With
0
 
LVL 31

Accepted Solution

by:
Helen_Feddema earned 500 total points
ID: 41749613
Did you declare objExcel as Excel.Application?  That is needed to use named constants.  There should be a reference to the Excel object model in the References dialog.
0
 

Author Closing Comment

by:mpim
ID: 41749631
Great! Thank you so much for your help..
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now