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
Related
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
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.
I'm trying to do a VBA code to accomplish 2 things as follows:
Count how many characters there is on cell A1, using the formula LEN(A1) and one the last line, I'm trying to have the formula RIGHT(LEFT(A1;Q1-2);6) on cell J1
Please follow down my VBA code so far:
LR = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LR
cel = "A" & i
cel2 = "P" & i
cel3 = "Q" & i
Range("P" & i).Formula = "=LEN(" & cel & ")"
Range("J" & i).Formula = "=RIGHT(LEFT(" & cel & "," & cel3 & "-" & 2 & ")," & 6 & ")"
Next i
It seems something silly what is missing, however, I couldnt manage to solve it so far
Thanks in advance
You’re missing a Right, and some other things
Range("J" & i).Formula = "=RIGHT(LEFT(" & cel & "," & cel3 & "-2), 6)"
Im confused with the range.formula on excel VBA,
Im trying to use the formula RIGHTas follows on my VBA code:
LR = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LR
cel = "A" & i
cel2 = "P" & i
cel3 = "Q" & i
Range("R" & i).Formula = "=RIGHT("cel", "cel2" & "" - "" & "cel3")"
The last row of my code, I trying to do right(A1, P1-Q1)
However, Im getting syntax error and Im struggling to solve
Thanks in advance
You were very, very close:
Sub marine()
LR = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LR
cel = "A" & i
cel2 = "P" & i
cel3 = "Q" & i
Range("R" & i).Formula = "=RIGHT(" & cel & "," & cel2 & " - " & cel3 & ")"
Next i
End Sub
I'm trying to use the Sumif function in VBA, however, I'm getting a run-time error. The error started when I tried to add the variable CritLastCol into the formula. If I replace that with an actual cell reference it works. I want to be able to autofill the rows below this and my criteria will change based on the row. In other words my sum range and criteria range are fixed and my criteria is the cell to the left of my activecell (but not one over, it could be 20, 30 columns over).
Sub SumBydimcode()
Dim lastCol As Integer
Dim CritLastCol As String
lastCol = Cells(3 & Columns.Count).End(xlToLeft).Column
lastRow = Range("A" & Rows.Count).End(xlUp).Row
NewLastRow = Range("I" & Rows.Count).End(xlUp).Row
CritLastCol = ActiveCell.End(xlToLeft).Value
ActiveCell.FormulaR1C1 = "=SUMIF(R3C8:R" & lastRow & "C8," & CritLastCol & ",R3C10:R" & lastRow & "C" & lastCol & ")"
ActiveCell.AutoFill Destination:=Range(ActiveCell.Address & ":J" & NewLastRow)
End Sub
You need to surround your CritLastCol variable in quotation marks. To do so in VBA, you use the double-quote twice "". As a result, your code would look like this:
ActiveCell.FormulaR1C1 = "=SUMIF(R3C8:R" & lastRow & "C8,""" & CritLastCol & """,R3C10:R" & lastRow & "C" & lastCol & ")"
Notice the extra double-quotes surrounding CritLastCol
Ok, I figured out a way to do it... Instead of autofill, I used a loop starting at the end of my data and working back up.
Sub SumBydimcode()
Dim lastCol As Integer
Dim CritLastCol As String
lastCol = Cells(3 & Columns.Count).End(xlToLeft).Column
lastRow = Range("A" & Rows.Count).End(xlUp).Row
NewLastRow = Range("I" & Rows.Count).End(xlUp).Row
NewFirstRow = lastRow + 7
S = ActiveCell.Row
If S > NewLastRow - NewFirstRow Then
For S = ActiveCell.Row To NewFirstRow Step -1
CritLastCol = ActiveCell.End(xlToLeft).Value
ActiveCell.FormulaR1C1 = "=SUMIF(R3C8:R" & lastRow & "C8,""" & CritLastCol & """,R3C10:R" & lastRow & "C" & lastCol & ")"
ActiveCell.Offset(-1, 0).Select
Next S
End If
End Sub