VBA: Creating series in a chart - vba

I am trying to create two different series but if I run the macro the second series keeps overwriting the first one unfortunately. So that at the end, I only have the second series in my chart. Can anyone help?
With ChtObj
'Series LTU
Set Ser = .Chart.SeriesCollection.NewSeries
With Ser
.Name = "=" & Dataws.Cells(CurrentRow, 1).Address(False, False, xlA1, xlExternal)
.XValues = "=" & Dataws.Cells(CurrentRow, 3).Address(False, False, xlA1, xlExternal)
.Values = "=" & Dataws.Cells(CurrentRow, 4).Address(False, False, xlA1, xlExternal)
End With
'Series LTA
With Ser
.Name = "LTA_" & Dataws.Cells(CurrentRow, 1)
.XValues = "=" & Dataws.Cells(CurrentRow, 3).Address(False, False, xlA1, xlExternal) & "," & Dataws.Range(Dataws.Cells(CurrentRow, 14), Dataws.Cells(CurrentRow, 22)).Address(False, False, xlA1, xlExternal)
.Values = "=" & Dataws.Cells(CurrentRow, 4).Address(False, False, xlA1, xlExternal) & "," & Dataws.Range(Dataws.Cells(CurrentRow, 42), Dataws.Cells(CurrentRow, 50)).Address(False, False, xlA1, xlExternal)
End With
End With
Thank you!

The problem is you are using the same Ser, so this is replaced.
With ChtObj
Set Ser = .Chart.SeriesCollection.NewSeries
With Ser
.Name = "=" & Dataws.Cells(CurrentRow, 1).Address(False, False, xlA1, xlExternal)
.XValues = "=" & Dataws.Cells(CurrentRow, 3).Address(False, False, xlA1, xlExternal)
.Values = "=" & Dataws.Cells(CurrentRow, 4).Address(False, False, xlA1, xlExternal)
End With
'Series LTA
Set Ser = .Chart.SeriesCollection.NewSeries
With Ser
.Name = "LTA_" & Dataws.Cells(CurrentRow, 1)
.XValues = "=" & Dataws.Cells(CurrentRow, 3).Address(False, False, xlA1, xlExternal) & "," & Dataws.Range(Dataws.Cells(CurrentRow, 14), Dataws.Cells(CurrentRow, 22)).Address(False, False, xlA1, xlExternal)
.Values = "=" & Dataws.Cells(CurrentRow, 4).Address(False, False, xlA1, xlExternal) & "," & Dataws.Range(Dataws.Cells(CurrentRow, 42), Dataws.Cells(CurrentRow, 50)).Address(False, False, xlA1, xlExternal)
End With
End With
Try defining a new series again or use a different name for Series2.

Related

How to Link Contents from Contents in an Excel workbook to another Excel workbook using VBA?

I have built a VBA code that links cell contents from one Excel sheet Employee Form to another worksheet Employee Database in the same workbook as below.
Sub Submit_Form()
Dim LastRow As Long, ws As Worksheet
Set ws = Sheets("Employee Database")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1
ws.Range("A" & LastRow).Value = Worksheets("Employee Form").Range("D7").Value
....
ws.Range("Z" & LastRow).Value = Worksheets("Employee Form").Range("AB").Value
End Sub
The code works well, but how to modify the code now that these sheets are in separate Excel files?
The new Excel files are the same as the respective names of these worksheets.
Thanks!
We could try to code below:
Sub connect()
'
' connect Macro
'
'
ActiveWorkbook.Queries.Add Name:="Sheet1", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Excel.Workbook(File.Contents(""D:\Try\write.xlsx""), null, true)," & Chr(13) & "" & Chr(10) & " Sheet1_Sheet = Source{[Item=""Sheet1"",Kind=""Sheet""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Sheet1_Sheet,{{""Column1"", Int64.Type}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
Sheets.Add After:=ActiveSheet
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Sheet1;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Sheet1]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = False
.ListObject.DisplayName = "Sheet1"
.Refresh BackgroundQuery:=False
End With
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
End Sub
You can also link to another worksheet by doing an action:
Connect to another workbook
Hope that helps!

VBA: How to create a series in a chart with more than 1 X value

I am trying to create a new series with VBA that will have more than 1 X-value.
It's no problem when creating a series with only 1 X-value but it doesn't work with more than that. Can anyone help?
Works:
.XValues = "=" & Dataws.Cells(CurrentRow, 3).Address(False, False, xlA1, xlExternal)
Doesn't work (run time error 1004):
.XValues = "=" & Dataws.Cells(CurrentRow, 3) & Dataws.Cells(CurrentRow, 14) & Dataws.Cells(CurrentRow, 15) & Dataws.Cells(CurrentRow, 16) & Dataws.Cells(CurrentRow, 17) & Dataws.Cells(CurrentRow, 18) & Dataws.Cells(CurrentRow, 19) & Dataws.Cells(CurrentRow, 20) & Dataws.Cells(CurrentRow, 21) & Dataws.Cells(CurrentRow, 22).Address(False, False, xlA1, xlExternal)
Thank you!
The addresses for the XValues need to be comma separated.
The result should look something like: =[Map1]Sheet1!C1,[Map1]Sheet1!N1:V1
So you need to connect two addresses with & "," &.
.XValues = "=" & dataws.Cells(CurrentRow, 3).Address(False, False, xlA1, xlExternal) & "," & _
dataws.Range(dataws.Cells(CurrentRow, 14), dataws.Cells(CurrentRow, 22)).Address(False, False, xlA1, xlExternal)
Note that continuous cells can be combined to one range address like:
dataws.Range(dataws.Cells(CurrentRow, 14), dataws.Cells(CurrentRow, 22))
which means cell 14 up to cell 22. So you don't need to add them separately.

application.worksheetfunction.vlookup error 1004?

i have a debug in my code, but i cannot figure out why it is happening, could you please review the code and see where i messed up? Note the error debug is happening on the ActiveCell.FormulaR1C1 line.
'ENRICHMENT CODE FOR VARIOUS TITLES
For Each wbtitle In wbrange
sThisWorkTitle = wbtitle
sThisWorkColumnNum = wbtitle.Column
sThisWorkColumnNam = Split(Cells(, sThisWorkColumnNum).Address, "$")(1)
'identifying CASH RADICAL COLUMN LETTER
If sThisWorkTitle = "Account Cash Radical" Then
scashradicalcolumnnam = Split(Cells(, sThisWorkColumnNum).Address, "$")(1)
Else
'do nothing
End If
''' CASH RELATED?
If sThisWorkTitle = "Cash Related?" Then
wbtitle.Select
Range(sThisWorkColumnNam + gspstart).Select
ActiveCell.FormulaR1C1 = Application.WorksheetFunction.VLookup(Range(scashradicalcolumnnam & ActiveCell.Row), Range(scashradicalcolumnnam & immsstart & ":" & scashradicalcolumnnam & immsfinal), 1, False)
ActiveCell.AutoFill Range(ActiveCell.Address, Cells(gspfinal))
Columns(sThisWorkTitle).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Else
'do nothing
End If
Scott has highlighted the problem line.
You could try amending so an actual formula is inserted into the cell.
So instead of the following:
ActiveCell.FormulaR1C1 = _
Application.WorksheetFunction.VLookup( & _
Range(scashradicalcolumnnam & ActiveCell.Row) & _
, Range(scashradicalcolumnnam & immsstart & ":" & scashradicalcolumnnam & immsfinal), 1, False)
Something like:
ActiveCell = "=vlookup(" & scashradicalcolumnnam & ActiveCell.Row & _
"," & scashradicalcolumnnam & immsstart & ":" & _
scashradicalcolumnnam & immsfinal & _
", 1, False)"

Runtime error 1004 with Count and Countif formula VBA

I am getting run time error 1004 when converting the following formula to code:
When I try to consider the only Count forumlas, it was working.
Formula:
=(COUNT(C25:C26)-COUNTIF(C25:C26,0))/(COUNT(E25:E26)-COUNTIF(E25:E26,0))
Code:
Sheets("Sheet1").Cells(29, 3).Formula = "=(COUNT(" & Sheets("Sheet1").Range(Cells(25, 3), Cells(26, 3)).Address(False, False) _
& "- COUNTIF(" & Sheets("Sheet1").Range(Cells(25, 3), Cells(26, 3)).Address(False, False) & ",0 )/" & _
"(COUNT(" & Sheets("Sheet1").Range(Cells(25, 5), Cells(26, 5)).Address(False, False) _
& "- COUNTIF(" & Sheets("Sheet1").Range(Cells(25, 5), Cells(26, 5)).Address(False, False) & ",0 ))"
You forgot some brakets/parenthesis and let spaces.
With Sheets("Sheet1")
.Cells(29, 3).Formula = _
"=(COUNT(" & .Range(.Cells(25, 3), .Cells(26, 3)).Address(False, False) _
& ")-COUNTIF(" & .Range(.Cells(25, 3), .Cells(26, 3)).Address(False, False) & ",0))/" & _
"(COUNT(" & .Range(.Cells(25, 3), .Cells(26, 3)).Address(False, False) _
& ")-COUNTIF(" & .Range(.Cells(25, 3), .Cells(26, 3)).Address(False, False) & ",0))"
End With
Use a string variable to debug and prompt it with a MsgBox.
Dim StrFormula As String
With Sheets("Sheet1")
StrFormula = _
"=(COUNT(" & .Range(.Cells(25, 3), .Cells(26, 3)).Address(False, False) _
& ")-COUNTIF(" & .Range(.Cells(25, 3), .Cells(26, 3)).Address(False, False) & ",0))/" & _
"(COUNT(" & .Range(.Cells(25, 3), .Cells(26, 3)).Address(False, False) _
& ")-COUNTIF(" & .Range(.Cells(25, 3), .Cells(26, 3)).Address(False, False) & ",0))"
End With
MsgBox StrFormula
Don't make it to confusing,
Range("C29") = _
"=(COUNT(Sheet1!R[-4]C:R[-3]C)-COUNTIF(Sheet1!R[-4]C:R[-3]C,0))/(COUNT(Sheet1!R[-4]C[2]:R[-3]C[2])-COUNTIF(Sheet1!R[-27]C[2]:R[-4]C[2],0))"
'or
Range("C29") = _
"=(COUNT(Sheet1!C25:C26)-COUNTIF(Sheet1!C25:C26,0))/(COUNT(Sheet1!E25:E26)-COUNTIF(Sheet1!E2:E25,0))"
Further to my comments, this is how you will qualify your cells object. Notice the DOT before cells?
Tested
Sub Sample()
Dim ws As Worksheet
Dim sFormula As String
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
sFormula = "=(COUNT(" & _
.Range(.Cells(25, 3), .Cells(26, 3)).Address(False, False) & ")" & _
"- COUNTIF(" & _
.Range(.Cells(25, 3), .Cells(26, 3)).Address(False, False) & ",0 ))/" & _
"(COUNT(" & _
.Range(.Cells(25, 3), .Cells(26, 3)).Address(False, False) & ")" & _
"- COUNTIF(" & _
.Range(.Cells(25, 3), .Cells(26, 3)).Address(False, False) & ",0 ))"
Debug.Print sFormula
.Cells(29, 3).Formula = sFormula
End With
End Sub

VBA Macro for Excel working incorrectly

I am working on a macro which does some mathematical calculations and export the data to a row in another sheet in the same excel workbook.
At the beginning, I wrote a macro which on button click will export some data to a row in an excel sheet and upon clicking the same button again, it replaces/overwrites the data previously exported row with the new data but I tried to change the macro such that it exports the data to the next available row instead of overwriting the previous data using the following macro
Sub WJArchive()
'
' WJArchive Macro
'
Dim LastCell As Range
Dim LastCellRowNumber As Long
Set LastCell = ActiveSheet.Cells(ActiveSheet.Rows.Count, "B").End(xlUp)
LastCellRowNumber = LastCell.Row + 1
Sheets("WJ LOGS").Select
Range("B" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R4C2"
Range("C" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R5C2"
Range("D" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R6C2"
Range("E" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R8C2"
Range("F" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R9C2"
Range("G" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R10C2"
Range("H" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R11C2"
Range("I" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R12C2"
Range("J" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R13C2"
Range("K" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R14C2"
Range("L" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R17C2"
Range("M" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R18C2"
Range("N" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R19C2"
Range("O" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R20C2"
Range("P" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R21C2"
Range("Q" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R22C2"
Range("R" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R23C2"
Range("S" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R26C2"
Range("T" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R27C2"
Range("U" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R28C2"
Range("V" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R29C2"
Range("W" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R30C2"
Range("X" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R31C2"
Range("Y" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R32C2"
Range("Z" & LastCellRowNumber).Select
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
Range("B" & LastCellRowNumber).Select
End Sub
For some reasons regardless of what the the last row that is available, the above macro exports the data to row 33 in the excel worksheet on a button click and on a next button click it just replaces the data in row 33 with new data and strangely this works very fine when I run this macro in developer mode when I compile it and assess it with a breakpoint.
The only issue is copying the data to row 33 in design mode.
Not sure, why am getting the issue.
Try removing the Select and ActiveCell methodology. And also add a reference to the Sheet.
E.g., replace
Range("H" & LastCellRowNumber).Select
ActiveCell.FormulaR1C1 = "='Price Sheet'!R11C2"
with
Sheet1.Range("H" & LastCellRowNumber).FormulaR1C1 = "='Price Sheet'!R11C2"
You are calculating LastCellRowNumber based in the wrong sheet. Replace:
Set LastCell = ActiveSheet.Cells(ActiveSheet.Rows.Count, "B").End(xlUp)
with:
Set LastCell = Sheets("WJ LOGS").Cells(Rows.Count, "B").End(xlUp)