MAcro vba Code help

ananya11

New Member
Joined
Feb 1, 2018
Messages
6
HI all,

I am using one macro file with below code . it`s perfectly file but now I need add one condition in same macro file .

New condition is : if in my Raw data worksheet column E having some specific model like C400,n9000,q10000 .
than calculation method id different ,

example file enclosed SD worksheet is the required result sheet .





VBA code is :
Code:
Sub MyMacro()
Dim intCount As Integer
Dim ws1 As Worksheet, ws2 As Worksheet
Dim lngTRow, lngRow As Long
Set ws1 = Sheets("Raw Data")
Set ws2 = Sheets("SD")
 
'TargetDataRow
lngTRow = 8
ws1.Range("A1:B5").Copy
ws2.Range("A1").PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ws2.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
  
ws2.Range("A7:E7") = Array("Customer Name", "S ID", "Device ID", "Counter", "Date")
  
For lngRow = 8 To ws1.Cells(Rows.Count, "A").End(xlUp).Row
If Not ws1.Range("G" & lngRow).Value = "N/A" Then
  ws2.Range("A" & lngTRow).Value = ws1.Range("A" & lngRow).Value
  ws2.Range("B" & lngTRow).Value = ws1.Range("C" & lngRow).Value
  ws2.Range("E" & lngTRow).Value = ws1.Range("G" & lngRow).Value
  ws2.Range("E" & lngTRow).NumberFormat = "dd-mm-yy"
  For intCount = 1 To 8
    ws2.Range("A" & lngTRow + intCount).Value = ws2.Range("A" & lngTRow).Value
    ws2.Range("B" & lngTRow + intCount).Value = ws2.Range("B" & lngTRow).Value
    ws2.Range("E" & lngTRow + intCount).Value = ws2.Range("E" & lngTRow).Value
    ws2.Range("E" & lngTRow + intCount).NumberFormat = "dd-mm-yy"
  Next intCount
  ws2.Range("D" & lngTRow).Resize(7) = Application.Transpose(ws1.Range("H" & _
    lngRow).Resize(, 7).Value)
  For intCount = 0 To 4
    If ws2.Range("D" & lngTRow + intCount).Text = "N/A" Then
    ws2.Range("D" & lngTRow + intCount).Value = 0
    End If
  Next intCount
  ws2.Range("D" & lngTRow + 7).FormulaR1C1 = "=SUM(R[-6]C:R[-5]C)"
  ws2.Range("D" & lngTRow + 8).FormulaR1C1 = "=SUM(R[-4]C:R[-5]C)"
  ws2.Range("C" & lngTRow) = ws1.Range("D" & lngRow)
  ws2.Range("C" & lngTRow + 1) = ws1.Range("D" & lngRow) & "-DA3"
  ws2.Range("C" & lngTRow + 2) = ws1.Range("D" & lngRow) & "-DA4"
  ws2.Range("C" & lngTRow + 3) = ws1.Range("D" & lngRow) & "-NC3"
  ws2.Range("C" & lngTRow + 4) = ws1.Range("D" & lngRow) & "-NC4"
  ws2.Range("C" & lngTRow + 5) = ws1.Range("D" & lngRow) & "-MC"
  ws2.Range("C" & lngTRow + 6) = ws1.Range("D" & lngRow) & "-BC"
  ws2.Range("C" & lngTRow + 7) = ws1.Range("D" & lngRow) & "-B"
  ws2.Range("C" & lngTRow + 8) = ws1.Range("D" & lngRow) & "-C"
lngTRow = lngTRow + 9
End If
Next
End Sub
 
Last edited by a moderator:

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
For instance:
Code:
For lngRow = 8 To ws1.Cells(Rows.Count, "A").End(xlUp).Row
If Not ws1.Range("G" & lngRow).Value = "N/A" Then
    Select Case UCase(ws1.Range("E" & lngRow))
        Case "C400"
            'Code here
        Case "N9000"
            'Code here
        Case "Q10000"
            'Code here
    End Select
    End If
Next
 
Upvote 0
HI Thanks for Reply ,

This is data in my raw Sheet .

I need in my Raw data worksheet column E having some specific model like C400,n9000,q10000 .
than calculation method id different ,

Method is DA3-BC= DA3
NC3-MC=NC3


[TABLE="width: 1161"]
<tbody>[TR]
[TD]adasd[/TD]
[TD]sadaasd[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]sdsd[/TD]
[TD]sdsd[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]dsf[/TD]
[TD]sdf[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]dsf[/TD]
[TD="align: right"]954[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]afds[/TD]
[TD]dsf[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]TOTAL[/TD]
[TD]DA3[/TD]
[TD]DA4[/TD]
[TD]NC3[/TD]
[TD]NC4[/TD]
[TD]MC[/TD]
[TD]BC[/TD]
[/TR]
[TR]
[TD]Customer Name[/TD]
[TD]ID[/TD]
[TD]S ID[/TD]
[TD]Device ID[/TD]
[TD]P Name[/TD]
[TD]Installation Date (dd-mm-yyyy)[/TD]
[TD]Date and Time Last Received[/TD]
[TD="align: right"]11[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]13[/TD]
[TD="align: right"]112[/TD]
[TD="align: right"]113[/TD]
[TD="align: right"]172[/TD]
[TD="align: right"]173[/TD]
[/TR]
[TR]
[TD]abc[/TD]
[TD="align: right"]1323[/TD]
[TD]as239876[/TD]
[TD]as239876[/TD]
[TD]asdertfgyu C123 liffdgg[/TD]
[TD]02-24-2017[/TD]
[TD]09-21-2017 02:01 PM[/TD]
[TD="align: right"]31537[/TD]
[TD="align: right"]2082[/TD]
[TD="align: right"]1876[/TD]
[TD="align: right"]13856[/TD]
[TD="align: right"]13723[/TD]
[TD="align: right"]1826[/TD]
[TD="align: right"]40[/TD]
[/TR]
[TR]
[TD]xyz[/TD]
[TD="align: right"]4343[/TD]
[TD]uy567987[/TD]
[TD]uy567987[/TD]
[TD]asdertfgyu C400 liffdgg[/TD]
[TD]09-16-2014[/TD]
[TD]09-21-2017 11:30 AM[/TD]
[TD="align: right"]31537[/TD]
[TD="align: right"]2082[/TD]
[TD="align: right"]1876[/TD]
[TD="align: right"]13856[/TD]
[TD="align: right"]13723[/TD]
[TD="align: right"]1826[/TD]
[TD="align: right"]40[/TD]
[/TR]
</tbody><colgroup><col><col><col><col><col><col><col><col span="7"></colgroup>[/TABLE]
 
Upvote 0
HI

And After Running Macro code Result Sheet SD like

[TABLE="width: 492"]
<tbody>[TR]
[TD]adasd[/TD]
[TD]sadaasd[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]sdsd[/TD]
[TD]sdsd[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]dsf[/TD]
[TD]sdf[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]dsf[/TD]
[TD="align: right"]954[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]afds[/TD]
[TD]dsf[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Customer Name[/TD]
[TD]S ID[/TD]
[TD]Device ID[/TD]
[TD]Counter[/TD]
[TD]Date[/TD]
[/TR]
[TR]
[TD]abc[/TD]
[TD]as239876[/TD]
[TD]as239876[/TD]
[TD="align: right"]31537[/TD]
[TD="align: right"]21-09-17[/TD]
[/TR]
[TR]
[TD]abc[/TD]
[TD]as239876[/TD]
[TD]as239876-DA3[/TD]
[TD="align: right"]2082[/TD]
[TD="align: right"]21-09-17[/TD]
[/TR]
[TR]
[TD]abc[/TD]
[TD]as239876[/TD]
[TD]as239876-DA4[/TD]
[TD="align: right"]1876[/TD]
[TD="align: right"]21-09-17[/TD]
[/TR]
[TR]
[TD]abc[/TD]
[TD]as239876[/TD]
[TD]as239876-NC3[/TD]
[TD="align: right"]13856[/TD]
[TD="align: right"]21-09-17[/TD]
[/TR]
[TR]
[TD]abc[/TD]
[TD]as239876[/TD]
[TD]as239876-NC4[/TD]
[TD="align: right"]13723[/TD]
[TD="align: right"]21-09-17[/TD]
[/TR]
[TR]
[TD]abc[/TD]
[TD]as239876[/TD]
[TD]as239876-MC[/TD]
[TD="align: right"]1826[/TD]
[TD="align: right"]21-09-17[/TD]
[/TR]
[TR]
[TD]abc[/TD]
[TD]as239876[/TD]
[TD]as239876-BC[/TD]
[TD="align: right"]40[/TD]
[TD="align: right"]21-09-17[/TD]
[/TR]
[TR]
[TD]abc[/TD]
[TD]as239876[/TD]
[TD]as239876-B[/TD]
[TD="align: right"]3958[/TD]
[TD="align: right"]21-09-17[/TD]
[/TR]
[TR]
[TD]abc[/TD]
[TD]as239876[/TD]
[TD]as239876-C[/TD]
[TD="align: right"]27579[/TD]
[TD="align: right"]21-09-17[/TD]
[/TR]
[TR]
[TD]xyz[/TD]
[TD]uy567987[/TD]
[TD]uy567987[/TD]
[TD="align: right"]31537[/TD]
[TD="align: right"]21-09-17[/TD]
[/TR]
[TR]
[TD]xyz[/TD]
[TD]uy567987[/TD]
[TD]uy567987-DA3[/TD]
[TD="align: right"]2042[/TD]
[TD="align: right"]21-09-17[/TD]
[/TR]
[TR]
[TD]xyz[/TD]
[TD]uy567987[/TD]
[TD]uy567987-DA4[/TD]
[TD="align: right"]1876[/TD]
[TD="align: right"]21-09-17[/TD]
[/TR]
[TR]
[TD]xyz[/TD]
[TD]uy567987[/TD]
[TD]uy567987-NC3[/TD]
[TD="align: right"]12030[/TD]
[TD="align: right"]21-09-17[/TD]
[/TR]
[TR]
[TD]xyz[/TD]
[TD]uy567987[/TD]
[TD]uy567987-NC4[/TD]
[TD="align: right"]13723[/TD]
[TD="align: right"]21-09-17[/TD]
[/TR]
[TR]
[TD]xyz[/TD]
[TD]uy567987[/TD]
[TD]uy567987-MC[/TD]
[TD="align: right"]1826[/TD]
[TD="align: right"]21-09-17[/TD]
[/TR]
[TR]
[TD]xyz[/TD]
[TD]uy567987[/TD]
[TD]uy567987-BC[/TD]
[TD="align: right"]40[/TD]
[TD="align: right"]21-09-17[/TD]
[/TR]
[TR]
[TD]xyz[/TD]
[TD]uy567987[/TD]
[TD]uy567987-B[/TD]
[TD="align: right"]3918[/TD]
[TD="align: right"]21-09-17[/TD]
[/TR]
[TR]
[TD]xyz[/TD]
[TD]uy567987[/TD]
[TD]uy567987-C[/TD]
[TD="align: right"]25753[/TD]
[TD="align: right"]21-09-17[/TD]
[/TR]
</tbody><colgroup><col><col><col><col><col></colgroup>[/TABLE]


Thanks

Anu
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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