+ Reply to Thread
Results 1 to 6 of 6

Macro to eliminate zeroes if numbers in column match and both sets of columns have zeroes

Hybrid View

  1. #1
    Registered User
    Join Date
    01-15-2019
    Location
    Miami
    MS-Off Ver
    2016
    Posts
    66

    Macro to eliminate zeroes if numbers in column match and both sets of columns have zeroes

    I'm not sure if this is possible. I have a file that has as the unique identifier the JournalKey column. I need a macro that if the journal key is the same for both rows then verify if there are zeroes in each of these rows in columns E and F and H and I. Both sets of columns need to have zeroes, so both E and F have zeroes, and/or both H and I have zeroes. And if there are zeroes it should delete the value in the cell and leave it blank.

    For example in the image below the following would be done:

    - For rows 2 and 3 that are the same #, since both columns H and I have zeroes it would delete the zeroes and leave blank
    - For rows 4 and 5 nothing would be done
    - For rows 6 and 7, it would delete the zeroes in rows E and F.
    - For rows 8 and 9, it would delete the zeroes in rows E and F and in rows H and I
    - For rows 10 and 11, it would not delete anything because there is a value in column F and also a value in column I

    template.JPG

    I'm attaching this file also.

    Thanks in advance.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: Macro to eliminate zeroes if numbers in column match and both sets of columns have zer

    The code below is assuming that the data will always have the same pattern :
    1. two same number consecutively on column A
    2. so the total rows count for the whole data is always even

    So, my way is something like this :
    Count how many loop do I need by counting the total rows of the data divided by two.
    Then do the checking for case-1 (column E and F) and for case-2 (column H and I) :

    Sub test()
    Set sel = Range("A2")
    n = Range("A2", Range("A2").End(xlDown)).Rows.Count / 2
    
    For i = 1 To n
    
    'check for case-1
    If sel.Offset(0, 4) = 0 And sel.Offset(1, 5) = 0 Then
    sel.Offset(0, 4).Value = "" 'or maybe sel.Offset(0, 4).ClearContents
    sel.Offset(1, 5).Value = "" 'or maybe sel.Offset(1, 5).ClearContents
    End If
    
    'check for case-2
    If sel.Offset(0, 7) = 0 And sel.Offset(1, 8) = 0 Then
    sel.Offset(0, 7).Value = ""
    sel.Offset(1, 8).Value = ""
    End If
    
    'offset the sel to jump one row
    Set sel = sel.Offset(2, 0)
    
    Next i
    End Sub
    Last edited by karmapala; 09-12-2019 at 01:11 PM.

  3. #3
    Registered User
    Join Date
    01-15-2019
    Location
    Miami
    MS-Off Ver
    2016
    Posts
    66

    Re: Macro to eliminate zeroes if numbers in column match and both sets of columns have zer

    Thank you Karmapala. I double-checked the data and it sometimes has entries with 3 or 4 rows with the same Journal Key so neither assumption is accurate.. Sorry for not mentioning this before.

    Is there a way to do it if that's the case?

    I attached an example with the different cases.
    Attached Files Attached Files

  4. #4
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: Macro to eliminate zeroes if numbers in column match and both sets of columns have zer

    Hi luajambeiro,

    The code below is assuming that there is no blank row in between two cells with value in column-A.

    Please try the code below :
    (don't forget to remove the "B" bold "/B" attribute)

    Sub test()
    Set rng1 = Range("A2", Range("A2").End(xlDown))
    
    'create a helper cells to get unique value from column A
    rng1.Copy Destination:=Range("Z2")
    Set rng2 = Range("Z2", Range("Z2").End(xlDown))
    rng2.RemoveDuplicates Columns:=1, Header:=xlNo
    
    'loop through each value of the unique cell
    For Each r2Cell In rng2.Cells.SpecialCells(xlCellTypeConstants)
    
    With rng1
    .Replace r2Cell.Value, True, xlWhole, , False, , False, False
    
    Set rng3 = .SpecialCells(xlConstants, xlLogical)
    Set sum1 = Range(rng3.Offset(0, 4), rng3.Offset(0, 5))
    Set sum2 = sum1.Offset(0, 3)
    
    result1 = WorksheetFunction.Sum(sum1)
    result2 = WorksheetFunction.Sum(sum2)
    
    If result1 = 0 Then sum1.Cells.SpecialCells(xlCellTypeConstants).Value = ""
    If result2 = 0 Then sum2.Cells.SpecialCells(xlCellTypeConstants).Value = ""
    
    .Replace True, r2Cell.Value, xlWhole, , False, , False, False
    End With
    
    Next
    
    'clear the cells helper
    rng2.ClearContents
    End Sub
    The line which I bold is a code I just very knew (and learnt) in this week
    from one of the senior member here, Fluff13.

    Hope it works for you.
    Last edited by karmapala; 09-13-2019 at 12:41 PM.

  5. #5
    Registered User
    Join Date
    01-15-2019
    Location
    Miami
    MS-Off Ver
    2016
    Posts
    66

    Re: Macro to eliminate zeroes if numbers in column match and both sets of columns have zer

    This worked great! Thank you so much karmapala!!

  6. #6
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: Macro to eliminate zeroes if numbers in column match and both sets of columns have zer

    Glad I can help, and thanks for the rep, luajambeiro

    BTW, I just realized that maybe it doesn't need the ".SpecialCells(xlCellTypeConstants)"

    Sub test()
    Set rng1 = Range("A2", Range("A2").End(xlDown))
    
    'create a helper cells to get unique value from column A
    rng1.Copy Destination:=Range("Z2")
    Set rng2 = Range("Z2", Range("Z2").End(xlDown))
    rng2.RemoveDuplicates Columns:=1, Header:=xlNo
    
    'loop through each value of the unique cell
    For Each r2Cell In rng2
    
    With rng1
    .Replace r2Cell.Value, True, xlWhole, , False, , False, False
    
    Set rng3 = .SpecialCells(xlConstants, xlLogical)
    Set sum1 = Range(rng3.Offset(0, 4), rng3.Offset(0, 5))
    Set sum2 = sum1.Offset(0, 3)
    
    result1 = WorksheetFunction.Sum(sum1)
    result2 = WorksheetFunction.Sum(sum2)
    
    If result1 = 0 Then sum1.ClearContents
    If result2 = 0 Then sum2.ClearContents
    
    .Replace True, r2Cell.Value, xlWhole, , False, , False, False
    End With
    
    Next
    
    'clear the cells helper
    rng2.ClearContents
    End Sub
    Last edited by karmapala; 09-14-2019 at 05:14 PM.

+ 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] Delete all Zeroes and keep letters and Numbers
    By minayat in forum Excel General
    Replies: 8
    Last Post: 01-26-2017, 05:21 AM
  2. [SOLVED] Add leading zeroes to groups of numbers
    By boomcie in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-01-2015, 10:31 AM
  3. [SOLVED] leading zeroes Telephone numbers
    By databasef in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-21-2015, 09:40 AM
  4. [SOLVED] Hiding Zeroes & Rounding Numbers
    By Solon in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 04-13-2013, 01:04 AM
  5. Add zeroes within dates (NOT leading zeroes)
    By anthony19 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-13-2012, 04:08 PM
  6. HOW DO MAKE ZEROES DISAPPEAR WHEN THERE ARE NO NUMBERS TO SUM
    By tazman2168 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-10-2005, 09:00 AM
  7. [SOLVED] minimum from various columns without zeroes
    By joie in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 11-29-2005, 01:55 AM

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