Solved

Mark cell in Excel bold using VBA Access

Posted on 2016-08-09
5
33 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

777 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