VBA Adding another variable to a loop - excel

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

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.

Apply formatting to a range using array loop - VBA

Using VBA i make to:
Set two ranges
Create an array of all ranges names
Loop the array in order to apply formatting to each range in the array
BUT on line .Interior.Color = vbRed
i receive the below error:
Run-time error '424': Object required
Code
Sub test()
Dim rng1 As Range, rng2 As Range
Dim strRanges As Variant
Dim i As Long
Set rng1 = Sheet1.Range("A1:D1")
Set rng2 = Sheet2.Range("C5:H5")
strRanges = Split("rng1,rng2", ",")
For i = LBound(strRanges) To UBound(strRanges)
With strRanges(i)
.Interior.Color = vbRed
End With
Next i
End Sub
i have already use:
With Range(strRanges(i))
instead of:
With strRanges(i)
without any luck!
Any help will appreciate.
You can do this in just one line if the ranges are in the same worksheet
Sheet1.Range("A1:D1,C5:H5").Interior.Color = vbRed
You can use union if the ranges are in the same worksheet
Dim rng1 As Range, rng2 As Range
Set rng1 = Sheet1.Range("A1:D1")
Set rng2 = Sheet1.Range("C5:H5")
Union(rng1, rng2).Interior.Color = vbRed
You can use real arrays for your ranges if they are in different worksheets
Sub test()
Dim rng(1 To 2) As Range
Set rng(1) = Sheet1.Range("A1:D1")
Set rng(2) = Sheet2.Range("C5:H5")
Dim i As Long
For i = LBound(rng) To UBound(rng)
With rng(i)
.Interior.Color = vbRed
End With
Next i
End Sub
If you don't have numbered range variable names then you can use another array:
Sub test()
Dim rngABC As Range, rngXYZ As Range
Set rngABC = Sheet1.Range("A1:D1")
Set rngXYZ = Sheet1.Range("C5:H5")
Dim ArrRng() As Variant
ArrRng = Array(rngABC, rngXYZ)
Dim rng As Variant
For Each rng In ArrRng
rng.Interior.Color = vbRed
Next rng
End Sub
Note that if you think you have to use numbered variable names like
Dim rng1 As Range, rng2 As Range
this always is a clear sign for using an array instead:
Dim rng(1 To 2) As Range
numbered variable names are a bad practice. Always choose meaningful names.

Need help comparing values of two columns in different WorkBooks

I'm trying to compare two columns in two different WB let's say A and B which have only column each.
I'd like to msgbox a text whenever the value of cell in the column of A is also in the column of B.
I managed to put values in a variant variable and like now to compare them. I still get a 424 error at the final if statement that checks the correspondance.
Here is the code :
Option Explicit
Sub uniformisation()
Dim range1 As Variant
Dim range2 As Variant
Dim Tab1 As Variant, tab2 As Variant
Dim fichierM As Workbook
Dim fichierF As Workbook
Set fichierF = Workbooks.Open("thepath")
Set fichierMission = Workbooks.Open("thepath")
fichierF.Activate
fichierM.Activate
Dim wsF As Worksheet
Dim wsM As Worksheet
Set wsF = fichierF.Worksheets("test")
Set wsM = fichierM.Worksheets("A")
Dim C As range
Dim D As range
Set C = wsFlex.Columns(1)
Set D = wsMiss.Columns(1)
Dim TotalRows1 As Long
Dim TotalRows2 As Long
With wsF
TotalRows1 = C.Rows(Rows.Count).End(xlUp).Row
Tab1 = range(Cells(2, 1), Cells(TotalRows1, 1)).Value
MsgBox UBound(Tab1)
End With
With wsM
TotalRows2 = Rows(D.Rows.Count).End(xlUp).Row
tab2 = range(Cells(2, 2=1), Cells(TotalRows2, 1))
MsgBox UBound(tab2)
End With
For Each range1 In Tab1
For Each range2 In tab2
If range1.Value = range2.Value Then
MsgBox range1
End If
Next range2
Next range1
fichierM.Close
fichierF.Close
End Sub
Any help would be really apreciated, thanks !
you definitions are all over the place and the code is too long for what it is supposed to do. Also, you have chosen variant which is not really needed for what you want to do. Here is a shorter version that can get you started:
Sub CompareTwoColumns()
Dim rng1 As Range
Dim rng2 As Range
Dim WB1 As Workbook
Dim WB2 As Workbook
'make sure both workbooks are open
Set WB1 = Workbooks.Open("thepath1")
Set WB2 = Workbooks.Open("thepath2")
'loop through both columns and compare
For Each rng1 In WB1.Worksheets("Sheet1").UsedRange.Columns(1).Cells
For Each rng2 In WB2.Worksheets("Sheet1").UsedRange.Columns(1).Cells
If rng1.Value = rng2.Value Then
MsgBox rng1.Value
End If
Next rng2
Next rng1
End Sub

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