PDA

View Full Version : Application Defined or Object Defined Error



NeverQuit101
07-19-2013, 07:06 AM
I am very new to VBA having only been using it for about a week with my only background in coding coming from a course on Java. So please forgive me if this code is absolutely terrible and makes your eyes bleed.

Anyway, I have a sheet and I am focusing on 4 main columns. I happen to be looking at an insurance policy inventory and the first 2 columns are plan code and duration. So for each plan code, there are several sets of durations that increment from, say, 1 to 99 until starting over. The other two columns contain data that I wish to put in a graph.

Sub Graph_Create()
'
' Graph_Create Macro
'
'
Dim Plan As String
Dim Duration As Double
Dim TempDuration As Double
Dim Count As Double
Dim TempCount As Double
Dim X As Double

Count = 2
Plan = Range("B" & Count).Value

Do While Plan <> (" ")
Sheets("Access Data").Select
Duration = Range("F" & Count).Value
TempDuration = Duration
TempCount = Count
X = 0
Do While TempDuration = Duration + X
TempCount = TempCount + 1
TempDuration = Range("F" & TempCount).Value
X = X + 1
Loop
Charts.Add
ActiveChart.ChartType = xlLine
ActiveChart.SetSourceData Source:=Sheets("Access Data").Range("'F' & Count:'F' & (TempCount - 1),'H' & Count: 'H' & (TempCount - 1)")
Count = TempCount
Plan = Range("B" & Count).Value
Loop


End Sub

So the idea here is that while it is in a certain plan code, it cycles through the durations using Count as a place holder and TempCount to increment until it reaches a new duration set. Because durations increment by 1, a new duration set will be when the new duration (TempDuration) does not equal my duration place holder (Duration) plus the number of cycles through the duration loop (X). This indicates the duration count is restarting.
I then want it to use Count and TempCount as markers for the beginning and end of data in the two colums that I wish to put in a chart on a new page. Now my main problem seems to be coming from the SetSourceData line. I have been messing around with it and I keep getting Application Defined or Object Defined Error. And in some of my attempts to change it I get Method Range of object Global failed.

I am sure I have more problems than this though and any help is greatly appreciated. Again, sorry if this code is really terrible, and sorry if this post is in the wrong place.

EDIT: I also realized that I am really only using 3 columns of data, not 4 like I said, because one of the columns I want in the graph is duration which I am also sorting through by

SamT
07-19-2013, 08:21 AM
I could be wrong, but...
About this line
ActiveChart.SetSourceData Source:=Sheets("Access Data").Range("'F' & Count:'F' & (TempCount - 1),'H' & Count: 'H' & (TempCount - 1)")

This is the actual Name of the Range you're assigning to SourceData

'F' & Count:'F' & (TempCount - 1),'H' & Count: 'H' & (TempCount - 1) Note: Ampersands, colons, and spaces are not allowed in Defined Names.

I think you're looking for a rangeRange("F" & TempCount - 1 & ":H" & TempCount - 1) Which will give you Range(F42:H42"), where 42 is the answer to everything.

I don't know what you're trying to return with Count:'F' & (TempCount - 1)


Hitchhikers trivia you never cared about: 42 is the number of Egyptian "Laws of Living Good" in Moses' time.

NeverQuit101
07-19-2013, 09:03 AM
Thanks for the response!

Ok so something that was really messing with me was that in the data I want two non-adjacent columns. So I want column F and H in the graph data, but not G. So if I had a range like you said Range("F42:H42"), that would also include column G would it not? So what I did was I recorded a macro where I highlighted the columns I wanted and it gave me this in the VBA code.


ActiveChart.SetSourceData Source:=Range( _
"'Access Data'!$F$2:$F$45,'Access Data'!$H$2:$H$45")


So I was trying to mimic that in selecting the range, but with Count and TempCount instead. That's why I had that general format ("F2:F45, H2:H45") and then I wasn't sure how to substitute in Count and TempCount for the actual numbers.

SamT
07-19-2013, 10:09 AM
Use:ActiveChart.SetSourceData Source:=Range( _
"'Access Data'!F" & TempCount - 1 & ",'Access Data'!H" & TempCount - 1)
Note: Complete Strings with punctuation enclosed in double quotes.

Variables and Math are not

Alternately use:Dim FStr As String
Const HStr As String = ",'Access Data'!H"
FStr = "'Access Data'!F"
ActiveChart.SetSourceData Source:=Range(FStr & TempCount - 1 & HStr & TempCount - 1) Shows two ways of setting FStr and HStr

My prefered alternate:Dim FRng As String
Dim HRng As String
'code
'code
'code
FRng = "'Access Data'!F" & TempCount - 1
HRng = "'Access Data'!H" & TempCount - 1
ActiveChart.SetSourceData Source:=Range(FRng & "," & HRng) Since the Comma is not part of any Range.