+ Reply to Thread
Results 1 to 7 of 7

A simple vba problem

Hybrid View

  1. #1
    Registered User
    Join Date
    06-20-2014
    Location
    Croatia
    MS-Off Ver
    2010
    Posts
    17

    A simple vba problem

    Hi


    Could you please tell me how to write the sum of each found cell in range

    IF there is a value "DND" in a range I want to sum all found offset.value of DND and put the result in a desired cell.


    This code only gives me last value of DND instance

    Sub novi()
    
    
    
    Dim dnd As String
    Dim ofdnd As Range
    Dim rng As Range
    
    
    Set rng = Worksheets(1).Range("G9:G20")
    Set rng2 = Worksheets(1).Range("J1")
    
    For Each cell In rng
    
    If cell.Value = "DND" Then
    
      rng2 = application.sum(cell.Offset(, 1).Value)
    
    End If
    
    Next cell
    
    End Sub
    Attached Files Attached Files
    Last edited by petar256; 01-23-2018 at 05:55 AM.

  2. #2
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,876

    Re: A simple vba problem

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.
    Cheers!
    Deep Dave

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,710

    Re: A simple vba problem

    rng2 = rng2 + cell.Offset(, 1).Value
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,628

    Re: A simple vba problem

    Try this code
    Sub novi()
    
    Dim dnd As String
    Dim cel As Range
    Dim rng As Range
    Dim X As Long
    
    Set rng = Worksheets(1).Range("A1:A20")
    
    For Each cel In rng
    
    If cel.Value = "DND" Then
    
      Worksheets(2).Range("A1").Offset(X, 0) = "DND"
      Worksheets(2).Range("A1").Offset(X, 1) = cel.Offset(, 1).Value
      X = X + 1
    End If
    
    Next cel
    
    End Sub
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,710

    Re: A simple vba problem

    A couple of options:

    Option Explicit
    
    Sub novi()
    ' original with variables defined
    
    Dim dnd As String
    Dim ofdnd As Range
    Dim rng As Range
    Dim rng2 As Range
    Dim cell As Range
    
    Set rng = Worksheets(1).Range("G9:G20")
    Set rng2 = Worksheets(1).Range("J1")
    
    For Each cell In rng
        If cell.Value = "DND" Then
            rng2 = Application.Sum(cell.Offset(, 1).Value)
        End If
    Next cell
    
    End Sub
    
    Sub novi_v2()
    ' amended version with loop
    
    Dim rng As Range
    Dim rng2 As Range
    Dim cell As Range
    
    Set rng = Worksheets(1).Range("G9:G20")
    Set rng2 = Worksheets(1).Range("J1")
    
    rng2.Value = 0
    
    For Each cell In rng
        If cell.Value = "DND" Then
            rng2.Value = rng2.Value + cell.Offset(, 1).Value
        End If
    Next cell
    
    End Sub
    
    Sub novi_v3()
    ' using SUMIF without loop
    
    Dim rng As Range
    Dim rng2 As Range
    
    Set rng = Worksheets(1).Range("G9:G20")
    Set rng2 = Worksheets(1).Range("J1")
    
    rng2.Value = Application.WorksheetFunction.SumIf(rng, "DND", rng.Offset(, 1))
    
    End Sub

  6. #6
    Registered User
    Join Date
    06-20-2014
    Location
    Croatia
    MS-Off Ver
    2010
    Posts
    17

    Re: A simple vba problem

    Quote Originally Posted by TMS View Post
    A couple of options:

    Option Explicit
    
    Sub novi()
    ' original with variables defined
    
    Dim dnd As String
    Dim ofdnd As Range
    Dim rng As Range
    Dim rng2 As Range
    Dim cell As Range
    
    Set rng = Worksheets(1).Range("G9:G20")
    Set rng2 = Worksheets(1).Range("J1")
    
    For Each cell In rng
        If cell.Value = "DND" Then
            rng2 = Application.Sum(cell.Offset(, 1).Value)
        End If
    Next cell
    
    End Sub
    
    Sub novi_v2()
    ' amended version with loop
    
    Dim rng As Range
    Dim rng2 As Range
    Dim cell As Range
    
    Set rng = Worksheets(1).Range("G9:G20")
    Set rng2 = Worksheets(1).Range("J1")
    
    rng2.Value = 0
    
    For Each cell In rng
        If cell.Value = "DND" Then
            rng2.Value = rng2.Value + cell.Offset(, 1).Value
        End If
    Next cell
    
    End Sub
    
    Sub novi_v3()
    ' using SUMIF without loop
    
    Dim rng As Range
    Dim rng2 As Range
    
    Set rng = Worksheets(1).Range("G9:G20")
    Set rng2 = Worksheets(1).Range("J1")
    
    rng2.Value = Application.WorksheetFunction.SumIf(rng, "DND", rng.Offset(, 1))
    
    End Sub

    Thnk you! Exactly what I needed

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,710

    Re: A simple vba problem

    You're welcome. Thanks for the rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Simple Problem
    By Johnny0811 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-25-2017, 08:59 AM
  2. Percentage Formula on completions
    By Technopagen in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-02-2015, 05:13 AM
  3. Simple Problem I think
    By todd18us in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-01-2014, 07:20 PM
  4. My problem is simple - no - it's me that's simple...
    By Glenn Kennedy in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 07-08-2012, 02:20 PM
  5. [SOLVED] Simple problem im sure....
    By euanhampton in forum Excel General
    Replies: 2
    Last Post: 05-03-2012, 10:59 AM
  6. Simple Problem?
    By eadlam in forum Excel General
    Replies: 1
    Last Post: 09-01-2006, 10:01 PM
  7. Simple Problem - Please Help
    By mpeplow in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-17-2006, 02:12 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1