Problems Specifying an Excel File, Worksheet, & Range from VB6

Dragonsong42

New Member
Joined
Mar 16, 2009
Messages
1
I am writing a Visual Basic 6 program, in which I would like to open an Excel file, loop through several worksheets, and set the value of a specific cell within the worksheet. My code looks more or less like this (some variables are declared prior to this code snapshot):

Dim objExcel As Object
Set objExcel = CreateObject("excel.application")
ConvDataPath = (Path & "\Converted Data\Converted_Data_XX.xls")
objExcel.Workbooks.Open FileName:=(ConvDataPath)
ConvData = "Converted_Data_XX.xls"

Shtname = Mysheet1

For k = 1 To 4
Shtname = (k & ", " & Shtname)
objExcel.Windows(ConvData).Activate
objExcel.Sheets(Shtname).Select
Range(A1).Select
Next k


The problem that I am having is that VB is not seeing the Range selection as a valid function when a sheet is selected. If I remove the line where I specify the sheet, the program stops having an issue with the Cell selection, but that's not an option for me. Placing "objExcel." before the "Range" doesn't fix my problem.

Has anyone heard of VB6 having a problem with this function when manipulating Excel data? I've used similar functions before in macros. I'm wondering if perhaps I'm missing some OLE files or something...

Any help is appreciated!
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

NateO

Legend
Joined
Feb 17, 2002
Messages
9,700
Hello,

Welcome to MrExcel.com. :)

Even worse, you're hanging Excel in memory. You need quotes in your Range reference.

You also want to qualify all of your Excel Objects, Methods, Properties, e.g.,

Rich (BB code):
Dim objExcel As Object
Dim xlWb as Object
Dim xlWs as Object
Set objExcel = CreateObject("excel.application")
ConvDataPath = (Path & "\Converted Data\Converted_Data_XX.xls")
set xlWB = objExcel.Workbooks.Open FileName:=(ConvDataPath)
ConvData = "Converted_Data_XX.xls"
 
For each xlWs in xlWB.Worksheets
    xlWs.Range("A1").Select ' Whatever
Next
*aircode*

And from there, terminate your variables and Quit Excel or make it Visible, or whatever it is you're doing, from there.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,982
Messages
5,834,682
Members
430,310
Latest member
Excelorate3305

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top