VBA, need to replace values of specific number of cells in a row(not the whole row), if the value of the first cell is 0.

Anunay

New Member
Joined
May 4, 2022
Messages
9
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
VBA, need to replace values of specific number of cells in a row(not the whole row), if the value of the first cell is 0. For example if the column A(SR NO.1) has a value 0 in any cell then the value of cells in that particular row up to column D needs to be replaced with a blank. Similarly in the Column E if the value of any cell is 0 then the value of cells in that row up to column H needs to be replaced with blank. This format is going to help with my Dataload implementation. A VBA code to counter this problem would be really helpful.

Book1
ABCDEFGH
1SR NO. 1DATACHARGESAUXSR NO.2DATACHARGESAUX
20RANDOM001SPECIFIC1001
30RANDOM000RANDOM00
41SPECIFIC10012MORE SPECIFIC2002
50RANDOM000RANDOM00
61SPECIFIC10011SPECIFIC1001
Sheet1
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hello Anunay, welcome.
You can try with few loops.
VBA Code:
Sub ClearZeroRow()
  
    Dim vRng, vLastCell As Range, vC As Long, vR As Long, _
             vN1 As Long, vN2 As Long, vN3 As Long
  
    With ActiveSheet
        Set vLastCell = .Cells.SpecialCells(xlLastCell)
        vRng = Range("A1", vLastCell)
        vC = vLastCell.Column
        vR = vLastCell.Row
        For vN1 = 1 To vC Step 4
            For vN2 = 2 To vR
                If vRng(vN2, vN1) = 0 Then
                    For vN3 = 0 To 3
                         vRng(vN2, vN1 + vN3) = ""
                    Next vN3
                End If
            Next vN2
        Next vN1
        .Range("A1").Resize(vR, vC) = vRng
    End With
  
End Sub
 
Last edited:
Upvote 0
Hello Anunay, welcome.
You can try with few loops.
VBA Code:
Sub ClearZeroRow()
 
    Dim vRng, vLastCell As Range, vC As Long, vR As Long, _
             vN1 As Long, vN2 As Long, vN3 As Long
 
    With ActiveSheet
        Set vLastCell = .Cells.SpecialCells(xlLastCell)
        vRng = Range("A1", vLastCell)
        vC = vLastCell.Column
        vR = vLastCell.Row
        For vN1 = 1 To vC Step 4
            For vN2 = 2 To vR
                If vRng(vN2, vN1) = 0 Then
                    For vN3 = 0 To 3
                         vRng(vN2, vN1 + vN3) = ""
                    Next vN3
                End If
            Next vN2
        Next vN1
        .Range("A1").Resize(vR, vC) = vRng
    End With
 
End Sub
Genius
 
Upvote 0
You can try with few loops.
.. or with no loops at all.

VBA Code:
Sub ClearPartRows()
  With Intersect(Rows("2:" & Range("A" & Rows.Count).End(xlUp).Row), Range("A:A,E:E"))
    .Replace What:=0, Replacement:="#N/A", LookAt:=xlWhole
    On Error Resume Next
    Intersect(.Areas(1).SpecialCells(xlConstants, xlErrors).EntireRow, Columns("A:D")).ClearContents
    Intersect(.Areas(2).SpecialCells(xlConstants, xlErrors).EntireRow, Columns("E:H")).ClearContents
    On Error GoTo 0
  End With
End Sub
 
Upvote 0
Solution
Guys Check out this new problem i got i can seem to wrap my head around it, i'll delete this message in 9 hours so it doesn't waste this thread.

 
Upvote 0

Forum statistics

Threads
1,215,034
Messages
6,122,782
Members
449,095
Latest member
m_smith_solihull

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