VBA help with cell value

decadence

Well-known Member
Joined
Oct 9, 2015
Messages
525
Office Version
  1. 365
  2. 2016
  3. 2013
  4. 2010
  5. 2007
Platform
  1. Windows
Hi, I am trying to get the data from one cell value and add it to another cell value but the result i keep getting is just one cell value being added to all the other cells values, can someone help please.

Code:
    Dim Rng1 As Range, i As Range, Rng2 As Range, x As Range, a As Range, b As Range
    Application.ScreenUpdating = False
    For Each i In Range("A1:Z2")
    Select Case i.value2
    Case "IPN", "Part", "Part Number"
        If Not Rng1 Is Nothing Then
            Set Rng1 = Union(Rng1, i)
        Else
            Set Rng1 = i
        End If
            End Select
        Next
    If Rng1 Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    If Not Rng1 Is Nothing Then
        Set Rng1 = Rng1.Resize(Cells(Rows.Count, Rng1.Column).End(xlUp).row).Offset(1).SpecialCells(xlCellTypeVisible)
    End If
    Application.ScreenUpdating = False
    For Each x In Range("A1:Z2")
    Select Case x.value2
    Case "Foot", "Package", "Pack"
        If Not Rng2 Is Nothing Then
            Set Rng2 = Union(Rng, x)
        Else
            Set Rng2 = x
        End If
            End Select
        Next
    If Rng2 Is Nothing Then
    MsgBox "Footprint not Found!"
    Exit Sub
    End If
    Application.ScreenUpdating = False
    If Not Rng2 Is Nothing Then
        Set Rng2 = Rng2.Resize(Cells(Rows.Count, Rng2.Column).End(xlUp).row).Offset(1).SpecialCells(xlCellTypeVisible)
    End If
    Application.ScreenUpdating = False
     For Each a In Rng1
        For Each b In Rng2
     If a.value2 = "DNF_" Then
     a.Cells.value2 = a.Cells.value2 + b.Cells.value2
            End If
        Next
    Next
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I was toggling with the idea of filtering that's why special cells have been added in the previous post. Here is the same code but without special, Apologies for the mistake.

Code:
    Dim Rng1 As Range, i As Range, Rng2 As Range, x As Range, a As Range, b As Range
    Application.ScreenUpdating = False
    For Each i In Range("A1:Z2")
    Select Case i.value2
    Case "IPN", "Part", "Part Number"
        If Not Rng1 Is Nothing Then
            Set Rng1 = Union(Rng1, i)
        Else
            Set Rng1 = i
        End If
            End Select
        Next
    If Rng1 Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    If Not Rng1 Is Nothing Then
        Set Rng1 = Rng1.Resize(Cells(Rows.Count, Rng1.Column).End(xlUp).row).Offset(1)
    End If
    Application.ScreenUpdating = False
    For Each x In Range("A1:Z2")
    Select Case x.value2
    Case "Foot", "Package", "Pack"
        If Not Rng2 Is Nothing Then
            Set Rng2 = Union(Rng, x)
        Else
            Set Rng2 = x
        End If
            End Select
        Next
    If Rng2 Is Nothing Then
    MsgBox "Footprint not Found!"
    Exit Sub
    End If
    Application.ScreenUpdating = False
    If Not Rng2 Is Nothing Then
        Set Rng2 = Rng2.Resize(Cells(Rows.Count, Rng2.Column).End(xlUp).row).Offset(1)
    End If
    Application.ScreenUpdating = False
     For Each a In Rng1
        For Each b In Rng2
     If a.value2 = "DNF_" Then
     a.Cells.value2 = a.Cells.value2 + b.Cells.value2
            End If
        Next
    Next
End Sub
 
Last edited:
Upvote 0
I'm guessing, that it's the final part of your code that isn't working correctly.
But could you please let us know what you are trying to achieve?
 
Upvote 0
Hi Fluff, Yes it's the last bit of code i believe
Code:
     a.Cells.value2 = a.Cells.value2 + b.Cells.value2

I Am trying to get the cell value from Rng2 and add it to Rng1 but only in the Cells of Rng1 where DNF_ is present

Example

From This
[TABLE="class: grid, width: 100"]
<tbody>[TR]
[TD]Rng1
[/TD]
[TD]Something
[/TD]
[TD]Something
[/TD]
[TD]Something
[/TD]
[TD]Rng2
[/TD]
[TD]Something
[/TD]
[/TR]
[TR]
[TD]DNF_
[/TD]
[TD]data
[/TD]
[TD]data
[/TD]
[TD]data
[/TD]
[TD]Found
[/TD]
[TD]data
[/TD]
[/TR]
[TR]
[TD]data
[/TD]
[TD]data
[/TD]
[TD]data
[/TD]
[TD]data
[/TD]
[TD]data
[/TD]
[TD]data
[/TD]
[/TR]
[TR]
[TD]DNF_
[/TD]
[TD]data
[/TD]
[TD]data
[/TD]
[TD]data
[/TD]
[TD]Found
[/TD]
[TD]data
[/TD]
[/TR]
</tbody>[/TABLE]



To This
[TABLE="class: grid, width: 100"]
<tbody>[TR]
[TD]Rng1
[/TD]
[TD]Something
[/TD]
[TD]Something
[/TD]
[TD]Something
[/TD]
[TD]Rng2
[/TD]
[TD]Something
[/TD]
[/TR]
[TR]
[TD]DNF_Found
[/TD]
[TD]data
[/TD]
[TD]data
[/TD]
[TD]data
[/TD]
[TD]Found
[/TD]
[TD]data
[/TD]
[/TR]
[TR]
[TD]data
[/TD]
[TD]data
[/TD]
[TD]data
[/TD]
[TD]data
[/TD]
[TD]data
[/TD]
[TD]data
[/TD]
[/TR]
[TR]
[TD]DNF_Found
[/TD]
[TD]data
[/TD]
[TD]data
[/TD]
[TD]data
[/TD]
[TD]Found
[/TD]
[TD]data
[/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:
Upvote 0
Try
Code:
    For Each a In Rng1
        If a.Value2 = "DNF_" Then
            a.Value2 = a.Value2 + Cells(a.Row, rng2.Column).Value2
        End If
    Next a
 
Upvote 0
Thank you Fluff, It works perfectly how I would like it to.
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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