Solved

vba excell automation to visual studio express 2012 vb

Posted on 2013-11-30
5
454 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
Comment Utility
Can you expand on 'simply not accepted',  please?
0
 
LVL 14

Expert Comment

by:Faustulus
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks !!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
My experience with Windows 10 over a one year period and suggestions for smooth operation
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

772 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

14 Experts available now in Live!

Get 1:1 Help Now