+ Reply to Thread
Results 1 to 4 of 4

Help with a macro producing incorrect results if data in rngC - please

Hybrid View

  1. #1
    Registered User
    Join Date
    11-07-2016
    Location
    UK
    MS-Off Ver
    2010
    Posts
    11

    Help with a macro producing incorrect results if data in rngC - please

    Hi

    In this macro I have
    rngA (Column A excluding Title)
    rngC (Column C excluding Title)
    rngOtherColumns (All columns to the right of C)

    What should happen when run
    Delete all values in rngC that = "Used", keep all other values
    Loop through rngA
    Where an exact match is found in rngOtherColumns on the same write "Used" rngC on the same row as the value searched for.

    2 error scenarios
    If rngC contains no values, the macro correctly writes "Used" to the correct cell - BUT, it deletes "used" Title
    If rngC contains values anywhere in the column it writes "Used" to the cell below where it should be written - it does not delete the "used" title

    Clearly some offset is occurring if data exists in Column C, I assume the same offset is protecting the title.

    Any help would be appreciated. ColinK
    Attached Files Attached Files

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,938

    Re: Help with a macro producing incorrect results if data in rngC - please

    Add the parts in red from the snippet of your code below:
        If Not foundCell Is Nothing Then
            If rngC.Cells(cell.Row - 1, 1).value = "" Then
                rngC.Cells(cell.Row - 1, 1).value = "Used"
            End If
        End If
    For info, it did not delete the "used" title for me.

    BSB

  3. #3
    Registered User
    Join Date
    11-07-2016
    Location
    UK
    MS-Off Ver
    2010
    Posts
    11

    Re: Help with a macro producing incorrect results if data in rngC - please

    Thanks for your prompt reply and suggested solution

    That is better but some anomalies
    If I start with column C empty, including Title, it misses Cow - and puts Used in the budgie row
    If I put any random text anywhere in Column C (leaving title blank) t works OK
    If I have the Title Used but no other data in column C it deleted the title and returns the correct results
    If I have the Title Used AND random data anywhere in column C it deleted the title and returns correct results for each row except Horse (row 2) - see screenshot

    I can probably live with that, however the title or no title in Column C should have no affect on the macro.Attachment 814453

  4. #4
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Help with a macro producing incorrect results if data in rngC - please

    Hello colink2.
    Try with:

    Sub ColC_Used_New()
    Dim a, Q&, i&, Rng As Range, C As Range
    a = Cells(1).CurrentRegion: Q = UBound(a)
    If Range("D1") = "" Then Range("D1") = "."
    Set Rng = Range("D1", [a1].SpecialCells(xlLastCell))
    For i = 1 To Q
      Set C = Rng.Find(a(i, 1), LookIn:=xlValues, LookAt:=xlWhole)
      If C Is Nothing Then a(i, 1) = Empty Else a(i, 1) = "Used: " & C.Address(0, 0)
    Next
    Columns("C").ClearContents: Range("C1").Resize(Q) = a
    End Sub
    You are always very welcome if you add reputation by clicking the * (bottom left) of each message that has helped you.

+ 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] VLookup producing incorrect table
    By Marvo in forum Excel Formulas & Functions
    Replies: 29
    Last Post: 02-16-2021, 12:02 PM
  2. [SOLVED] sum formula not working, producing incorrect answers Excel 2003
    By crzyg8r in forum Excel General
    Replies: 6
    Last Post: 09-28-2016, 12:14 PM
  3. Incorrect excel data table results
    By pisco in forum Excel General
    Replies: 0
    Last Post: 08-21-2015, 04:38 AM
  4. [SOLVED] Create Table Style Macro Not Producing Expected Results
    By mcclanat in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-19-2015, 10:47 AM
  5. [SOLVED] Sort macro conflicting with formula to produce incorrect results
    By turtlesrun in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-12-2015, 08:56 AM
  6. [SOLVED] Forumula Results Incorrect when data missing. Wanting to hide the incorrect formula.
    By firemedic6265 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 02-05-2014, 05:01 PM
  7. [SOLVED] Conditional format formula producing incorrect results?
    By Dougie12. in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-20-2014, 03:02 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