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
Related
My problem is relatively simple: I'm attempting to create a named range by taking the Active Sheet's Used Range but excluding Column A.
Essentially I'm attempting to figure out how to do the opposite of a Union.
How would I go about this?
Thank you.
I haven't tried any solutions as none of the solutions I've found on Google or here on StackExchange are applicable to my current situation. All of them are highly specific, and I just need a general answer on how to remove cells from a defined range.
How about this:
Sub RangeMinusFirstColumn()
Dim rng As Range
Dim sh As Worksheet
Set sh = ActiveSheet
Set rng = sh.Range("A1").CurrentRegion
Set rng = rng.Offset(, 1)
Set rng = rng.Resize(, rng.Columns.Count - 1)
End Sub
I'm looking for an easy way to select the columns within a given Range.
The Sub below does this, but it seems to be too complicated...
Somebody an idea?
Sub selectColumnInCurrentRegion(columnNrToBeSelected as Integer)
Dim myRange As Range
Dim myArr As Variant 'just for debugging purposes
Dim myWS As Worksheet
Set myWS = ws_TargetForCGraphSearch 'Adjust your Worksheet
Dim upperLeftCornerAsName As String
upperLeftCornerAsName = "TargetResultStartPoint" 'Adjust your Corner
Set myRange = Application.Intersect(myWS.Range(upperLeftCornerAsName).CurrentRegion, _
myWS.Range(upperLeftCornerAsName).Offset(0, columnNrToBeSelected -1).EntireColumn)
myArr = myRange
myRange.Select
End Sub
Thank you in advance!
Use the columns property to access column of choice from CurrentRegion range object.
Set myRange = myWS.Range(upperLeftCornerAsName).CurrentRegion.Columns(columnNrToBeSelected)
Belts and braces, you would check myWS.Range(upperLeftCornerAsName).CurrentRegion is valid and the columnNrToBeSelected is <= .columns.Count of .CurrentRegion and >0.
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
In Excel 2010, how do I create a two column range from two separate ones using VBA?
The code below uses Union, but the combined range (rngAll) only contains the first column (rng1):
Dim rng1 As Range
Dim rng2 As Range
Dim rngAll As Range
Set rng1 = Range(TableColumn1)
Set rng2 = Range(TableColumn2)
Set rngAll = Application.Union(rng1, rng2)
Thanks.
Try this by qualifying the Range object with Sheet:
Set rng1 = Sheets(1).Range(TableColumn1)
Set rng2 = Sheets(1).Range(TableColumn2)
Set rngAll = Application.Union(rng1, rng2)
Then in the Watch Window or Immediate Window, you may check the rngAll.Address. It should show you both the ranges.
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