I'm trying to create a dynamic graph in VBA in Excel. Unfortunately, when I generate the graph the values that are meant for the y axis are ending up as the series name. Here's the graph. I'm an extremely new VBA user, so I'm sure I am just overlooking a very basic error. Here is my code.
Sheets("Sheet1").Activate
Dim lRow As Long
Dim lCol As Long
'Find the last non-blank cell in column G and H
Range("G3:H500").Select
Do Until ActiveCell.Value = ""
ActiveCell.Offset(1, 0).Select
Loop
lastrow = ActiveCell.Row - 1
'Find the last non-blank cell in row 1
lCol = Cells(1, Columns.Count).End(xlToLeft).Column
MsgBox "Last Row: " & lastrow & vbNewLine & _
"" & Columns(lCol).Address(False, False)
Set rng = Range("G3:H300" & lastrow)
Set xrang = Range("F3:F300" & lastrow)
rng.Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ApplyChartTemplate ( _
"c:\users\name\appdata\Roaming\Microsoft\Templates\Charts\brand_line_chart.crtx" _
)
ActiveChart.SetSourceData Source:=Range("G3:H300" & lastrow)
ActiveChart.Axes(xlCategory).Select
ActiveChart.SeriesCollection(1).XValues = Range("F3:F300" & lastrow)
Any help would be greatly appreciated, thank you!
Delete this line ActiveChart.SeriesCollection(1).XValues = Range("F3:F300" & lastrow). Also since you are getting the lastrow data , change the ("G3:H300" & lastrow) to ("G3:H" & lastrow). Do this everywhere you have set range
Related
I'm entering a value in the last blank cell (as I can't do the last row in a column) due to other data being there. I was to add the sum of all the above cells to each column.
The number of columns is variable as is the number of names
I've been able to add the relevant formula but I can't get it to copy across in the same way my other code did.
This is the line with the error, to copy to the last used column, everything else works except this bit.
Range("O" & nextfree).AutoFill Range("O" & nextfree, Cells("O" & nextfree, lastcolumn))
I get a run type error 13, Type mis-match.
The full code is here
Sub addrow()
'Checks the number of users then adds them to the active sheet section
Dim rowsToAdd As Integer
Dim lastcolumn As Long
Dim lastRow As Long
Dim ws As Worksheet, ws1 As Worksheet
Set ws = ThisWorkbook.Worksheets("Refs")
Set w1 = ThisWorkbook.Worksheets("Active events")
With ws
lastRow = Sheets("Refs").Cells(.Rows.Count, "A").End(xlUp).Row
lastcolumn = Sheets("Active events").Cells.Find("*", searchorder:=xlByColumns, SearchDirection:=xlPrevious).Column
MsgBox lastRow - 1
MsgBox lastcolumn
End With
With ws1
Rows("5:5").Resize(lastRow - 1).Insert Shift:=xlDown ' minus 2 to account for header row and also existing text in row 4
End With
Worksheets("Refs").Range("A2:A" & lastRow).Copy Worksheets("Active events").Range("M4")
Range("O4:O" & lastRow + 2).Formula = "=SUMIF($C$14:$C$5032,$M4,O$14:O$5032)"
Range("O4:O" & lastRow + 2).AutoFill Range("O4", Cells(lastRow + 2, lastcolumn))
'Find the next blank cell in the names range and adds totals and the sum value to all columns
nextfree = Range("M4:M" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("M" & nextfree).Value = "Total"
Range("O" & nextfree).Value = "=SUM(O4:O" & nextfree - 1 & ")"
'Problem code here
Range("O" & nextfree).AutoFill Range("O" & nextfree, Cells("O" & nextfree, lastcolumn))
End Sub
I am attempting to concatenate a column's worth of fields (~900 at the moment) from two other fields in the same sheet.
I am trying to create a macro to enter the formula into Column C. I can't keep the quotation marks straight.
Sub Concatenate()
Dim i As Long
Dim LastRow As Long
Dim Con As String
Dim WS As Worksheet
Set WS = Sheets("Vlookups")
'Set upper range of Loop
LastRow = Range("C" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
'Set to Active Worksheet
Worksheets("Vlookups").Activate
'Explicitly reference the Sheet when calling for any Range or Cell
With WS
For i = 2 To LastRow
Con = "=CONCATENATE(" & .Cells(i, 15).Select & "," & "-" & "," & .Cells(i, 16).Select & ")"
.Cells(i, 3).Select
ActiveCell.Formula = Con
Next i
End With
Application.ScreenUpdating = False
End Sub
There is no need to Select or Activate.
There is no need to loop.
It looks like you are finding the last row based on column C, but then writing the formula into column C, which seems suspect. Perhaps find the last row based on column O, or column P?
There's no need to use the CONCATENATE formula.
With WS
' find last row based on column O, or maybe P
LastRow = .Range("O" & .Rows.Count).End(xlUp).Row
.Range("C2:C" & LastRow).Formula = "=O2&""-""&P2"
End With
If you actually want hard-coded strings instead of cell references in your formula, then:
With WS
' find last row based on column O, or maybe P
LastRow = .Range("O" & .Rows.Count).End(xlUp).Row
For i = 2 to LastRow
Range("C" & i).Formula = "=""" & Range("O" & i).Value & "-" & Range("P" & i).Value & """"
Next
End With
I wrote this code and it keeps giving me an error that the size of the copy area and the paste area are not the same.
but if I just use the copy-paste method, it works perfectly. could you pls help me out.
Sub copy()
eRow = Sheet5.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Sheet4.Range("a4", "d23").copy
Sheet5.Cells(eRow, 1).PasteSpecial (xlPasteValues)
End Sub
Move values one by one with a value transfer. As implied in the name, a value transfer does not carry over formats.
This just copies the 2 individual cells A4 & D23
Sub copy_me()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim lr As Long
lr = ws.Range("A" & ws.Rows.Count).End(xlUp).Offset(1).Row
ws.Range("A" & lr).Value = ws.Range("A4").Value
ws.Range("D" & lr).Value = ws.Range("D23").Value
End Sub
If you meant to grab the entire range A4:D23 then
ws.Range("A4:D23").Copy
ws.Range("A" & lr).PasteSpecial xlPasteValues
OR
ws.Range("A" & lr).Resize(20, 4).Value = ws.Range("A4:D23").Value
I have 2 sheets, in one sheet I am looking for a specifik text in a column, if that exists then it should copy all the rows with the specific text and paste them in another sheet. That is working for me, but the problem is that when I want to pastespecial, only paste the values and not the formulas I isn't working.
Here is the code, any idea what to do?
With Sheets(1)
LR = .Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If .Range("A" & i).Value = "Orange" Then .Rows(i).Copy
Sheets(2).Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial _
Paste:=xlPasteValues
Next i
End With
You have a logic error in the code, which I missed in my comment. You need the PasteSpecial inside the If block:
With Sheets(1)
LR = .Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If .Range("A" & i).Value = "Orange" Then
.Rows(i).Copy
Sheets(2).Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial _
Paste:=xlPasteValues
End If
Next i
End With
While the answer of Rory should fit perfectly your problem, you could speed it up by a big amount using a variable for the ranges to copy...
Dim rng As Range
With Sheets(1)
LR = .Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If .Range("A" & i).Value = "Orange" Then
If rng Is Nothing Then
Set rng = .Rows(i)
Else
Set rng = Union(rng, .Rows(i))
End If
End If
Next
rng.EntireRow.Copy
Sheets(2).Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
End With
Written by phone. May contain errors.
I have a workbook containing multiple sheets of varying sizes. I want to add a total column after the last row and copy the formula across all columns. I have defined the last row and column and the formula appears as expected in the correct place but I receive an error when trying to fill across. How do I correctly reference both dynamic cells for the fill? I'm just using a single sheet for now for testing but will eventually be looping through all the sheets in the book.
Sub Addtotals()
Dim Bord As Worksheet
Dim LRow As Long
Dim LCol As Long
Dim frmcell As Range
Set Bord = Sheets("Borders")
With Bord
'--> Define last rows and columns
LRow = .Range("A" & Rows.Count).End(xlUp).Row
LCol = .Range("A" & Columns.Count).End(xlToLeft).Column
'--> Add Total text to first column
.Range("A" & LRow).Offset(1, 0).Select
ActiveCell = "Total"
'--> Add formula to next column
Set frmcell = Range("B" & LRow + 1)
frmcell.Formula = "=sum(B2:B" & LRow & ")"
'--> Fill formula across range
frmcell.Select
Selection.AutoFill Destination:=Range(frmcell & LCol), Type:=xlFillDefault
End With
End Sub
Thanks :)
Like this?
Option Explicit
Sub Addtotals()
Dim Bord As Worksheet
Dim LRow As Long, LCol As Long
Set Bord = Sheets("Borders")
With Bord
'--> Define last rows and columns
LRow = .Range("A" & Rows.Count).End(xlUp).Row + 1
LCol = .Cells(1, Columns.Count).End(xlToLeft).Column
'--> Add Total text to first column
.Range("A" & LRow).Value = "Total"
'--> Fill formula across range
.Range("B" & LRow & ":" & _
Split(Cells(, LCol).Address, "$")(1) & LRow).Formula = _
"=Sum(B2:B" & LRow - 1 & ")"
End With
End Sub