Find the Last Row, Column, or Cell on a Sheet then

Unicode

Board Regular
Joined
Apr 9, 2019
Messages
58
Hello Everyone, I am not very in detail with macro VBA coding, I have compiled the following code. My project is set on a flat file, vertical format across range (A:D) there is only 4 columns with data. Titled "Bank, Year, Month, Total". Due to the months of the year, below "Year" column I see 2018, after, 9 rows down I have year 2019. My code selects the range of the 4 columns and creates a Chart on the next TAB called "Chart". Would you know how to make script code function if I enter the next month on the "DataSource" file going down on the "YEAR" column within "Total" columns, so, that when I run my script it will automatically list the next month and only keep the last 12 months behind and keeping the next added month moving forward on the second TAB named "Chart"?



[Code:]


Sub addcharttest()
Dim cht As ChartObject
Dim wks As Worksheet
Set wks = ActiveSheet




' Author: Unicode
' Date: 6/26/2019


'## Select Data from sheet "DataSource TAB". Select Range (A:D), Copy then, Paste results on Sheet(CHART), "Start paste one (8) Row Down" into sheet "Chart".


Dim LastRow As Long 'no dummy data with these Dimensions
Dim Results As Worksheet
Dim DataSource As Worksheet




Application.ScreenUpdating = False 'Makes the code run faster and reduces screen _
flicker a bit.




Set Results = Sheets("Chart")
LastRow = Results.Cells(Results.Rows.Count, "Z").End(xlUp).row

Range("A9:D23").Copy
Results.Range("A" & LastRow + 8).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Application.CutCopyMode = False





'## This is the Sheet script on Sheet(DataSource TAB) that requires the following script line for currency format (.NumberFormat = "$#,##0.00"), changes into your worksheet named "Chart" with RANGE(A1:O2).


Application.CutCopyMode = False


With Sheets("Chart").Range("A10:A23", "C10:C23")
.NumberFormat = "General"
.Value = .Value
End With

With Sheets("Chart").Range("D10:D23")
.NumberFormat = "$#,##0.00"
End With



'add an embedded chart and set it equal to an object variable


Set cht = wks.ChartObjects.Add(420, 75, 400, 250)


'specify the data and the chart type


With cht.Chart
.SetSourceData wks.Range("A9:A9", "A10:D23")
.ApplyCustomType xlColumnClustered
Worksheets("DataSource").ChartObjects.Delete '<-- Code deletes all charts within a worksheet


End With
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
How about I do this with the following
Code:
:

Sub setname()
ActiveWorkbook.Names.Add Name:="Last12", RefersTo:= _
"=OFFSET(ThisWorks!$a$1,COUNTA(ThisWorks!$a:$a)-12,0,12,1)"
End Sub






[QUOTE="sijpie, post: 5304450, member: 105311"]You don't need to do any VBA. See here:

[URL]https://peltiertech.com/Excel/Charts/DynamicLast12.html[/URL][/QUOTE]
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,488
Members
448,967
Latest member
visheshkotha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top