I want to modify the Macro script to remove any negative value from my "Final" in the Excel file attached.

M Dallag
M Dallag used Ask the Experts™
on
I want to modify the Macro script to remove any value from my "Final" in the Excel file attached.
DDDD.xlsm
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
What do you want to replace on Final Worksheet and with what?
Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015
Commented:
Please replace the existing code with the following codes and see if that works for you...
Sub MakeFinal()
    Dim wsWell As Worksheet, wsFinal As Worksheet
    Dim rwWellMax As Long, rwFinal As Long
    Dim col As Integer, colWellMax As Integer, colStep As Integer
    
    Application.ScreenUpdating = False
    Set wsWell = ThisWorkbook.Worksheets("SDGM (Static)")
    Set wsFinal = ThisWorkbook.Worksheets("Final")
    
    rwWellMax = wsWell.Range("A1").CurrentRegion.Rows.Count
    colWellMax = wsWell.Range("A1").CurrentRegion.Columns.Count
    colStep = wsWell.Range("B1").End(xlToRight).Column
    
    wsWell.Activate
    wsFinal.Cells.Clear
    'Headers
    wsWell.Range("A1").Copy wsFinal.Range("A1")
    wsWell.Range(wsWell.Cells(2, 1), wsWell.Cells(2, colStep - 1)).Copy wsFinal.Range("B1")
    colStep = colStep - 2
    
    'Copy data
    For col = 2 To colWellMax Step colStep
        rwFinal = wsFinal.Range("A1").CurrentRegion.Rows.Count + 1
        'Well
        wsFinal.Range("A" & rwFinal & ":A" & (rwWellMax - 3 + rwFinal)) = wsWell.Cells(1, col)
        'Dates
        wsWell.Range("A3:A" & rwWellMax).Copy wsFinal.Range("B" & rwFinal)
        'Data
        wsWell.Range(wsWell.Cells(3, col), wsWell.Cells(rwWellMax, col + colStep - 1)).Copy wsFinal.Range("C" & rwFinal)
    Next col
    
    wsFinal.Activate
    'Clear cells with No Data
    wsFinal.Cells.Replace What:="No Data", Replacement:="", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    'Clear cells with Pt Created
    wsFinal.Cells.Replace What:="Pt Created", Replacement:="", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    'Clear cells with Scan Timeout
    wsFinal.Cells.Replace What:="Scan Timeout", Replacement:="", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
     'Clear cells with I/O Timeout
    wsFinal.Cells.Replace What:="I/O Timeout", Replacement:="", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    
    'Replace Negative Values
    ReplaceNegativeValues wsFinal
    'Adjust columns
    wsFinal.Columns.AutoFit
End Sub


'The following code replaces the negative values with blanks

Sub ReplaceNegativeValues(ws As Worksheet)
Dim x
Dim i As Long, j As Long
x = ws.Range("A1").CurrentRegion

For i = 1 To UBound(x, 1)
    For j = 1 To UBound(x, 2)
        If x(i, j) < 0 Then x(i, j) = ""     'If you want to replace negative values with 0, replace x(i, j) = "" with x(i, j) = 0 in this line
    Next j
Next i
ws.Range("A1").CurrentRegion.Value = x
End Sub

Open in new window

Top Expert 2014

Commented:
Do you want to use the absolute value of the cell values or clear the cell content?

Author

Commented:
Thanks
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
You're welcome!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial