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

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
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.
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

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
Back
Top