Hi i'm facing a subscript out of range error when I tried to copy a chart from excel sheet to word document. I checked the worksheet name it seems to be fine. I tried changing to ThisWorkbook.Sheets(wsname) but i received an application define type error. How do I go about resolving this issue.
With ThisWorkbook.Worksheets(wsName)
.ChartObjects(1).Activate
ActiveChart.ChartArea.Copy
End With
The name of the worksheet I'm trying to reference
I have finally figured out what's the problem.
Since I'm referencing a chartsheet and not a worksheet, I have to use .Charts instead of .Worksheets. In addition since it is a chartsheet, the chartsheet itself is a chart object, hence .ChartObjects(1) is not required, only .ChartArea.Copy
Correct Code:
With ThisWorkbook.Charts(wsName)
.ChartArea.Copy
End With
Related
So, sometimes when I try to execute this command, it gives me an error. The problem is that it is very inconsistent. In certain cases, it works and in others, it just doesn't.
This is the line from getCellVal function. cellName is a string and s is an integer.
getCellVal = Sheets(s).Range(cellName).Value
This time it is giving me:
Run-time error '438':
Object doesn't support this property or method
This line actually worked without problems moments ago. I added some other functions that use it and now it's not working anymore.
Any ideas about why?
Unqualified calls into the Sheets collection implicitly refer to whatever workbook is currently active, and it's important to know that the collection contains Worksheet objects, ...but also Chart objects.
Since you mean to work with a Worksheet, use the Worksheets collection instead.
If you're not getting an "index out of bounds" error, then the sheet you asked for does exist. But error 438 points to that sheet not being a Worksheet (and thus not having a Range member).
I bet the active workbook has a chart sheet at index s.
The solution is simply to be explicit.
If you mean to work with the workbook that contains the code that's running, qualify Workbook member calls with ThisWorkbook:
getCellVal = ThisWorkbook.Worksheets(s).Range(cellName).Value
If you mean to work with a workbook, then you need to get ahold of the Workbook object when you open it:
Dim wb As Workbook
Set wb = Application.Workbooks.Open(path)
'...
getCellVal = wb.Worksheets(s).Range(cellName).Value
How can I select data range for a chart from different sheet using VBA? Suppose that data sheet name is data_sheet, chart sheet name is chart_sheet, and my data range is A1:A20. How can I do this in excel? I checked THIS but didn't work for different sheets. Otherwise, I checked THIS but returned this error: Subscript out of range:
With Worksheets("chart_sheet")
ActiveChart.SetSourceData Source:=Worksheets("data_sheet").Range("A1:A20")
End With
Assuming "chart_sheet" is the name of your Chart and "data_sheet" is the name of your Worksheet, I think you want to do the following:
Charts("chart_sheet").SetSourceData Source:=Worksheets("data_sheet").Range("A1:A20")
Your With block was not doing anything useful - the purpose of a With block is to allow you to just type . as a shortcut for something like Worksheets("data_sheet")..
So something like:
With Sheets("chart_sheet")
.SetSourceData Source:=Worksheets("data_sheet").Range("A1:A20")
End With
would work, because the .SetSourceData is an abbreviation of Sheets("chart_sheet").SetSourceData. (Notice also that the Sheets collection contains both Worksheets and Charts objects, so Charts("chart_sheet") and Sheets("chart_sheet") both point to the same thing.)
ActiveChart refers to the currently active chart, just as ActiveSheet returns to the currently sheet. If you don't have a chart active when that piece of code executes, you will get an error.
So the following piece of code would also probably have worked for you:
Sheets("chart_sheet").Activate
ActiveChart.SetSourceData Source:=Worksheets("data_sheet").Range("A1:A20")
as chart_sheet is probably not a worksheet, did you try this ?
with sheets("chart_sheet")
Background:
Every month I need to run a report. However, before I can do this, I must export some data in to excel. Once the data is in excel, I have to alter some columns and get the format correct before it is appended to the end of another document to do some analysis on.
What I would like:
I would like to have the document that I append the data to open. I will then export the data in to excel from my program (excel just opens with the data and it is not saved anywhere) and from my larger document, run a VBA script that will alter the data on the other workbook (Book1) so that it can be copied over to the analysis document when in the correct format.
What I have so far:
I have started basic. So far all I am trying to do is set all the cells to the correct height to make it easier to read. However, when I run this code, I get:
Run-time error '9':
Subscript out of range
The code I have so far is:
Sub Data_Ready_For_Transfer()
' Format all cell heights to 15
With Workbooks("Book1.xlsm").Worksheets("Sheet1")
Cells.RowHeight = 15
End With
End Sub
It appears to be having issues with the With Workbooks("Book1.xlsm").Worksheets("Sheet1") part of the code. I have also tried With Workbooks("Book1").Worksheets("Sheet1") and I have tried this with the open, unsaved document and a saved version of the workbook.
Am I missing something obvious?
As follow up from comments, workbook Book1 was opened in another instance of Application object.
In that case this code should work (for already opened workbook):
Sub Data_Ready_For_Transfer()
Dim wb As Workbook
Set wb = GetObject("Book1")
'if we get workbook instance then
If Not wb Is Nothing Then
With wb.Worksheets("Sheet1")
.Cells.RowHeight = 15
End With
End If
End Sub
One more issue, I've changed Cells.RowHeight = 15 to .Cells.RowHeight = 15 to specify, that Cells belongs to workbook Book1 sheet Sheet1.
Charts("Vendor").ChartTitle.Text = "Test"
Is throwing subscript error. The chart does exist and is named Vendor.
Any ideas?
Chart is a child object of a ChartObject (which like #DerekCheng alluded to, are contained within a worksheet); therefore, you'll need to get the Chart directly from there. Try this instead.
Worksheets("YourSheetName").ChartObjects("Vendor").Chart.ChartTitle.Text = "Test"
I'm trying to duplicate some code a former coworker of mine wrote in an excel spread sheet, but I'm a little new to VBA so I'm having trouble parsing it out. Here's the part of the code I'm confused about.
Private Sub cbWriteIES_Click()
Dim Hangle, Vangle As Double
Dim header(1 To 15) As String
'ASSIGN HEADER TO VARIABLE
For i = 1 To 15
header(i) = Sheet1.[header].Cells(i, 1) & Sheet1.[header].Cells(i, 2)
Next i
Can someone explain to me what's happening in the for loop? It runs fine in the original workbook, but when I copy and paste into my workbook it throws a 424 object required error. It looks to me like he's associated an object with Sheet1 called [header] but I can't seem to find where he did that. Any insight into this would be greatly appreciated. Thanks!
"header" is a named range on sheet1. If yo uare using excel 2010 you can go to the "Formulas" tab and click "Name Manager" to see the named range.
The for loop is setting the array "header" to the first 15 rows in the second column of the range named "header" on sheet1.
The 424 Object Required error is coming up because you do not have a range named "header" on sheet1 in the workbook you copied this to.