VBA copying a range of cells - excel

Here is my code. Please help!
I am trying to copy a discontinuous range of cells.
Sub ()
Worksheets("Name").Activate
Dim Rng1 As Range
Set Rng1 = Range("B24:K24")
Dim Rng2 As Range
Set Rng2 = Rng1.End(xlDown)
Dim Rng3 As Range
Set Rng3 = Union(Range("C9"), Range("C11"), Range("C13"), Range("C15"), Rng2)
Rng3.Copy
End Sub

Related

Using for each with a worksheet reference

If ws below has both the range object and cell property:
dim ws as worksheet
set ws = sheets("sheet_1")
x = ws.range("A1")
x = ws.cells(1,1)
Why doesn't this work:
dim cel as range
dim rng as range
set rng = 'some range
for each cel in rng
ws.cel.value = "foo"
next
I want to ensure the right sheet is being referenced, and it seems logical to use ws as part of the cel reference to ensure the sheets reference is explicit.
You need to include how you set the rng, that's a vital bit of info to help troubleshoot. But generally, this should work:
Sub t()
Dim ws As Worksheet
Set ws = Sheets("sheet_1")
Dim cel As Range, rng As Range
Set rng = ws.Range("A1:C100")
For Each cel In rng.Cells
cel.Value = "foo"
Next
End Sub

VBA Begginer - Set multiple PivotItems in a range

I'm trying to set a range so I can automatically add background color to specific cells. I'm writing a code, that is not completed, but I'm stuck with something.
I can color all single cells that has value and from just one kind of "type"... I have multiple "types" and I wanted to reference them all!!
My code is like this:
Dim ws As Worksheet
Dim pt As PivotTable
Dim rng1 As Range
Dim rng2 As Range
Dim rng As Range
Set ws = ActiveSheet
Set pt = ws.PivotTables("PivotTable14")
Set rng1 = pt.DataBodyRange
Set rng2 = pt.PivotFields("Type").PivotItems("Residential").DataRange
For Each rng In Intersect(rng1, rng2)
If rng.Value > 0 Then
rng.Interior.Color = vbYellow
End If
Next
How can I reference all PivotItems inside PivotFields "Type"???
I tried something like:
Set rng2 = pt.PivotFields("Type").PivotItems("(All)").DataRange
But that didn't work.

VBA Adding another variable to a loop

I have a method CreatePage(). I would like to loop through variables to pass through the method. I have managed to get it working for one variable:
Sub main()
Dim rng As Range
Dim aSht As Worksheet
Set aSht = ThisWorkbook.Sheets("Sheet1")
For Each rng In aSht.Range("A5:A8")
CreatePage(rng)
Next rng
End Sub
I need it to loop as follows:
CreatePage("A5", "C6")
CreatePage("A6", "C7")
CreatePage("A7", "C8"), ...
I am stuck on how to pass through two variables. This is where I have got so far, but I don't think I'm going in the right direction:
Sub main()
Dim rng As Range
Dim rng2 As Range
Dim aSht As Worksheet
Dim bSht As Worksheet
Set aSht = ThisWorkbook.Sheets("Sheet1")
Set bSht = ThisWorkbook.Sheets("Sheet2")
For Each rng In aSht.Range("A5:A8")
For Each rng2 In bSht.Range("C6:C9")
CreatePage(rng,rng2)
Next rng, rng2
End Sub
I have adjusted CreatePage() to hold two variables. I'm puzzled on getting the second variable in the for loop. Can anyone help out?
A nested for loop won't help here. You need to loop one range only and pick the required cell from the other
Sub main()
Dim rng As Range
Dim rng2 As Range
Dim aSht As Worksheet
Dim bSht As Worksheet
Set aSht = ThisWorkbook.Sheets("Sheet1")
Set bSht = ThisWorkbook.Sheets("Sheet2")
Dim idx As Long
Set rng = aSht.Range("A5:A8")
Set rng2 = bSht.Range("C6:C9")
'You might want to add code here to check the size and shape of your ranges
For idx = 1 To rng.Cells.Count
CreatePage rng.Cells(idx), rng2.Cells(idx)
Next
End Sub
Just a simple change needed.
For Each rng In aSht.Range("A5:A8")
For Each rng2 In bSht.Range("C6:C9")
CreatePage rng, rng2
Next rng2
Next rng

Subtract two ranges to add formula in a part of a column table

I want to add formula into one table column, except the first row of this same column (whose value need to be fixed).
So I've try to subtract the column data range from his first cell :
Dim rng1 As Range, rng2 As Range, rng3 As Range
rng1 = Sheets("MySheet").ListObjects("MyTable").ListColumns("Column2").DataBodyRange
rng2 = Sheets("MySheet").ListObjects("MyTable").ListColumns("Column2").DataBodyRange.Cells(1)
rng3 = rng1 - rng2
rng3.Formula = "=SUM(D179;G179)"
But this return me an execution error '91'
As Gary's says, you need to set the range,
For example:
Dim rng1 As Range, rng2 As Range, rng3 As Range
set rng1 = Sheets("MySheet").ListObjects("MyTable").ListColumns("Column2").DataBodyRange
set rng2 = Sheets("MySheet").ListObjects("MyTable").ListColumns("Column2").DataBodyRange.Cells(1)
'rng3 = rng1 - rng2 'you have not indicate what rng3 range is
rng3.Formula = "=SUM(D179;G179)"'still no indication what rng3 is

finding out what is the range with Endxl

First get the range of non-empty cells from Source workbook. Then select similar range of cells in Destination Workbook. How can this be achieved?
Full Code::
~~~~~~~~~~~
Public Sub ConvertTo_K()
Dim rng1 As Range
Dim rng2 As Range
Set rng1 = Workbooks("Source.xls").Worksheets("Source").Range(Cells(2, "A"), Cells(Rows.Count, "A").End(xlUp).Resize(, 1))
Set rng2 = Workbooks("Destination.xls").Worksheets("Destination").Range(rng1.Address)
rng2.Value = Round(rng1.Value / 1000, 2)
'At this point, an error message of Type Mismatch pops up (Due to different ranges of rng1 and rng2). Do i have to use a loop? How to do that?
End Sub
Round(rng1.Value / 1000, 2) will not work because rng1 is a collection of ranges and can be accessed by rng1.Cells(1).value
rng1.Value is not valid.
You may loop thru each cell of rng2 and apply the round formula.
Public Sub ConvertTo_K()
Dim rng1 As Range
Dim rng2 As Range
Dim RoundRange As Range
Dim rngVal As Double
Dim SourceWkb As Workbook
Set SourceWkb = Workbooks("Source.xls")
Dim SourceSht As Worksheet
Set SourceSht = SourceWkb.Worksheets("Source")
With SourceSht
Set rng1 = .Range(.Cells(2, "A"), .Cells(.Rows.Count, "A").End(xlUp).Resize(, 1))
End With
Dim DestinWkb As Workbook
Set DestinWkb = ThisWorkbook 'Workbooks("Destination.xls")
Dim DestinSht As Worksheet
Set DestinSht = DestinWkb.Worksheets("Destination")
With DestinSht
Set rng2 = .Range(rng1.Address)
End With
'rng2.Value = Round(rng1.Value / 1000, 2) This wont work
rng1.Copy rng2
For Each cell In rng2
cell.Value = Round(cell / 1000, 2)
Next
End Sub
If your rng1 works fine you could do it this way:
set rng2 = Workbooks("Destination.xls").Worksheets("Destination").Range(rng1.address)

Resources