from nothing to more than 1.

ASadStudent

New Member
Joined
Oct 26, 2022
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hello everyone, I want to ask how I can change this line from nothing to more than one.
Everytime I try it doesn't seem to work.

VBA Code:
 If Not rng Is Nothing Then rng.Interior.Color = vbRed

Thanks a lot for helping me

(Here is the line of code of code where it is used in)
VBA Code:
 Sub Kijken_2()
  Dim omzet As Worksheet, Maandafsluiting As Worksheet
  Dim data As Variant, ky As Variant
  Dim lr As Long, rw As Long
  Dim d As Object, d2 As Object
  Dim rng As Range
 
  Set d = CreateObject("Scripting.Dictionary")
  Set d2 = CreateObject("Scripting.Dictionary")
 
  Set omzet = Workbooks.Item("Omzet").Sheets("Sheet1")
  Set Maandafsluiting = Workbooks.Item("Maandafsluiting").Sheets(1)
 
  lr = omzet.Cells(Rows.Count, 2).End(3).Row
  With omzet.Cells(1, 1).Resize(lr, 14)
    data = .Value
    .Interior.ColorIndex = xlNone
  End With
 
  For rw = LBound(data) To UBound(data)
    If data(rw, 14) <> 0 Then
      ky = data(rw, 2)
      If Not d.exists(ky) Then
        d(ky) = data(rw, 14) & "|" & rw
      End If
    End If
  Next rw
 
  lr = Maandafsluiting.Cells(Rows.Count, 2).End(3).Row
  data = Maandafsluiting.Cells(1, 1).Resize(lr, 6).Formula
  
  For rw = LBound(data) To UBound(data)
    ky = data(rw, 2)
    d2(ky) = Empty
    If d.exists(ky) Then
      data(rw, 6) = Split(d(ky), "|")(0)
    End If
  Next rw

  For Each ky In d.keys
    If Not d2.exists(ky) Then
      rw = Split(d(ky), "|")(1)
      If rng Is Nothing Then
        Set rng = omzet.Cells(rw, 2)
      Else
        Set rng = Union(rng, omzet.Cells(rw, 2))
      End If
    End If
  Next
 
  If Not rng Is Nothing Then rng.Interior.Color = vbRed
 
 
  Maandafsluiting.Cells(1, 6).Resize(UBound(data)).Formula = Application.Index(data, 0, 6)
End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi ASadStudent,

the code checks if the range has been assigned or not by checking for If Not rng Is Nothing Then and if the rng exists work with it - I won't see any benefit in using something like

VBA Code:
If rng.Cells.Count > 0 Then

Ciao,
Holger
 
Upvote 0
Hi ASadStudent,

the code checks if the range has been assigned or not by checking for If Not rng Is Nothing Then and if the rng exists work with it - I won't see any benefit in using something like

VBA Code:
If rng.Cells.Count > 0 Then

Ciao,
Holger
Thanks for the suggestion.
This line of code is as a check to see if the product number in the first excel file is found in the second one.

If it isn't found then I want it to turn red and if it is found multiple times then I want it to turn orange.

So do you know how I can change this line so that I can also do the second check ?
 
Upvote 0
Try to declare rng as variant
Replace:
VBA Code:
Dim rng As Range
by
VBA Code:
Dim rng
 
Upvote 0
Can't say for sure for your case, but depending, checking for a comma inside rng.Address may help.

Another approach would be keeping a counter where you union found ranges. If counter > 1 then multiple ranges were found. You may even use a single boolean you set to true in the same place.

Another Edit: If rng is only setting a single cell, then HaHoBe's if statement would work just make it greater than 1.
 
Last edited:
Upvote 0
Try
VBA Code:
If Not Rng Is Nothing Then
Rng.Interior.Color = vbRed
ElseIf Rng.Cells.Count = 1 Then
Rng.Interior.Color = "as required"
ElseIf Rng.Cells.Count = 2 Then
Rng.Interior.Color = "as required"
End If
 
Upvote 0
Solution

Forum statistics

Threads
1,215,362
Messages
6,124,502
Members
449,166
Latest member
hokjock

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top