Solved

Mark cell in Excel bold using VBA Access

Posted on 2016-08-09
5
38 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
[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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

626 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