I'm trying to insert a chart into my spreadsheet, as outlined in msdn here:
https://msdn.microsoft.com/en-us/library/bb238877(v=office.12).aspx
Unfortunately i get the error in the
Method 'SetSource Data' of Object' _chart' failed
on the line starting ActiveChart. Why is it doing this? i've tried both string and range variables here to no avail.
In addition to the fact that i can't get this method to work, i don't like the fact you need to select the select the graph, surely there is a better way?
Function TimeSeries(rngToPrint As Range)
Dim strRange As String
Dim rngChart As Range
lngstartrow = 8
lngendrow = Range("a10000").End(xlUp).Row
Range("$A$" & CStr(lngstartrow) & ":$B$" & CStr(lngendrow)).Select
Sheets(rngToPrint.Worksheet.Name).Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("$A$" & CStr(lngstartrow) & ":$B$" & CStr(lngendrow)), PlotBy:=xlLine
End Function
The PlotBy argument specifies whether to plot the data by rows or columns. So the argument should be set to either xlRows or xlColumns.
The error appears at the following line because there is no PlotBy:=xlLine
Modify it to:
ActiveChart.SetSourceData Source:=Range("$A$" & CStr(lngstartrow) & ":$B$" & CStr(lngendrow)), PlotBy:=xlRows
Or either:
ActiveChart.SetSourceData Source:=Range("$A$" & CStr(lngstartrow) & ":$B$" & CStr(lngendrow)), PlotBy:=xlColumns
Related
I know this topic was already asked and I tried to copy on how to insert a formula in one cell, however, I got an error in my vba code.
Here is my code:
ws.Range("C9").Formula = "=CountIf(wsRD.Range(C & Rows.count).End(xlUp).Row, ""Event"")" 'CountIf(wsRD.Range("C" & Rows.count).End(xlUp).Row, "Event") 'count(Search("Event", wsRD.Range("C" & Rows.count).End(xlUp).Row, 1))
I need to insert a formula in ws.Range("C9"), in which, it summarizes the count of the cell that have a value of "Event" in wsRD.Range("C" & Rows.count).End(xlUp).Row. May I know what's the problem in my code? Appreciate your help.
Thank you.
You could get rid of the LRow variable and just drop it in your equation if you wanted to.
Dim LRow as Long
LRow = ws.Range("C" & ws.Rows.Count).End(xlUp).Row
ws.Range("C9").Formula = "=COUNTIF(C10:C" & LRow & ", ""Event"")"
I'm sure this could be the correct answer
ws.Select
LRow = ws.Range("C" & Rows.Count).End(xlUp).Row
Range("C9").FormulaLocal = "=COUNTIF(C10:C" & LRow & ";""Event"")"
So basically, I used FormulaLocal to write the formula the same way I write it in Excel, then, because the formula must be a big String, I separated it in 2 strings, put the value LRow, and used & & to concatenate
This works
mySheet.Rows(CStr(activeRow) & ":" & CStr(activeRow + deletedRowAmount)).Delete
This does not work
mySheet.Columns(CStr(columDelete) & ":" & CStr(columDelete + deletedRowAmount)).Delete
What I am missing here?
ERROR is VBA Runtime Error 1004 “Application-defined or Object-defined error”
Columns are A,B,C,... when used in a string as you are using them. If they are numbers you will need to do it slightly different:
With mySheet
.Range(.cells(1,columDelete), .cells(1,columDelete + deletedRowAmount)).EntireColumn.Delete
End With
In general, what Scott Craner says is quite enough, but here is a way to go around it and make it easier:
Sub TestMe()
Columns(GetColumnRange(2, 5)).Delete
End Sub
Public Function GetColumnRange(colStart As Long, colEnd) As String
GetColumnRange = Split(Cells(1, colStart).Address(True, False), "$")(0) & _
":" & Split(Cells(1, colEnd).Address(True, False), "$")(0)
End Function
Try the TestMe function, it should delete the columns from 2(B) to 5(E).
I am performing a linear interpolation for my class project. I have created an interpolation function and have to perform calculation dynamically, as the number of column varies for each problem. So, I have retrieved the value for last column(ltr) and trying to concatenate it with R1C1 format. But it doesn’t work. Could you please suggest some idea, how do workaround for this issue.
Please find below the following code:
Private Sub TrialCheck_Click()
Dim lrt As Double
With ActiveSheet
'retrives last column i.e lrt = 447
lrt = .Cells(.Rows.Count, "F").End(xlUp).Row
End With
Range("I3").Value = Range("F3").Value * Range("B3").Value
Range("I58").Value = Range("F" & lrt).Value * Range("B58").Value
'MacroR
'following works as 447 is hardcoded
'Range("I4").Value = _
"=(LinInterp(RC[-8],R4C[-4]:R447C[-4],R4C[-3]:R447C[-3])*RC[-7])"
'following code doesn't concatenate value of lrt
Range("I4").Value = _
"=(LinInterp(RC[-8],R4C[-4]:R&lrt&C[-4],R4C[-3]:R&lrt&C[-3])*RC[-7])"
Range("J4").Select
Range("I4").AutoFill Destination:=Range("I4:I57"), Type:=xlFillDefault
Range("I4:I57").Select
End Sub
you have to do string concatenation
"string" & lrt & "string" & lrt & "string"
Range("I4").Value = _
"=(LinInterp(RC[-8],R4C[-4]:R" & lrt & "C[-4],R4C[-3]:R" & lrt & "C[-3])*RC[-7])"
I am writing a small script and I would like to insert a function to a specific cell. I have tried the following piece of code:
end_time = ActiveCell.Offset(0, 2).Address
time_dif = "=TEXT(" + end_time + "-" + st_time + ";""[ωω]:μμ:δδ"")"
found = False
Workbooks("Template.xlsx").Worksheets(1).Range(ActiveCell.Offset(0, 3).Address).Select
'Var = ActiveCell.formula
ActiveCell.formula = time_dif 'Var
The last line in the above code is throwing an error:
'Application-Defined or Object-Defined Error'
The time_dif variable contains the following string:
"=TEXT($H$19-$H$13;"[ωω]:μμ:δδ")"
as seen in the local variables window
I have tried many different ways for the above to make it work but unfortunattely all of them failed.
What it actually worked is putting the exact same formula in another cell. Pause the execution an activate the cell with the formula. Put the formula in the variable Var and then move the activecell to the correct position and insert the variable Var value there.
Var = ActiveCell.formula 'Break point here / Run one step to take formula
ActiveCell.formula = Var 'Move to correct cell manually / Continue execution
Another thing I tried was removing the "=" from the formula and it puts the formula in the cell correctly as string.
Any advise or ideas about this?
The VBE uses English syntaxes.
Try using a sub to get the English syntax, e.g:
Sub ert()
InputBox Prompt:=" ", Default:=Selection.Formula
End Sub
The correct answer thus will be
"=TEXT(" + end_time + "-" + st_time + ",""[ωω]:μμ:δδ"")"
with the "," replacing the ";".
have you tried ActiveCell.FormulaR1C1 ?
Here you might change to include Chr(34), in your string, but as it seems to display correctly, I don't think that's the problem...
"=TEXT(" & end_time & "-" & st_time & ";" & Chr(34) & "[hh]:mm:ss" & Chr(34) & ")"
I have A column and B column, They compare their previous values And Iam comparing it with the following formulae
=IF((OR(AND(A2=A1,B2=B1),K2=0),0,1) it puts the 0 or 1 on the coresponding Q column
so when it goes to the 5th cell then it becames
=IF((OR(AND(A5=A4,B5=B4),K5=0),0,1)
But im trying to apply it in my VBA code like these
For numm = 2 To lastRow
Sheet1.Cells(numm, "Q").Value = ="IF(OR(AND(sheet1.cells(numm,""A"").value=sheet1.cells(numm-1,""A"")simlar way becolumn),sheet1.cells(numm,""k"").value=0),1,0)"
Next numm
But Im unable to peform the action it says 1004 error and object error
How do i use cells(numm,"A") in my VBA formulae or atleast any other way to put my formula and make it work
The reference to the looping numm within your formula needs to be out of the string.
Maybe you can set in VBA the cell formula itself...
For numm = 2 To lastRow
Sheet1.Cells("Q" & numm).Formula = _
"=IF((OR(AND(A" & numm & "=A" & numm - 1 & ",B" & numm & _
"=B" & numm - 1 & "),K" & numm & "=0),0,1)"
Next numm
Personally, I'd do the whole statement within VBA (ifs, ors, ands) and just drop the value back to Excel. Using Excel formulas makes the code harder to read.
Are you looking for something like:
Public Sub test()
Dim Sheet1 As Excel.Worksheet
Set Sheet1 = ActiveSheet
Dim numm As Integer, lastrow As Integer
lastrow = 5
For numm = 2 To lastrow
Sheet1.Cells(numm, "Q").Value = "=IF(OR(AND(" & Sheet1.Cells(numm, "A").Address & "=" & Sheet1.Cells(numm - 1, "A").Address & "," & Sheet1.Cells(numm, "B").Address & "=" & Sheet1.Cells(numm - 1, "B").Address & ")," & Sheet1.Cells(numm, "k").Address & "=0),1,0)"
Next numm
End Sub
Sheet1 can't be referred to inside the value of the cell, it only exists in the vba function you're creating, so instead you can append the strings together, and just get the cell addresses out to match the sort of function you were creating earlier.