473,396 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to find range of an excel sheet???????

Hi All,

I am developing one application in VBScript which automatically generates excel charts. But, I have a problem here.
I need to get the range value(like A1:B50) before calling the Activechart.setSourcedata function.....
In the below code I am asking the user to enter the range.
But, It is not requiremnet, The user just gives the .xls file. We need to get the range of the data.
Please give me the code which gets the range of an excel sheet.
thanx in advance....................

------------------------------VBCode--------------------

AutoChart()

sub AutoChart()

dim app, chartType
set app = createobject("Excel.Application")

'Make it visible
app.Visible = true

'Ask for file name
sFilename = app.GetOpenFilename("All files (*.*), *.*")

'To Exit if Cancel was pressed
If sFilename = "False" Then Exit Sub


' Add a new workbook
dim wb
set wb = app.workbooks.open(sFilename)

'Read the range
Dim inRng
inRng = InputBox("Enter the Range ( Ex: A1:F50 )", "Range")

'Get the range
dim rng
set rng = wb.ActiveSheet.Range(inRng)


'Add a new chart
wb.Charts.Add
'Assgin the chart type
wb.ActiveChart.ChartType = 65

'Set the SourceData
wb.ActiveChart.SetSourceData rng, 2

' Give the user control of Excel
app.UserControl = true

end sub

-----------------------------end----------------------------
Oct 29 '07 #1
2 2420
Hi I have tried with the following code .............
But it only works when the data starts from A1..... otherwise it fails....

--------------------
Dim col, row, i, j
col = ""
j=1
for i = 1 to 50
If app.Cells(i, j).Value <> "" then
j=j+1
else Exit For
end if
Next

j=j-1
select case j
case 1
col = "A"
case 2
col = "B"
case 3
col = "C"
case 4
col = "D"
case 5
col = "E"
case 6
col = "F"
case 7
col = "G"
case 8
col = "H"
case 9
col = "I"
case 10
col = "J"
case 11
col = "K"
case 12
col = "L"
case 13
col = "M"
case 14
col = "N"
case 15
col = "O"
case 16
col = "P"
case 17
col = "Q"
case 18
col = "R"
case 19
col = "S"
case 20
col = "T"
case 21
col = "U"
case 22
col = "V"
case 23
col = "W"
case 24
col = "X"
case 25
col = "Y"
case 26
col = "Z"

end select

j=1
for i = 1 to 3000
If app.Cells(i, j).Value <> "" then
i=i+1
else Exit For
end if
Next
row = i-1

Dim range
range = "A1:"&col&row

msgbox range
---------------------------
Oct 29 '07 #2
kadghar
1,295 Expert 1GB
Hi I have tried with the following code .............
But it only works when the data starts from A1..... otherwise it fails....
Working on your very code, try using a DO instead of a FOR, and use two variables...

Expand|Select|Wrap|Line Numbers
  1. dim row1 as long
  2. dim row2 as long
  3.  
  4. row1=1
  5. do
  6.     if cells(row1,1) <>"" then exit do
  7.     row1=row1+1
  8. loop until row1 >= 65000 'i dont remember the number of rows in a sheet
  9. row2 = row1+1
  10. do
  11.     if cells(row2,1) = "" then exit do
  12.     row2=row2+1
  13. loop until row2 >= 65000 'Same here
And i also recomend you to create a function that gives you back the Excel range, use ASCII code and the CHR and ASC functions for that instead of a case select.

HTH

**PS: remember that your range will go from row1 to (row2 - 1) since row2 is already an empty one.
Oct 29 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: moller | last post by:
I'm new to perl but not to programming in general. I hav been trying to put some data into an excel sheet. And it works fine.... if I hardcode the data. And I of cource I dont want to do that....
3
by: zxo102 | last post by:
Hi there, I need your help for python <--> excel. I want to paste selected cells (range) to different location on the same sheet in Excel through python. I have tried it for a while but could not...
4
by: Bill R via AccessMonster.com | last post by:
I get this Run-Time Error 1004 whenevery the following code runs: On Error GoTo XLSheet2 Set objXL = CreateObject("Excel.Application") With objXL Set objWkb = .Workbooks.Open(strPath) With...
10
by: Maik | last post by:
Hello, I've got a problem with access to special ranges. I renamed some cells (for example "C4" in "CUSTOM.GENERAL.VPRIM"). This is necessary, because I have to read out only these cells in...
0
by: pmclinn | last post by:
The code below takes the data from the S column and grabs all the data and graphs it. The problem is this example grabs 500 cells worth of data and some of these cells = nothing. How do I rescope...
6
by: XxLicherxX | last post by:
Hello everyone - new to VB.net working with Excel I am trying to populate data into cells in an Excel spreadsheet using Range.Value. No matter what cell I give as an argument to the Range...
1
by: Socko | last post by:
I'm trying to fix an sub routine in an VB module that basically reads in a MS database and writes it to an Excel Spread sheet. It works just fine except that the data isn't sorted correctly. I have...
0
by: rogerford | last post by:
1) I have a Namedrange which is named Company in sheet 2. Sheet 1 cell A2 has a list created via data validation referencing to this sheet 2 Column A values. 2) If i go to Name Manager (by doing a...
0
by: CoreyReynolds | last post by:
Hey all, I have a piece of code that dumps a bunch of data into a spreadsheet. Also rearranges it into a pivot table and then graphs the pivot table as well so my boss can get a clear view of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.