Excel VBA Named Range for Form Keeps Changing - excel

I created a userform in VBA where you can add and delete records and I am using a named range to refresh the list box showing the data in the source. The issue I am running into is the named range keeps decrementing each time I delete a record. The main issue I am running into is if the record in the first row of data ($A$2) is deleted it changes the
What I want it to stay as so it does not error out when a record it deleted from A2:
=OFFSET('Carrier List'!$A$2,0,0,COUNTA('Carrier List'!$A$1:$A$1000),11)
What it changes to when a record is deleted from A2:
=OFFSET('Carrier List'!#REF!,0,0,COUNTA('Carrier List'!$A$1:$A$999),11)
I am calling this range in several functions, namely when the form is opened, when a record is added, when a record is edited, and when a record is deleted. It only causes issues when a record is deleted but it pretty much leaves the app dead in the water when that happenes.
VBA I am using
when form is opened:
Private Sub UserForm_Initialize()
Me.lstbxCurrCarrierLst.RowSource = "CarrierList"
End Sub
For the add,edit,delete functions:
Me.lstbxCurrCarrierLst.RowSource = "CarrierList"
So, is there a way I can lock the field range so it does not change OR is there another way I can set the RowSource so it stays constant as there are changes in the dataset?

Consider using Index() instead of Offset() in the range name definition. With Index() you can point to row 2 and you don't need to worry about deleting row 2.
The first Index addresses row 2 in column A, the second Index uses the row identified by the CountA function. The colon operator connects the two cells into a range.
=Index('Carrier List'!$A:$A,2):Index('Carrier List'!$A:$A,COUNTA('Carrier List'!$A$1:$A$1000))
Edit: I missed that the original named range is 11 columns wide, i.e. starts in column A and extends to column K. To do this in the named range formula, simply change the second Index() to work in column K, but keep the CountA for column A.
=Index('Carrier List'!$A:$A,2):Index('Carrier List'!$K:$K,COUNTA('Carrier List'!$A$1:$A$1000))

Related

Using While loop for getting the handle of rows in a particular Column

I'm still learning VBA. I'm having a requirement in which I need to work on rows which are not empty in a particular column. I tried For loop. But considering 10 thousand entries in the Excel, there were performance issues. Can you suggest a while loop which only jumps to the rows which has value and not through all the rows?
Basically I need the handle of all the rows which are not empty.
SpecialCells is your friend here.
If you want all Cells that have Constant Values in them from Column A, then you can use Columns(1).SpecialCells(xlCellTypeConstants)
If you want all Cells that have a Formula in them from Column A, then you can use Columns(1).SpecialCells(xlCellTypeFormulas)
If you want all Cells that are Blank from Column A, then you can use Columns(1).SpecialCells(xlCellTypeBlanks)
Together, these 3 will give you all cells in the column. This means that "Non Blank" will be xlCellTypeConstants combined with xlCellTypeFormulas. Another name for "combination" is Union, which lets us stick Ranges together
Dim rngNonBlank AS Range
Set rngNonBlank = Union(Columns(1).SpecialCells(xlCellTypeConstants), _
Columns(1).SpecialCells(xlCellTypeFormulas))
Dim rngLoopThrough AS Range
For Each rngLoopThrough In rngNonBlank
'You can use rngLoopThrough.EntireRow to get the entire Row
Next rngLoopThrough

Excel - Adding a row outside of forumla range

Currently I have a couple of SUM functions setup in my worksheet.
Lets use H2:H34 for my example: If I add a row within that range, the sum function will automatically adjust. The problem is when I add new rows, it is below the range (below H:33). In this case, I would just have to highlight the column and expand/drag the range to where it needs to be. There are a total of 8 columns that are doing a sum function. Is this only possible by using a macro?
=SUBTOTAL(3,H2:H34)
With your formula being the extent of the range minus one row we can use this:
=SUBTOTAL(3,H2:INDEX(H:H,ROW()-1))
Now as rows are added or deleted the reference will remain intact.
If there is a chance that row 2 would be deleted you will get #Ref errors. To avoid that you can use this formula:
=SUBTOTAL(3,INDEX(H:H,2):INDEX(H:H,ROW()-1))
Now there are no specific cell references and adding or deleting will not effect the formula and the range will be dynamic.
you could use a named range like =sum(range1) and something like;
Select first empty cell in column F starting from row 1. (without using offset )
to find the first empty cell to change the reference for the named range.
Triggering it would be difficult... potentially could use on_save?
or option 2.... bit of a dodge, but can work....
you say your sum is in cell H35... maybe move it to h36, and create a dummy hidden row in 35 (0.1 row height). that way when you add a row, it is always above 35. Use sum H1:H35 and it will always auto update? its not a neat solution, but should work
Use the following code, you need to adjust column "H" to whatever column you need.
Dim sumtest As Variant
sumstest = Application.WorksheetFunction.Subtotal(9, Range("H2:H" & ActiveSheet.Cells(ActiveSheet.Rows.Count, "H").End(xlUp).Row))

Issue using Dynamic Named Ranges for Cascading Data Validation

I'm using two cells with Data Validation - the first cell (E9) simply creates a drop down menu based on the range A2:A6, and the second cell (E10) validation uses the source INDIRECT(E9), which will always refer to one of five different named ranges.
When I have the named ranges fixed, (i.e A2:A250), the second drop down works, but I really need the ranges to be dynamic, so far I've been creating named ranges with the following "source" formula:
=OFFSET(LookupLists!$B$2,0,0,COUNTA(LookupLists!$B:$B),1)
With the other ranges being the exact same only in columns C-F.
When I write out this formula it highlights the correct area on the screen, but the drop down button in cell E10 is completely unresponsive, when the drop down list should show the exact area that's being highlighted.
As a note, the lists themselves are created using an array formula and some VBA code to create a sorted unique list based on another part of the spreadsheet, so I've been unable to use tables to create the ranges as some other websites have suggested.
INDIRECT doesn't work with dynamic ranges. Credit to these guys for the solution:
http://chandoo.org/forum/threads/passing-a-named-range-to-a-formula-with-indirect.5854/#post-32423
First, insert a module into you sheet and paste in the UDF:
Option Explicit
Function RetrieveRangeForName(psRange As String) As String
RetrieveRangeForName = Range(psRange).Address
End Function
Then you will need a helper cell, since I don't think UDFs work in the Data Validation dialog. In E11, enter =RetrieveRangeForName(E9).
Then in the Data Validation, set to List, you can enter: =INDIRECT(E11)
The reason it doesn't work as discussed here is that INDIRECT expects a string that it can evaluate to give a reference. However your named range is already a reference. Logically, the only way to use it in INDIRECT is to convert it into a string first which you can do with a UDF:-
Function GetAddress(Name As String) As String
Dim rng As Range, addr As String
Set rng = Worksheets("Sheet1").Range(Name)
addr = rng.Address
GetAddress = addr
End Function
Then use this to define a range called NewRange:-
=INDIRECT(GetAddress(Sheet1!$E$9))
Finally this can be used in the validation for E10 (Named Range ListB is defined as in the question, ListA etc. correspondingly for columns A to E).

VBA - Macro That Finds Specified Header, Filters Values in Various Sheets where Column Locations Change

Trying to figure out a Macro that will do the following:
1) Find a specified header (ie: "status")
2) Filter this column to a specified value (ie: "discontinued")
3) Once filtered, find another specified column (ie: "replaced with")
The challenge for me is I can't figure out how to set it so the column "Field" is variable to the sheet its on.
I have multiple files that have the "status" header in different locations, (one file has it in column c, another in column f, etc). Is there a way to have excel find that specific column in each sheet is and filter it?
Find a specified header
You would implement this quite exactly as you'd subconsciously do in your head while "finding a specific header": iterate all columns from left to right until you find it, ...and throw a fit when it's not there. The function should be generic enough to not be attached to any specific worksheet, so you should pass it a worksheet as a parameter.
Something like this:
'returns column number of specified header, or -1 if not found.
Public Function FindHeader(sheet As Worksheet, header As String) As Integer
...
End Function
Find your header row, and loop through the non-empty cells. Your exit conditions would be either you've iterated all non-empty cells in the header row, or you've found a cell that matches the content you're looking for. When the loop exits, you need to know two things: whether the header was found, and if it was, under which column.
Filter this column for a specified value
You don't say how the value gets specified, so I'll assume all you need is a Sub that takes an Integer for the header column to filter, and a String parameter for the value to filter with. Using AutoFilter -related methods should get you there. Again you want to be able to use this code for any sheet, so pass one as a parameter:
Public Sub FilterForSpecificValue(sheet As Worksheet, columnIndex As Integer, filter As String)
...
End Sub
Find another specified column
Well, just reuse the function from #1, passing it the same sheet with another column header:
result = FindHeader(ActiveSheet, "AnotherHeader")
That's about as much as I can give you for the question you've asked; feel free to ask SO for any specific programming issues you might encounter implementing this. However it's possible you won't even need to ask, because it's very likely that somebody else somewhere has documented exactly how to do it - here are a couple links that can help you:
Selecting an entire row of data (StackOverflow)
Looping Through a Range of cells (MSDN)
StringContainsAny (StackOverflow)
AutoFilter Method (MSDN)

Excel charts - setting series end dynamically

I've got a spreadsheet with plenty of graphs in it and one sheet with loads of data feeding those graphs.
I've plotted the data on each graph using
=Sheet1!$C5:$C$3000
This basically just plots the values in C5 to C3000 on a graph.
Regularly though I just want to look at a subset of the data i.e. I might just want to look at the first 1000 rows for example. Currently to do this I have to modify the formula in each of my graphs which takes time.
Would you know a way to simplify this? Ideally if I could just have a cell on single sheet that it reads in the row number from and plots all the graphs from C5 to C 'row number' would be best.
Any help would be much appreciated.
OK, I had to do a little more research, here's how to make it work,
completely within the spreadsheet (without VBA):
Using A1 as the end of your desired range,
and the chart being on the same sheet as the data:
Name the first cell of the data (C5) as a named range, say TESTRANGE.
Created a named range MYDATA as the following formula:
=OFFSET(TESTRANGE, 0, 0, Sheet1!$A$1, 1)
Now, go to the SERIES tab of the chart SOURCE DATA dialog,
and change your VALUES statement to:
=Sheet1!MYDATA
Now everytime you change the A1 cell value, it'll change the chart.
Thanks to Robert Mearns for catching the flaws in my previous answer.
This can be achieved in two steps:
Create a dynamic named range
Add some VBA code to update the charts data source to the named range
Create a dynamic named Range
Enter the number of rows in your data range into a cell on your data sheet.
Create a named range on your data sheet (Insert - Name - Define) called MyRange that has a formula similar this:
=OFFSET(Sheet1!$A$1,0,0,Sheet1!$D$1,3)
Update the formula to match your layout
Sheet1!$A$1 set this to the top left hand side of your data range
Sheet1!$D$1 set this to the cell containing the number of rows
3 set this value to the number of columns
Test that the named range is working:
Select the dropdown menus Edit - Go To, type MyRange into the reference field.
Your data area for the chart should be selected.
Add some VBA code
Open the VBA IDE (Alt-F11)
Select Sheet1 in the VBAProject window and insert this code
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$D$1" Then Exit Sub
'Change $D$1 to the cell where you have entered the number of rows
'When the sheet changes, code checks to see if the cell $D$1 has changed
ThisWorkbook.Sheets("Sheet1").ChartObjects(1).Chart.SetSourceData _
Source:=ThisWorkbook.Sheets("Sheet1").Range("MyRange")
' ThisWorkbook.Sheets("Chart1").SetSourceData _
Source:=ThisWorkbook.Sheets("Sheet1").Range("MyRange")
'The first line of code assumes that chart is embedded into Sheet1
'The second line assumes that the chart is in its own chart sheet
'Uncomment and change as required
'Add more code here to update all the other charts
End Sub
Things to watch for
Do not directly use the named range as the data source for the chart. If you enter the named range "MyRange" as the Source Data - Data Range for the chart, Excel will automatically convert the named range into an actual range. Any future changes to your named range will therefore not update your chart.
Performance might be impacted by the approaches listed above.
The OFFSET function in the named range is "volatile" which means that it recalculates whenever any cell in the workbook calculates. If performance is an issue, replace it with the INDEX formula.
=Sheet1!$A$1:INDEX(Sheet1!$1:$65536,Sheet1!$D$1,2)
The code fires everytime data is changed on Sheet1. If performance is an issue, change the code to run only when requested (i.e. via a button or menu).
You could look at dynamic ranges. If you use the OFFSET function, you can specify a starting cell and the number of rows and columns to select. This site has some useful information about assigning a name to an OFFSET range.
You can set the range for a chart dynamically in Excel. You can use something like the following VBA code to do it:
Private Sub Worksheet_Change(ByVal Target as Range)
Select Case Target
Case Cells(14, 2)
Sheet1.ChartObjects(1).Chart.SetSourceData Range("$C5:$C$" & Cells(14,2))
...
End Select
End Sub
In this case, the cell containing the number of the last row to include is B14 (remember row first when referring to the Cells object). You could also use a variable instead of the Cells reference if you wanted to do this entirely in code. (This works in both 2007 and 2003.) You can assign this procedure to a button and click it to refresh your chart once you update the cell containing the last row.
However, this may not be precisely what you want to do ... I am not aware of a way to use a formula directly within a chart to specify source data.
Edit: And as PConroy points out in a comment, you could put this code in the Change event for that worksheet, so that neither a button nor a key combination is necessary to run the code. You can also add code so that it updates each chart only when the matching cell is edited.
I've updated the example above to reflect this.
+1s for the name solution.
Note that names don't really really reference ranges, they reference formulae. That's why you can set a name to something like "=OFFSET(...)" or "=COUNT(...)". You can create named constants, just make the name reference something like "=42".
Named formulae and array formulae are the two worksheet techniques that I find myself applying to not-quite-power-user worksheets over and over again.
An easy way to do this is to just hide the rows/columns you don't want included - when you go to the graph it automatically excludes the hidden rows/columns
Enhancing the answer of #Robert Mearns, here's how to use dynamic cells ranges for graphs using only the Excel's formulas (no VBA required):
Create a dynamic named Range
Say you have 3 columns like:
A5 | Time | Data1 | Data2 |
A6 | 00:00 | 123123 | 234234 |
...
A3000 | 16:54 | 678678 | 987987 |
Now, the range of your data may change according to the data you may have, like you have 20 rows of data, 3000 rows of data or even 25000 rows of data. You want to have a graph that will be updated automatically without the need to re-set the range of your data every time you update the data itself.
Here's how to do it simply:
Define another cell that it's value will have the number of the occupied cells with data, and put the formula =COUNTIF(A:A,"<>"&"") in it. For example, this will be in cell D1.
Go to "Formulas" tab -> "Define Name" to define a name range.
In the "New Name" window:
i. Give your data range a name, like DataRange for example.
ii. In the "Refers to" set the formula to: =OFFSET(Sheet1!$A$1, 0, 0,Sheet1!$D$1,3),
where:
Sheet1!$A$1 => Reference: is the Reference from which you want to base the offset.
0 => Rows: is the number of rows, up or down, that you want the upper-left cell of the results to refer to.
0 => Columns: is the number of columns, to the left or right, that you want the upper-left cell of the results to refer to.
Sheet1!$D$1 => Height: is the height, in number of rows, that you want the result to be.
3 => Width: is the width, in number of columns, that you want the result to be.
Add a Graph, and in the "Select Data Source" window, in the Chart data range, insert the formula as you created. For the example: =Sheet1!DataRange
The Cons: If you directly use the named range as the data source for the chart, Excel will automatically convert the named range into an actual range. Any future changes to your named range will therefore not update your chart.
For that you need to edit the chart and re-set the range to =Sheet1!DataRange every time. This may not be so usable, but it's better than editing the range manually...

Resources