Excel VBA Union - excel

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.

Related

How to get range assigned to Named Range in excel vba?

I have already defined the named ranges in my workbook. I want to use the range for placing my pie chart. I am trying to write a code which sets range to variable and move the chart to the specific location.
Dim Rng As Range
Dim ChtObj As ChartObject
Set Rng = ThisWorkbook.Name("BT_GATE1").RefersTo
Set ChtObj = ActiveChart.Parent
ChtObj.Top = Rng.Top
I think I am missing something or using a worng method. Can some one help me assigning a range to variable 'Rng'?
A named range is either one cell or a collection of more cells which have been given a name. Ultimately it is a range. So in your case you can directly set it to a range as shown below
Dim Rng As Range
Set Rng = Range("BT_GATE1")
Debug.Print Rng.Address
Debug.Print Rng.Top
Debug.Print Rng.Parent.Name

Macro that copies from one tab and pastes in a different tab based on a match of a cell

I am trying to expand upon a worksheet and require assistance.
Currently I have the following macro set up to copy from one sheet range and paste to another sheet:
Private Sub CommandButton1_Click()
Dim Rng1 As Range
Dim Rng2 As Range
Set Rng1 = Range("D12:G253")
Set Rng2 = Range("D12:G253")
Rng2.Value = Rng1.Value
Dim xlws1 As Worksheet
Dim xlws2 As Worksheet
Set xlws1 = Worksheets("Data")
Set Rng1 = xlws1.Range("d12:g253")
Set xlws2 = Worksheets("Final")
Set Rng2 = xlws2.Range("D12:g253")
Rng2.Value = Rng1.Value
However, now I would like the data pasted to be dynamic and not always land in "D12:G253" but instead find the four columns that correspond to the value in either the seventh or eight row. I have descriptions set up in the first three columns that I will stay and not need to be copied and I have headers in the first 11 rows that will not need to change either.
I've tried following these posts Post 1 Post 2 but cannot follow completely due to my lack of coding knowledge.
Please help me so I can expand.

Is it possible to select multiple declared ranges?

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

Maximum of Multiple Named Ranges

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

Copy a range to a new workbook with a condition

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

Resources