VBA Chart Second Data Series - excel

I've got some code that I wrote a few years back now, that would create a bar chart in one tab, based on data in another tab. I've now been tasked with displaying the same chart but with a second data series.
I have tried multiple ways, but have been unable to get this to work. I was wondering if anyone could look at the code below and point me in the right direction.
With Sheets("DynamicCharts")
LastRow = .Range("B" & .Rows.Count).End(xlUp).row
Set Rng1 = .Range("B2:B" & LastRow & ", D2:D" & LastRow)
ShName = .Name
End With
With Sheets("Live Data").ChartObjects("County").Chart
.SetSourceData Source:=Rng1
.FullSeriesCollection(1).Name = "='" & ShName & "'!$C$1"
.FullSeriesCollection(1).Values = "='" & ShName & "'!$C$2:$C$" & LastRow
.FullSeriesCollection(2).Name = "='" & ShName & "'!$D$1"
.FullSeriesCollection(2).Values = "='" & ShName & "'!$D$2:$D$" & LastRow
.FullSeriesCollection(1).XValues = "='" & ShName & "'!$B$2:$B$" & LastRow
End With
Rows 10 and 11, .FullSeriesCollection(2) are the rows I've added, hoping that would add my second data series. However, I just get a debug message saying "Parameter not valid".
Any help would be much appreciated.
Thanks in advance

Think I managed to work it out for those that are looking to achieve the same results
With Sheets("DynamicCharts")
LastRow = .Range("B" & .Rows.Count).End(xlUp).row
Set Rng1 = .Range("B2:B" & LastRow & ", D2:D" & LastRow)
ShName = .Name
End With
With Sheets("Live Data").ChartObjects("County").Chart
.SetSourceData Source:=Rng1
.FullSeriesCollection(1).Name = "='" & ShName & "'!$C$1"
.FullSeriesCollection(1).Values = "='" & ShName & "'!$C$2:$C$" & LastRow
.FullSeriesCollection(1).XValues = "='" & ShName & "'!$B$2:$B$" & LastRow
.SeriesCollection.NewSeries
.FullSeriesCollection(2).Name = "='" & ShName & "'!$D$1"
.FullSeriesCollection(2).Values = "='" & ShName & "'!$D$2:$D$" & LastRow
.FullSeriesCollection(2).XValues = "='" & ShName & "'!$B$2:$B$" & LastRow
End With

I am thankful for sharing the code, I am thinking of making a for-loop which includes new series (new columns) to the right of existing ones, kind of like what you did, but for several graphs instead of just one.
I guess I will be using a "if-statement" for if there already exists a series (e.g. yours 1 and 2), so it can skip those (in case you have formatted them later and they are overwritten with new ones...).
The tricky part I struggle with is how to refer to column and row of a range into a formula. So instead of writing:
.FullSeriesCollection(2).Values = "='" & ShName & "'!$D$2:$D$" & LastRow
I would rather write something like:
For i = 1 to "Columns_in_Range"
.FullSeriesCollection(i).Values = "='" & ShName & "'!$...## (D) => substitute with column(i) ##...$6:$D$" & LastRow
I have here highlighted that I want to substitute the letter with a formula which then will work in a for-loop.
Regards Hallvard Skrede

Related

VBA using part of formula as a celll value

Failing againg with my project
I have formulas with variable Brand that is changed dynamically (AF Column). Basically all I want is to extract Brands into a column next (AE) to the formula column for visial convenience
For i = LBound(Brand) To UBound(Brand)
Range("AF" & i + 2).Formula = "=COUNTIFS(C:C," & RTrim(Month(Mesyaz3)) & _
",H:H,""Headphones"",F:F," & Chr(34) & Brand(i) & Chr(34) & ")"
Next i
Range("AF:AF").Sort Key1:=Range("AF2"), Order1:=xlDescending, Header:=xlYes
ActiveSheet.Range("AG2:AG8").Formula = ActiveSheet.Range("AF2:AF8").Formula
ActiveSheet.Range("AH2:AH8").Formula = ActiveSheet.Range("AF2:AF8").Formula
Dim ws As Worksheet
Set ws = Worksheets(1)
Dim j As Variant
j = Application.Match(""" & Brand(i) & """, ws.Range("AF2:AF8"))
ActiveSheet.Range("AE2").Value = Application.Index(ws.Range("AF2:AF8"), j, 0)
And I get #N/A Already lost two days for that. Would be enourmously grateful to anyone who could help.
It's not exactly clear from your question as to your desired output but here's a guess:
For i = LBound(Brand) To UBound(Brand)
Range("AF" & i + 2).Formula = "=COUNTIFS(C:C," & RTrim(Month(Mesyaz3)) & _
",H:H,""Headphones"",F:F," & Chr(34) & Brand(i) & Chr(34) & ")"
Range("AE" & i + 2).Value = Brand(i)
Next i
Range("AE:AF").Sort Key1:=Range("AF2"), Order1:=xlDescending, Header:=xlYes
I've added a line to write the brand to AE, and altered the Sort to accommodate this.

Syntax error for Range("J" & lastrow).Formula = "=Range"(H & lastrow" / 4000 & ")"

I am trying to write Range("H" & lastrow) / 4000
let's say lastrow = 150, i have tried H150 it works, however i need the number to be dynamically. So please help me
Range("J" & lastrow).Formula = "=Range"(H & lastrow" / 4000 & ")"
While your initial problem was that you are mixing VBA syntax with Excel formula parameters, I also want to inform you that you might not need to input a formula. In the comments above you mentioned you just want the H150 /4000. The use of a formula seems redundant. Have a look at the two lines of codes below and see the difference.
Sub Test()
Dim lastrow As Long
With ThisWorkbook.Sheets("Sheet1") 'Change sheetname according to yours
lastrow = 150
.Range("J" & lastrow).Formula = "=H" & lastrow & "/4000" 'This will input a formula
.Range("J" & lastrow) = .Range("H" & lastrow) / 4000 'This will input the value directly
End With
End Sub
Now if you have a total that can differ, you can use another variable, e.g.: total to store that value and use in your code.
Sub Test()
Dim lastrow As Long, total as long
With ThisWorkbook.Sheets("Sheet1") 'Change sheetname according to yours
lastrow = 150
total = 100
.Range("J" & lastrow).Formula = "=H" & lastrow & "/" & total 'This will input a formula
.Range("J" & lastrow) = .Range("H" & lastrow) / total'This will input the value directly
End With
End Sub

Is there a way to create a search function in Excel that has 3-4 variable as inputs and returns only a single output?

I am creating a project capital estimating tool on excel. In the data that I have I need at least 3 variables to identify a specific piece of equipment that a project might need. For example The name of a fitting, its size, and the fluid that flows through it and this would return the unit cost price (which I already have in my database). Just need it to be in a tidy lookup function. Thank you
If you have your data, both search criteria & database in excel you can use this:
Option Explicit
Sub Test()
Dim strType As String, Datasheet As String, Service As String, Supplier As String, SearchString As String
Dim i As Long, Lastrow As Long
Dim wsCriteria As Worksheet, wsDatabase As Worksheet
Set wsCriteria = ThisWorkbook.Worksheets("Criteria")
Set wsDatabase = ThisWorkbook.Worksheets("Database")
With ThisWorkbook.Worksheets("Criteria")
strType = .Range("B2").Value
Datasheet = .Range("B3").Value
Service = .Range("B4").Value
Supplier = .Range("B5").Value
SearchString = strType & "_" & Datasheet & "_" & Service & "_" & Supplier
End With
Lastrow = wsDatabase.Cells(wsDatabase.Rows.Count, "A").End(xlUp).Row
For i = 2 To Lastrow
If wsDatabase.Range("A" & i).Value & "_" & wsDatabase.Range("B" & i).Value & "_" & wsDatabase.Range("C" & i).Value & "_" & wsDatabase.Range("G" & i).Value = SearchString Then
wsCriteria.Range("A7:J7").Clear
wsCriteria.Range("A7").Value = wsDatabase.Range("A" & i).Value
wsCriteria.Range("B7").Value = wsDatabase.Range("B" & i).Value
wsCriteria.Range("C7").Value = wsDatabase.Range("C" & i).Value
wsCriteria.Range("D7").Value = wsDatabase.Range("D" & i).Value
wsCriteria.Range("E7").Value = wsDatabase.Range("E" & i).Value
wsCriteria.Range("F7").Value = wsDatabase.Range("F" & i).Value
wsCriteria.Range("G7").Value = wsDatabase.Range("G" & i).Value
wsCriteria.Range("H7").Value = wsDatabase.Range("H" & i).Value
wsCriteria.Range("I7").Value = wsDatabase.Range("I" & i).Value
wsCriteria.Range("J7").Value = wsDatabase.Range("J" & i).Value
Exit For
End If
Next i
End Sub
Sheet Named "Criteria" before and after:
Sheet Named "Database":

VBA excel - range.formula issue

There is a particular part of my code which I cannot make work,
I'm trying to do the following command on VBA =RIGHT(LEFT(X1;Z1-2);LEN(LEFT(X1;Z1-2))-FIND(":";X1))
On cell X1, there is a text: RESULTS:NG & MODEL:IJ
My VBA code is:
LR = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LR
cel = "A" & i
cel2 = "Y" & i
cel3 = "Z" & i
cel4 = "X" & i
Range("M" & i).Formula = "=RIGHT(LEFT(" & cel4 & "," & cel3 & "-" & 2 & "),LEN(LEFT(" & cel4 & "," & cel3 & "-" & 2 & "))-FIND(:" & cel4 & "))"
Next i
I'm open for a better approach for this issue as well
Thanks in advance
Try writing all the formulas at once and reduce using quotes within the formula as much as possible.
Range(Cells(1, "M"), cells(lr, "M")).Formula = _
"=RIGHT(LEFT(X1, Z1-2), LEN(LEFT(X1, Z1-2))-FIND(char(58), X1))"
All range and cells reference within a sub procedure are better with a properly defined parent worksheet reference.
dim lr as long
with worksheets("sheet1")
LR = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range(.Cells(1, "M"), .cells(lr, "M")).Formula = _
"=RIGHT(LEFT(X1, Z1-2), LEN(LEFT(X1, Z1-2))-FIND(char(58), X1))"
end with

VBA code to iterate through sheets and copy specific data to specific cells

Link to sample workbook:http://www.wikisend.com/download/245286/MIS%20-%20July%20%20(1).xlsm
The workbook has vba code which deletes all sheeets and then creates new sheet by copying sheets from other workbooks.
I want another vba code to automatically generate Executive Summary sheet with all formating and data.Some formulas arent well defined so they will have to be edited manually later. As other worksheets witll change every month manual referencing wud be tedious
My current code:
Sub WorksheetLoop()
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Select
Sheets(Sheets.Count).Name = "Executive Summary"
Sheets("Executive Summary").Select
'Some formatting code on sheet Executive Summary
Dim ws As Worksheet
' Begin the loop.
currentRow=6 'start entering data from row 6
For Each ws In ThisWorkbook.Worksheets
'Assign cell at row=current row and col B reference to cell G5 at sheet ws
'Assign cell at row=current row and col F reference to cell J15 at sheet ws
'Assign cell at row=current row and col H reference to cell T5 at sheet ws
'increment Current row by 1
'don't know what to do here
Next ws
'select entire region( all filled cells)
'format text, add border etc
End Sub
Please tell me syntax of referencing
Solved the problem
Sub test()
Sheets("Executive Summary").Select
Range("A6:P23").Select
Range("P6").Activate
Selection.ClearContents
'Dim ws As Worksheet
Dim Row As Integer
Row = 6
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Executive Summary" Then
Range("A" & Row).Value = "='" & ws.Name & "'!C3"
Range("B" & Row).Value = "='" & ws.Name & "'!G5"
Range("C" & Row).Value = "='" & ws.Name & "'!G39"
Range("D" & Row).Value = "='" & ws.Name & "'!H39"
Range("E" & Row).Value = "=D" & Row & "-C" & Row
Range("F" & Row).Value = "=IF(A" & Row & "=" & Chr(34) & "POWER" & Chr(34) & ",+E" & Row & "*B" & Row & ",+E" & Row & "*B" & Row & "/100)"
Range("G" & Row).Value = "=IF(A" & Row & "=" & Chr(34) & "POWER" & Chr(34) & ",+(J" & Row & "-D" & Row & ")*B" & Row & ",+(J" & Row & "-D" & Row & ")*B" & Row & "/100)"
'"=(J" & Row & "-D" & Row & ")*B" & Row & "/100"
Range("H" & Row).Value = "=I" & Row & "-F" & Row & "-G" & Row
Range("I" & Row).Value = 0
Range("J" & Row).Value = "='" & ws.Name & "'!I39"
Range("K" & Row).Value = "='" & ws.Name & "'!L5"
Range("L" & Row).Value = "='" & ws.Name & "'!K39"
Range("M" & Row).Value = "=G" & Row
Range("N" & Row).Value = "=O" & Row & "-L" & Row & "-M" & Row
Range("O" & Row).Value = "=I" & Row
Range("P" & Row).Value = "='" & ws.Name & "'!M39"
Row = Row + 1
End If
Next ws
End Sub

Resources