Correct the vba code.

satputenandkumar0

New Member
Joined
Nov 28, 2012
Messages
12
Dear member,

I have below VBA code for output first and last series of the branches from the given data. But it takes more zeros in output. pls help me out from unnecessary zeros.

Code:
Sub x()

Dim r As Range, r1 As Range, n As Long, ws1 As Worksheet, ws2 As Worksheet

Application.DisplayAlerts = False
Application.ScreenUpdating = False
On Error Resume Next
Sheets("Data1").Delete
On Error GoTo 0
Set ws2 = Sheets.Add()
ws2.Name = "Data1"

Set ws1 = Sheets("Dispatch")
n = ws1.Range("A" & Rows.Count).End(xlUp).Row
ws1.Range("A1").Resize(n).AdvancedFilter xlFilterCopy, , ws2.Range("A1"), unique:=True
ws1.Range("B1").Resize(n).Copy ws2.Range("B1")
For Each r In ws2.Range("B2").Resize(n - 1)
    r = Left(r, 4)
Next r
ws2.Range("B1").Resize(n).AdvancedFilter xlFilterCopy, , ws2.Range("C1"), unique:=True
    
For Each r In ws2.Range("A2", ws2.Range("A" & Rows.Count).End(xlUp))
    For Each r1 In ws2.Range("C2", ws2.Range("C" & Rows.Count).End(xlUp))
        ws1.Range("F" & Rows.Count).End(xlUp)(2) = r
        ws1.Range("G" & Rows.Count).End(xlUp)(2).FormulaArray = "=MIN(IF(LEFT($B$2:$B$500,4)=" & Chr(34) & r1 & Chr(34) & ",IF($A$2:$A$500=" & Chr(34) & r & Chr(34) & ",VALUE(RIGHT($B$2:$B$500,LEN($B$2:$B$500)-1)))))"
        ws1.Range("H" & Rows.Count).End(xlUp)(2).FormulaArray = "=MAX(IF(LEFT($B$2:$B$500,4)=" & Chr(34) & r1 & Chr(34) & ",IF($A$2:$A$500=" & Chr(34) & r & Chr(34) & ",VALUE(RIGHT($B$2:$B$500,LEN($B$2:$B$500)-1)))))"
        ws1.Range("F2").CurrentRegion.Value = ws1.Range("F2").CurrentRegion.Value
    Next r1
Next r

Sheets("Data1").Delete

Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

Below is the Output from this code.



11215609011560982
11218900011890050
11200
11219804441980445
11200
34000
34000
34015900011590002
34000
34000
70400
70400
70400
70400
70460308986030899

<colgroup><col><col><col></colgroup><tbody>
</tbody>


Regards,
Nandkumar S.



<colgroup><col><col><col></colgroup><tbody>
</tbody>
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Forum statistics

Threads
1,214,917
Messages
6,122,233
Members
449,075
Latest member
staticfluids

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