Solved

vba excell automation to visual studio express 2012 vb

Posted on 2013-11-30
5
456 Views
Last Modified: 2013-12-01
Hello  experts,
the next line Works just fine in vb6

objsheet.Range(Cells(12, c + 1), Cells(fi, c + 1)).HorizontalAlignment = xlCenter

but in visual studio 2012 express visual basic, it simply is not accepted.

What is the correct way to do this?

Regards,
0
Comment
Question by:rguillermo
  • 2
  • 2
5 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39687305
Can you expand on 'simply not accepted',  please?
0
 
LVL 14

Expert Comment

by:Faustulus
ID: 39687308
Try
with objsheet
    Range(.Cells(12, c + 1), .Cells(fi, c + 1)).HorizontalAlignment = xlCenter
End With

Open in new window

0
 

Author Comment

by:rguillermo
ID: 39687386
GrahamSkan

the sentence:

    objsheet.Range(Cells(12, c + 1), Cells(fi, c + 1)).HorizontalAlignment = xlCenter

produces the message

    "Cells" is not declared. It may be inaccesible due to its protection level.


Faustulus

with objsheet
    Range(.Cells(12, c + 1), .Cells(fi, c + 1)).HorizontalAlignment = xlCenter
End With

returns message: An unhandled exception of type 'system.runtime.InteropServices.COMException' ocurred in MicrosoftVisualBasic.dll
Additional information to set the HorizontalAlignment property of the range class.

this sentence

objsheet.cells("a1:c10¨").selection()

does not work either.

What could be the problema?
0
 
LVL 14

Accepted Solution

by:
Faustulus earned 500 total points
ID: 39687463
I would try to properly declare the object and then change its properties:_

Dim objSheet As Worksheet
Dim Rng As Range

Set objSheet = ThisWorkbook.Sheets("Sheet1")
with objsheet
    Set Rng = Range(.Cells(12, c + 1), .Cells(fi, c + 1))
End With
Rng.HorizontalAlignment = xlCenter 

Open in new window


The error seems to be a known bug which is related to late binding, In your case I presume that objSheet is declared as an object rather than a worksheet, and perhaps that is all it takes. But setting up the range as well as the sheet should make sure that Excel understands your instruction at runtime.

You may like to read up about this error at this link.
0
 

Author Closing Comment

by:rguillermo
ID: 39689041
Thanks !!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

911 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

22 Experts available now in Live!

Get 1:1 Help Now