I'm trying to call a range between a fixed cell and a dynamic cell (which i can reference using a defined name and an offset). Im not too sure how the syntax should go. Currently I have:
Range("B21:Range("Anchor").Offset(-1,1)")
'(i know this is incorrect syntax, but just to show you the logic)
Where B21 is the fixed cell. And "Anchor" is the Defined name which has a dynamic cell address.
For some background: I'm trying to call all cells within a column of a table, however the table has an "add rows" function, so the last cell of the column is always changing. My "Anchor" is below the position the last row will be.
Im not very good with VBA language, so please bear with me.
You can do it like this:
Dim ws as worksheet, rng as range
set ws = activesheet
With ws
Set rng = .Range(.Range("B21"), .Range("Anchor").Offset(-1,1))
end with
Related
I am trying to create a dynamic named range for a checklist. This dynamic range should reference blank cells that are sandwiched between two named cells (TestA & TestB).
The purpose of this dynamic range is to eventually be referenced when I create a macro that calls the below for loop if a certain drop down selection at the beginning of this checklist changes to "section not needed".
Right now, I am concerned with this for loop not working & my dynamic named range not working.
I keep getting this error: "Method, range of object Global Failed"
Note: If you input my formula as a named range in Excel it will in fact give you the number of blank cells for however many cells you decide to sandwich between ranges TestA & TestB, but for some reason...Excel cannot recognize that range within VBA.
Any ideas on how to accomplish this would be very much appreciated.
Thank you!
I have searched the web and youtube for instances of someone trying to reference a blank range dynamically, but have failed to find anything helpful.
FORMULA:
Formula used to create named range "BetweenTest"
=COUNTBLANK(OFFSET(TestA,1,0,1,1):OFFSET(TestB,-1,0,1,1))
CODE:
Sub FFO ()
Dim Rng As Range
For Each Rng In Range("BetweenTest")
If Rng.Value = "" Then
With Rng
.Value = "N/A"
End With
End If
Next Rng
End Sub
The following excel sub is a filter that is filtering out rows based on the rows in the criteria row.
The code works well when the ranges are set with absolute data. I want to change the code to take the range from references stored as cell values (an indirect reference) but I cannot find a way to adapt other code snippets I see to work and I wonder if anyone can help me. I am not a programmer.
The problem is that as new data is inserted from time to time the range reference will move and the start of the data an the associated filter is in cell (using RC notation 14,14) and the data in cell 13,12. While I know I can’t use the indirect function in vba I wondered if there is a way to dynamically assign a range to be able to use the Advance filter function.
I have the code to find the last column and row of the data block.
I have tried the following code (2 attempts) but it won’t let me use the object in this way
I have tried to crate the cell reference as a string then assign it using the range function. I then read an answer where someone had put the value of the cells directly into the range function and it has worked for them ( they were copying cells). The 2 attempt are broadly the same but in the second I am trying to be more specific.
The issue seems to be as soon as I change from an absolute reference "A50" in the range statement the range no longer works. I am unsure how to resolve this and perhaps it can't be
It may be helpful to know the that data being filtered is rows of name and telephone data along with a tally system to show attendance (one column per week for a year)
The cells with the dynamic data hold them in the form A1 not RC format
Sub UseAdvancedFilterInPlace()
'This version of the sub has absolute references and works perfectly
Dim rdData As Range
Dim rgcriteria As Range
Call TurnOffStuff
Set rgData = Sheet9.Range(“A50”).CurrentRegion
Set rgcriteria = Sheet9.Range(“A46”).CurrentRegion
rgData.AdvancedFilter xlFilterInPlace, rgcriteria
Call TurnOnStuff
End Sub
Sub UseAdvancedFilterInPlace()
'This version of the sub has dynamic references and fails
Dim rdData As Range
Dim rgcriteria As Range
Call TurnOffStuff
Dim Top_of_data As String
Dim Top_of_Criteria As String
Dim My_range As Range
‘Attempt 1
'Set rgData = Range(Sheet9.Cells(13, 12).Value).CurrentRegion
'Set rgcriteria = Range(Sheet9.Cells(14, 14).Value).CurrentRegion
'Attempt 2
Set rgData = Sheet9.Range(Sheet9.Range(Cells(13, 12)).Value).CurrentRegion
Set rgcriteria = Sheet9.Range(Sheet9.Range(Cells(14, 14)).Value).CurrentRegion
rgData.AdvancedFilter xlFilterInPlace, rgcriteria
Call TurnOnStuff
End Sub
The actual error message I get is an application-defined or object-defined error
This worked for me.
Set rdData = Sheet9.Range(Sheet9.Range("L13").Value).CurrentRegion
Set rgcriteria = Sheet9.Range(Sheet9.Range("N15").Value).CurrentRegion
given that Range("L13").Value is A50 and Range("N15").Value is A46.
extra: Use the statement Option Explicit in the first line of every module, out of every sub or function. This option throws an error on undeclared variables, and will help you avoid renameing mistakes on variables.
I am using the following code to add a range:
Combined_Data.Range("A1:AZ200").Find("Marker").Offset(1,0).Select
Range(Selection, Selection.End(xlDown)).Select
ThisWorkbook.Names.Add Name:="Marker",RefersTo:=Selection
When I use the named range "Marker" in an excel formula (typed directly into the spreadsheet), the workbook doesn't seem to respect the values contained in the range. The only way I can get it to work is to use an R1C1 reference to the cells I want in the range.
Does anyone know how I can avoid the specific cell reference?
When I look at the Name Manager, the actual cell references of the range are correct. In this example it is: A6:A1655. The Name Manager has: ='Combined Data'!$A$6:$A$1655
When I use the R1C1 reference, the range appears the same in the Name Manager.
All the ranges should be fully qualified to the same worksheet Combined_Data. You also need to set the Range.Find() After parameter to the first cell in the Range.
Dim marker As Range
With Combined_Data
Set marker = .Range("A1:AZ200").Find("Marker", After:=.Range("A1"))
If Not marker Is Nothing Then
Set marker = marker.Offset(1, 0)
Set marker = .Range(marker, marker.End(xlDown))
marker.Name = "Marker"
End If
End With
These videos will help: Excel VBA Introduction Part 5 - Selecting Cells (Range, Cells, Activecell, End, Offset) and Excel VBA Introduction Part 15a - Find and FindNext
This question stems off another post I had. (see Search through column in excel for specific strings where the string is random in each cell)
Using the above image as reference, I am trying to search through column B (actually over 1000 lines) using column E as the "lookup values." The end goal would be for "just" the names to be displayed in column C. The trick is all the randomly generated characters the encompass the names. Below is what I would want the datasheet to look like. A formula or module should work, but the vlookup and other lookup function I can't get to work.
For a worksheet function approach, you could enter in C3 and fill down this formula:
=LOOKUP(8^5,SEARCH(E$3:E$7,B3),E$3:E$7)
The constant 8^5=32768 is chosen to be larger than the maximum possible string length so that LOOKUP returns the last matching value. The formula returns #N/A if no string is found.
Another possibility, which may be easier to understand then assylias post initially, but also may be a bit more time consumptive (although with 1,000 rows, I don't think it will matter much) is below.
This requires that you name the range in column E as myNames (or whatever name you wish, just update the code - alternatively, you cuold just write Range("E1:E6")). Also, if you move the random values from column B, update that in the code as well.
Sub findString()
Dim celString As Range, rngString As Range, celSearch As Range, rngSearch As Range
Dim wks As Worksheet
Set wks = Sheets("Sheet1") 'change sheet reference to whatever your sheet name is
Set rngString = wks.Range("myNames")
Set rngSearch = Intersect(wks.UsedRange, wks.Range("B1").EntireColumn)
For Each celString In rngString
For Each celSearch In rngSearch
If InStr(1, celSearch.Text, celString.Value) > 0 Then
celSearch.Offset(, 1) = celString.Value
End If
Next
Next
End Sub
Since, I worked on your original question as well, I would suggest getting the counts through Siddharth's answer and then running this, or assylias's code above to get the names next to the columns. You could put a button the sheet, or just use the Macro dialog box to run the macro.
I must be having a brain fog at this point because I am certain this is easy to do, and in fact I have managed to create other functions that are a bit more complicated for this project.
Anyway, what I am trying to do. I have a sheet (inventory-data) and in column 1, it lists a company name, which is a same for all the rows. i.e. each of the 1900 or so rows have companyname in the first cell.
Now, while the data will always be the same at each application, the number of rows will change.
So, I need a function that will first determine what the last row of data is in the range, and then change all of the cells in column one of each record to name_company. The company names will always be the same so I can staticly assign them. Here is what I have that does not work.
I was able to get it to work another way, but it would replace text all the way down to the very last row of the worksheet, way beyond where the data stops.
Thanks!
Sub changeCompany() 'Changes company name as pulled from Agemni into proper ETA format
Dim myCell As Range
Dim RngToChange As Range 'The range of cells that need to be changed
Dim LastRow As Long 'Declare variable to help determine the last row on a variable length worksheet
Dim i As Integer
With Worksheets("inventory-data") 'set the range to change
Set RngToChange = .Columns(1)
End With
LastRow = Worksheets("inventory-data").UsedRange.Rows.Count 'Have Excel determine what the last row is.
For i = LastRow To 1 Step -1
RngToChange.Cells.Value = "name_company"
Next i
End Sub
I've always had more success with [SomeCellOrRange].CurrentRegion.Rows.Count e.g:
Range("A1").CurrentRegion.Rows.Count
UsedRange looks for any use of cells, not limited to a continuous tabular block. It also sometimes needs you to re-save the workbook before it will properly shrink after you have eliminated some rows.