I am having a lot of trouble with this one. I want to replicate this value in VBA:
=MAX(MAX(Named_Range1),MAX(Named_Range2),MAX(Named_Range3))
I am having an impossible time trying to get this done.
If the three ranges are in the same sheet then:
Sub MaxMax()
Dim r As Range
Set r = Union(Range(Named_Range1), Range(Named_Range2), Range(Named_Range3))
MsgBox Application.WorksheetFunction.Max(r)
End Sub
If you don't want to place the formula in one of the cells and calculate the result in VBA code, you can also use the following code:
Dim rng1 As Range, rng2 As Range, rng3 As Range
Dim y As Double
Set rng1 = ThisWorkbook.Names("Named_Range1").RefersToRange
Set rng2 = ThisWorkbook.Names("Named_Range2").RefersToRange
Set rng3 = ThisWorkbook.Names("Named_Range3").RefersToRange
y = WorksheetFunction.Max(rng1, rng2, rng3)
If named ranges are in another workbook, replace ThisWorkbook with Workbooks("workbook name")
For the result to be placed in F9, please try:
Sub Maximal()
Range("F9").Formula = "=MAX(MAX(Named_Range1),MAX(Named_Range2),MAX(Named_Range3))"
End Sub
Related
I'm trying to count the number of occurrences in a table based on two criteria. The formula below seems to work when I change the second criteria range from F3:S7 to one column F3:F7. I tried to create a dim range and set the range for the are and sub it into the formula, but that doesn't work either. How can I get the second criteria to accept a range of several columns instead of one?
Sub Count_number_of_occurrences_with_multiple_criteria()
'declare a variable
Dim ws As Worksheet
Set ws = Worksheets("CountPrep2 (2)")
'count the number of occurrences with two criteria
ws.Range("B10") = Application.WorksheetFunction.CountIfs(ws.Range("B3:B7"), ws.Range("B9"), ws.Range("F3:S7"), ws.Range("A10"))
End Sub
According to the Microsoft documentation about CountIfs, each additional range must have the same number of rows and columns as Range1.
My suggestion would be to recreate the effect of checking the criteria using For Loops that cycle though the desired ranges. Like so:
Sub Count_number_of_occurrences_with_multiple_criteria()
'declare a variable
Dim ws As Worksheet, _
rng1 As Range, rng2 As Range, _
r1Cell As Range, r2Cell As Range, _
counter As Integer
Set ws = Worksheets("CountPrep2 (2)")
Set rng1 = ws.Range("B3:B7")
Set rng2 = ws.Range("F3:S7")
For Each r1Cell In rng1
If r1Cell = ws.Range("B9") Then
For Each r2Cell In rng2
If r2Cell = ws.Range("A10") Then
counter = counter + 1
End If
Next r2Cell
End If
Next r1Cell
'count the number of occurrences with two criteria
MsgBox counter
End Sub
Note: I don't know the specifics of your data or file format so the method by which I check the criteria likely does not accurately count the number of occurrences. This is just an example of how you might achieve your desired results.
I now have a range A2:A10
But I need to select A1, how can I do that?
Thanks in advance!
Edit:
Sorry I should be more specific. It's part of a loop so the range is dynamic.
The following code solved my problem. I needed this to work for any columns not just A2:A10.
Sub test2()
Dim myRange As Range, desiredRange As Range
Set myRange = ThisWorkbook.Worksheets("Sheet1").Range("A2:A10")
Set desiredRange = myRange.Resize(1, 1).Offset(-1)
desiredRange.Select
End Sub
There is no loop shown in your code. If you repeatedly set the variable myRange using a single column then you simply use
myRange.Cells(1,1)
to refer to the first cell
set the range then loop item in range and select first item:
Dim Cell As Range, ws As Worksheet
Set ws = ActiveSheet
For Each Cell In ws.Range("A2:A10")
Cell.Select
exit for
Next Cell
As above,assuming I have declared rng1, rng2 rng3 etc.is it possible to select the declared ranges in 1 selection? If so please can you provide the code?
Thanks
This is a tricky one, but if you record a macro you will see that the format is something like:
Range("A1:A10,C1:C10").Select
To change this over to a variable (which is the only way I can figure out how to do it):
Sub test()
Dim rng1 As Range
Dim rng2 As Range
Set rng1 = Sheet1.Range("A1:A10")
Set rng2 = Sheet1.Range("C1:C10")
Range(rng1.Address & "," & rng2.Address).Select
End Sub
UPDATED As suggested by #user-somenumber- above that apparently was able to perfectly understand your question, but was too bothered by its brevity to post anything more than a hint, the UNION method works well here:
Sub test()
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Set rng1 = Sheet1.Range("A1:A10")
Set rng2 = Sheet1.Range("C1:C10")
Union(rng1, rng2).Select
End Sub
Assuming you have three named ranges (rnge1, rnge2, rnge3), you can select all of them in vba like this:
Range("rnge1,rnge2,rnge3").Select
Hi all!
I have been trying to make vba code for the following purpose: copy a range of a workbook (screenshot above A1:F2) to a new workbook.
I have managed to achieve this. There is one additional criteria which i would like to add to the vba code. The vba code should only copy those columns where row 2 has a value filled in.
Thus, looking at the example in the screenshot, this would mean that by running the vba code, I would save to a new workbook the ranges A1:A2, C1:C2, E1:E2.
The new worbook would look like the second screenshot
Any help appreciated! Thanks in advance!
A very useful way of ignoring blanks - without looping - is to use SpecialCells. The code below is probably a little lengthier than needed for your question but it is written so that
It can be adapted to other sheets
It will copy non-blanks from row 2 whether they are values and/or formulae
In absence of seeing your code it copies to a new workbook
code
Sub CopyEm()
Dim WB As Workbook
Dim ws As Worksheet
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Set ws = Sheets(1)
On Error Resume Next
Set rng1 = ws.Rows(2).SpecialCells(xlConstants)
Set rng2 = ws.Rows(2).SpecialCells(xlFormulas)
If rng1 Is Nothing Then
Set rng3 = rng2
ElseIf rng2 Is Nothing Then
Set rng3 = rng1
Else
Set rng3 = Union(rng1, rng2)
End If
If rng3 Is Nothing Then Exit Sub
On Error GoTo 0
Set WB = Workbooks.Add
rng3.Offset(-1, 0).Copy WB.Sheets(1).[a1]
rng3.Copy WB.Sheets(1).[a2]
End Sub
I'd like to create a macro that selects a rectangular range of cells and sets the name of every one of those cells to the value/contents of the cell.
In terms of what I've thought so far, I get an error though with the cell.Name line.
Public Sub NameCell()
Dim rng As Range
Dim cell As Range
Set rng = Range("A1:D1")
For Each cell In rng
cell.Name = CStr(cell.Value)
Next
End Sub
Is this what you meant?
Sub setVal()
Range("A1:C6").Select
Selection = "value"
End Sub
I believe this may work for you unless I also misunderstood the question.
Dim r As Range
Dim cell As Range
Set r = Sheet1.UsedRange
For Each cell In r
Sheet1.Names.Add Name:=cell.Value, RefersTo:=cell
Next
Keep in mind, though, that you would want to check that the cell.Value is valid (no spaces, etc.) for a named range.
To replace a range of cells with their values (removing any formulas from the range), you would use something like this.
Public Sub NameCell()
Dim rng As Range
Set rng = Range("A1:D1")
rng.Value = rng.Value
End Sub