I have made a userform so a person can input a start date and an end date so a line graph will display the desired information. Currently I have everything working except the range update syntax.
I am saving the address of the start date's data as Ad and the address of the end date's address as Add (Both are strings).
I then try to set the range using these but I am doing something wrong. here is the code.
Dim CellX1 As Integer
Dim CellY1 As Integer
Dim CellX2 As Integer
Dim CellY2 As Integer
Dim Ad As String
Dim Add As String
Sheets("Data").Activate
Cells(CellY1, CellX1).Activate
Ad = ActiveCell.Address 'set start address
Cells(CellY2, CellX2).Activate
Add = ActiveCell.Address 'set end address
Sheets("Graph").Activate
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Name = "=""A3"""
This is the lines of code that i cant get to work:
ActiveChart.SeriesCollection(1).Values = "=Data!$Ad:Add"
ActiveChart.SeriesCollection(1).XValues = "=Time!$E:$F"
Should be able to Set those as the Range version. You also will do better to assign the series variables to actual Ranges instead of addresses as strings. Really, you should just Set directly which is what I have below.
Full code should be something like:
Dim CellX1 As Integer
Dim CellY1 As Integer
Dim CellX2 As Integer
Dim CellY2 As Integer
Dim Ad As Range
Dim Add As Range
Set Ad = Sheets("Data").Cells(CellY1, CellX1) 'set start address
Set Add = Sheets("Data").Cells(CellY2, CellX2) 'set end address
Sheets("Graph").Activate
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Name = Range("A3")
ActiveChart.SeriesCollection(1).Values = Range(Ad, Add)
ActiveChart.SeriesCollection(1).XValues = Worksheets("Time").Range("$E:$F")
Note that I changed the type of variable for Ad and Add to Range. This makes it easier to create a start/end Range for the chart.
Related
I have two drop-down menus that list a range of dates. One date identifies the beginning of the range and the other the end. I also created two named ranges as follows:
chart_Dates=INDEX(Dates,MATCH(Home!$A$7,Dates,0)):INDEX(Dates,MATCH(Home!$A$9,Dates,0))
chart_Frequency=INDEX(Frequency,MATCH(Home!$A$7,Dates,0)):INDEX(Frequency,MATCH(Home!$A$9,Dates,0))
Dates, and Frequency from those formulas are named ranges with set boundaries. If I manually enter the y-values as "Home!chart_Frequency" and the horizontal values as Home!chart_Dates the macro works just fine. I can adjust the start and end dates using the drop-down menus and the graph updates accordingly.
However, the macro recorder doesn't translate those action into code. I've attempted the following code without success (there is no error generated but the graph only references the first cell in my range).
ActiveChart.SeriesCollection(1).Values = Range("chart_Frequency")
ActiveChart.SeriesCollection(1).XValues = Range("chart_Dates")
How do I get the code to replicate what I can do manually? Any advice is greatly appreciated!
Try this:
Sub testing()
Dim freqRange As String
Dim datesRange As String
Dim Name As Variant
For Each Name In ThisWorkbook.Names
If Name.Name = "chart_Frequency" Then
freqRange = Name.Value
ElseIf Name.Name = "chart_Dates" Then
datesRange = Name.Value
End If
Next Name
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(1).Name = "=Sheet1!$B$1"
ActiveChart.FullSeriesCollection(1).Values = datesRange
ActiveChart.FullSeriesCollection(1).XValues = freqRange
End Sub
My chart didnt have any series so it adds a new series and it updates the Name, Values and XValues with no issues.
Here is the code I have right now. After it runs when I check the y-values and horizontal values on the graph itself and they are both blank.
'Create chart
Dim rng As Range
Dim cht As ChartObject
'Data range for the chart
Set rng = ActiveSheet.Range("$E$7:$F$501")
'Designates which cell to place the top left corner of the graph in
ActiveSheet.Range("N5").Select
'Create a chart
Set cht = ActiveSheet.ChartObjects.Add( _
Left:=ActiveCell.Left, _
Width:=360, _
Top:=ActiveCell.Top, _
Height:=216)
'Give chart some data
'I was using the line below to set the range but comment blocked it out and entered the suggested code
' cht.Chart.SetSourceData Source:=rng
Dim freqRange As String
Dim datesRange As String
Dim Name As Variant
For Each Name In ThisWorkbook.Names
If Name.Name = "chart_Frequency" Then
freqRange = Name.Value
ElseIf Name.Name = "chart_Dates" Then
datesRange = Name.Value
End If
Next Name
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(1).Name = "=Home!$A$5"
ActiveChart.FullSeriesCollection(1).Values = datesRange
ActiveChart.FullSeriesCollection(1).XValues = freqRange
'Determine the chart type
cht.Chart.ChartType = xlLineMarkersStacked
'Remove Major Gridlines
cht.Chart.Axes(xlValue).MajorGridlines.Delete
'Sets the legend to the bottom of the graph
cht.Chart.SetElement (msoElementLegendBottom)
cht.Name = "Frequency_Chart"
'Add title and reference A5 for name
ActiveSheet.ChartObjects("Frequency_Chart").Activate
ActiveChart.SetElement (msoElementChartTitleAboveChart)
Application.CutCopyMode = False
Selection.Caption = "=Home!R5C1"
I'm programming a Macro in VB for Excel 2013 that search for coincidences in different worksheets, and add a link to the cells that match.
I'm havin torubles to insert the link in the cell, since the link must be different for a range of cells, I need help here.
Here is my code
Dim bufferDetails As String
Dim tmpCell As String
Dim spot As String
Dim cell As Variant
Dim cellSpots As Variant
For Each cell In Worksheets("MMS-Locations").Range("D2:D1833")
If (cell.Value2 = "NULL") Then
cell.Value2 = "NULL"
Else
tmpCell = cell.Text
'A62
If (Left(tmpCell, 3) = "A62") Then
spot = spotName(tmpCell)
For Each cellSpots In Worksheets("DetailedMap").Range("G60:CF123")
If (cellSpots.Value2 = spot) Then
For Each linkToSpot In Worksheets("MMS-Locations").Range("H2:H1833")
Worksheets("MMS-Locations").Hyperlinks.Add _
Anchor:=Range(linkToSpot), _
Address:="http://example.microsoft.com", _
ScreenTip:="Microsoft Web Site", _
TextToDisplay:="Microsoft"
Next linkToSpot
Debug.Print ("Encontrado " + cellSpots)
End If
Next cellSpots
End If
End If
Next cell
End Sub
Function spotName(fullName As String) As String
Dim realSpot As String
Dim lenght As Integer
lenght = Len(fullName) - 3
realSpot = Right(fullName, lenght)
spotName = realSpot
End Function
As I was thinking the linkToSpot variable contains the actual cell in the range, so I can move my selection of the sell, but my code fails in there with this error:
Error in the Range method of the '_Global' object,
Just for reference, here is what I use to convert a phone number to an email for texting..setting it as a hyperlink in the current cell.
ActiveCell.Value = myNumbr
Set myRange = ActiveCell
ActiveSheet.Hyperlinks.Add anchor:=myRange, Address:="mailto:" & myRange.Value, TextToDisplay:=myRange.Value`
Keep your code simple to start with, until you find a working script, then add other items. Make good use of the F8 key to step through your code to find out exactly where an error occurs.
I'm trying to find if there's a given title on a cell, pass the address of that cell to a variable and use such location to adjust the size of the column. The reason I'm doing this is because I'm writing several functions which will shift the position of the columns. I'd appreciate it if someone could take a look and tell me what I'm doing wrong.
Option Explicit
Sub adjustColumns()
Dim PONumberCell As String
Dim PONumberAddress As Range
Dim TopLabelinColumn As Range
For Each TopLabelinColumn In Range("A1:Z1").Cells
If TopLabelinColumn Like "PO_NUMBER" Then TopLabelinColumn.Value = "PO"
PONumberCell = TopLabelinColumn.Address
Set PONumberAddress = PONumberCell
PONumberAddress.ColumnWidth = 70
Next TopLabelinColumn
End Sub
edited after OP's further request:
you are confusing a Range object (such as PONumberCell is meant to be) with a String variable one (like PONumberAddress), so
Set PONumberAddress = PONumberCell
doesn't work because you are trying to assign an object variable to a String one
but you can be more effective avoiding the loop and using the Find() method
Option Explicit
Sub adjustColumns()
Dim PONumberAddress As String
Dim PONumberCell As Range
Set PONumberCell = Range("A1:Z1").Find(what:="PO_NUMBER", LookIn:=xlValues, lookat:=xlPart, MatchCase:=False)
If Not PONumberCell Is Nothing Then
With PONumberCell
.value = "PO"
PONumberAddress = .Address
.EntireColumn.ColumnWidth = 70
End With
Else
Set PONumberCell = Range("A1:Z1").Find(what:="PO", LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False) '<--| if it didn't find "PO_NUMBER" then it seaches for a complete match of "PO"
If Not PONumberCell Is Nothing Then PONumberCell.EntireColumn.ColumnWidth = 70
End If
End Sub
Following the comments above, there are a few erros in your code:
Setting the PONumberAddress Range, you need to use the syntax : Set PONumberAddress = Range(PONumberCell) using the address string found in brackets.
To set the column width, use : PONumberAddress.Columns.ColumnWidth = 70.
According to your post, I think you want to do this only for columns where the header text is "PO_NUMBER", therefore you need all the code below inisde your If : If TopLabelinColumn.Value Like "PO_NUMBER" Then.
Code
Option Explicit
Sub adjustColumns()
Dim PONumberCell As String
Dim PONumberAddress As Range
Dim TopLabelinColumn As Range
For Each TopLabelinColumn In Range("A1:Z1").Cells
If TopLabelinColumn.Value Like "PO_NUMBER" Then
TopLabelinColumn.Value = "PO"
PONumberCell = TopLabelinColumn.Address
Set PONumberAddress = Range(PONumberCell)
PONumberAddress.Columns.ColumnWidth = 70
End If
Next TopLabelinColumn
End Sub
I have a string inside a cell in a workbook that I am using to define the address of a range that a lookup should be done upon.
As an example, let's say the string is called LookupRange and has the value:
''Rate Sheet'!Y111:AA126
My problem is that in my code, when I set the range I have to use:
Set yRange = ThisWorkbook.Worksheets("Rate Sheet").Range(LookupRange)
Is there a way to use the .Range() property without using the .Worksheets() property?
For instance, maybe a way to do something like:
Set yRange = ThisWorkbook.Range(LookupRange)
If not, I guess I might have to write some code to extract the sheetname from the sheet range?
Assuming LookupRange is a String, you can extract the Sheet and Range from the string using Mid() and InStr():
Sub TestIt()
Dim LookupRange As String
LookupRange = "'Rate Sheet'!Y111:AA126"
SheetL = Mid(LookupRange, 2, InStr(2, LookupRange, "'") - 2)
RangeL = Mid(LookupRange, InStr(1, LookupRange, "!") + 1, Len(LookupRange))
Set yRange = ThisWorkbook.Sheets(SheetL).Range(RangeL)
End Sub
I don't know exactly what sort of look up you are doing, but this should work..(parse it as a string)
Sub range_set()
Dim rr As Range
Set rr = Range(CStr(Range("LookupRange")))
Debug.Print Application.WorksheetFunction.VLookup(1, rr, 2, False)
End Sub
Where the named range "LookupRange" is a single cell that contains the sheet address "Sheet2!Y111:AA126" (or whatever) to be used in a "lookup".
Set yRange = Application.Range("'Rate Sheet'!Y111:AA126")
I have a named range lstVendors that refers to: =OFFSET(Data!$W$2,0,0,COUNTA(Data!$W$2:$W$400),1). I want this range to be populated when the workbook opens. I have the following code for this:
Private Sub Workbook_Open()
Application.WindowState = xlMaximized
Dim rslt()
Dim i As Integer
Dim n As Integer
Dim startRng As Range
Dim DropDown1 As DropDown
ThisWorkbook.Sheets("Dashboard").Shapes("TextBox 6").Visible = False
' Range("lstVendors").Offset(0, 0).Value = "Please Select..."
' Set DropDown1 = ThisWorkbook.Sheets("Dashboard").DropDowns("Drop Down 1")
' DropDown1.Value = 1
On Error Resume Next
If Not IsError(Range("lstVendors")) Then
Range("lstVendors").ClearContents
End If
On Error GoTo 0
rslt = Application.Run("SQLite_Query", "path/to/my/sqlite", "SELECT PROGRAM_ID FROM VENDOR;")
Set startRng = Range("lstVendors")
i = 0
For n = 2 To UBound(rslt)
Range("lstVendors").Offset(i, 0).Value = rslt(n)(0)
i = i + 1
Next n
End Sub
It errors on the Set startRng = Range("lstVendors"). I know this is because there's nothing in the range when I'm trying to set it, because if I put one entry into the named range, the set works, however, I need it populated by the sqlite query on each open as the data changes.
Any suggestions much appreciated.
Try this. You have a dynamic range that doesn't evaluate after you clear the contents. To avoid this, there are probably several ways, but easy to simply hardcode the startRange variable so that it always points to Data!$W$2 address, which is (or rather, will become) the first cell in your lstVendors range.
Private Sub Workbook_Open()
Dim rslt()
Dim i As Integer
Dim n As Integer
Dim startRng As Range
Dim DropDown1 As DropDown
Dim rngList As Range
'// Define your startRange -- always will be the first cell in your named range "lstVendors"
' hardcode the address because the dynamic range may not evalaute.
Set startRange = Sheets("Data").Range("W2")
'// Empty th lstVendors range if it exists/filled
On Error Resume Next
Range("lstVendors").Clear
On Error GoTo 0
'// Run your SQL query
rslt = Application.Run("SQLite_Query", "path/to/my/sqlite", "SELECT PROGRAM_ID FROM VENDOR;")
i = 0
'// Print results to the Worksheet, beginning in the startRange cell
For n = 2 To UBound(rslt)
'Increment from the startRange cell
startRange.Offset(i, 0).Value = rslt(n)(0)
i = i + 1
'Verify that "lstVendors" is being populated
Debug.Print Range("lstVendors").Address
Next n
End Sub
Thanks for the suggestions. Here is what I ended up doing in order to get around my problem. It involves adding something I didn't specify would be ok in my original question, so David's answer is great if what I did isn't an option. I first populated the first two cells in my named range with "Please Select..." and "All". In Sub Workbook_Open() we do this:
Private Sub Workbook_Open()
Application.WindowState = xlMaximized
Dim rslt()
Dim i As Integer
Dim n As Integer
Dim startRng As Range
Dim DropDown1 As DropDown
' Disable our not found message
ThisWorkbook.Sheets("Dashboard").Shapes("TextBox 6").Visible = False
' Set our start range to our named range
Set startRng = Range("lstVendors")
' Grab all vendor names
rslt = Application.Run("SQLite_Query", "path/to/my/sqlite", "SELECT PROGRAM_ID FROM VENDOR;")
' Print result. Skip first two rows as constants "Please Select..." and "All" are populated there
i = 2
For n = 2 To UBound(rslt)
startRng.Offset(i, 0).Value = rslt(n)(0)
i = i + 1
Next n
End Sub
Then we will create Sub Workbook_BeforeClose:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
' Disable the save changes dialog. This workbook will be locked up for display only. No need to confuse the user.
Application.DisplayAlerts = False
' Clear everything below the "Please Select..." and "All" cells in the named range
On Error Resume Next
Range("lstVendors").Offset(2, 0).ClearContents
On Error GoTo 0
' Save the changes to the named range
ThisWorkbook.Save
Application.DisplayAlerts = True
End Sub
This information is going to populate a drop down, so having Please Select and All hardcoded into the named range is acceptable for me. If that stipulation doesn't work for someone else looking at this in the future, please use David's suggestion! Thanks again!