Correction of two algorithms calculates empty lines - vba

Is it possible to correct the two algorithms? when I do a filtering by criteria, I calculate two columns, "Pareto_Analysis" and "cumulates", my problem is: that the two algorithms don't calculate the data filtered, but calculate all the lines.
Here is an example of filtering on this screen printer
Code algorithm for "Pareto_Analysis":
Sub calculDefect()
Dim ws As Worksheet
Set ws = Sheet7
With ws
Const SourceColumn As String = "G"
Const DestColumn As String = "K"
Const TotalCell As String = "H4" 'total defect of all defect
Const StartRow As Integer = 11
Const EndRow As Integer = 100
For i = StartRow To EndRow
ws.Range(DestColumn & i).Formula = "=(" & SourceColumn & i & "/" & TotalCell & ")*100"
Next i
End With
End Sub
Code algorithm for "cumule":
Sub calculatCumule()
Dim ws As Worksheet
Set ws = Sheet7
With ws
LastRow = ActiveSheet.Cells(Rows.Count, 11).End(xlUp).Row
Range("L11") = Range("K11").Value
Range("L12").FormulaR1C1 = "=R[-1]C+RC[-1]"
Range("L12").AutoFill Destination:=Range("L12:L" & LastRow & "")
End With
End Sub
To clarify the question further, here are some more screen dumps:
Here I chose the criteria I wanted:
Displays the data I have chosen:
Calculate the "pareto" column based on the column "Quantity_prod" and the column"cumule" based on the column "pareto":
And if you notice in the last picture the rest of the columns has 0 and 100 that repeats, normally should just calculate the 4 lines.

Try the 2 modified 'Algorithms" code below.
First, you need to run Sub calculDefect, and after run Sub calculatCumule.
Sub calculDefect()
Dim ws As Worksheet
Const SourceColumn As String = "G"
Const DestColumn As String = "K"
Const TotalCell As String = "H4" 'total defect of all defects
Const StartRow As Long = 11
Dim EndRow As Long, i As Long
Set ws = Sheet7
With ws
EndRow = .Range("G" & StartRow).End(xlDown).Row '<-- get last row with data in Column G
For i = StartRow To EndRow
.Range(DestColumn & i).Formula = "=(" & SourceColumn & i & "/" & TotalCell & ")*100"
Next i
End With
End Sub
'====================================================================
Sub calculatCumule()
Dim ws As Worksheet
Dim LastRow As Long
Set ws = Sheet7
With ws
LastRow = .Cells(.Rows.Count, "K").End(xlUp).Row '<-- get last row with data in Column K
.Range("L11") = .Range("K11").Value
.Range("L12").FormulaR1C1 = "=R[-1]C+RC[-1]"
.Range("L12:L" & LastRow).FillDown
End With
End Sub
Screen-shot of the results I've got running this code:
Edit 1: same 2 "algorithms" that work when you filter the data:
Sub calculDefect()
Dim ws As Worksheet
Const SourceColumn As String = "G"
Const DestColumn As String = "K"
Const TotalCell As String = "H4" 'total defect of all defects
Const StartRow As Long = 11
Dim EndRow As Long, i As Long
Dim VisRng As Range, C As Range
Set ws = Sheet7
With ws
EndRow = .Range("G" & StartRow).End(xlDown).Row '<-- get last row with data in Column G
' set visible range to only filtered cells in Column G
Set VisRng = .Range(Range(SourceColumn & StartRow), Range(SourceColumn & EndRow)).SpecialCells(xlCellTypeVisible)
.Range(TotalCell).Formula = WorksheetFunction.Sum(VisRng) '<-- re-calculate Total defects according to visible range
For Each C In VisRng
.Range(DestColumn & C.Row).Formula = "=(" & SourceColumn & C.Row & "/" & TotalCell & ")*100"
Next C
End With
End Sub
'=================================================================
Sub calculatCumule()
Dim ws As Worksheet
Dim VisRng As Range, C As Range
Dim StartRow As Long
Dim LastRow As Long
Set ws = Sheet7
With ws
LastRow = .Cells(.Rows.Count, "K").End(xlUp).Row '<-- get last row with data in Column K
StartRow = 11 '<-- init value
' set visible range to only filtered cells in Column G
Set VisRng = .Range(Range("K" & StartRow), Range("K" & LastRow)).SpecialCells(xlCellTypeVisible)
StartRow = VisRng.Item(1).Row '<-- update first row in visible range
For Each C In VisRng
If C.Row = StartRow Then
.Range("L" & C.Row) = .Range("K" & C.Row).Value
Else
.Range("L" & C.Row).Formula = "=SUBTOTAL(9,K" & StartRow & ":K" & C.Row & ")"
End If
Next C
End With
End Sub
Screen-shot of the results I've got running this code when filtering "Type_defect" to CPE02:

Related

Splitting Excel column into different tabs

I am trying to split data from an Excel column in to different tabs for each unique value. For example, I'd like a tab for each unique value in the Concat field that lists only the records for that specific person.
Currently using this code, which splits the tabs out correctly, but each tab has all of the worksheet's data, not just the individualized data.
Sub parse_data()
Dim lr As Long
Dim ws As Worksheet
Dim vcol, i As Integer
Dim icol As Long
Dim myarr As Variant
Dim title As String
Dim titlerow As Integer
vcol = 5
Set ws = Sheets("qReconcilers")
lr = ws.Cells(ws.Rows.Count, vcol).End(xlUp).Row
title = "A1:N1"
titlerow = ws.Range(title).Cells(1).Row
icol = ws.Columns.Count
ws.Cells(1, icol) = "Unique"
For i = 2 To lr
On Error Resume Next
If ws.Cells(i, vcol) <> "" And Application.WorksheetFunction.Match(ws.Cells(i, vcol), ws.Columns(icol), 0) = 0 Then
ws.Cells(ws.Rows.Count, icol).End(xlUp).Offset(1) = ws.Cells(i, vcol)
End If
Next
myarr = Application.WorksheetFunction.Transpose(ws.Columns(icol).SpecialCells(xlCellTypeConstants))
ws.Columns(icol).Clear
For i = 2 To UBound(myarr)
ws.Range(title).AutoFilter field:=vcol, Criteria1:=myarr(i) & ""
If Not Evaluate("=ISREF('" & myarr(i) & "'!A1)") Then
Sheets.Add(after:=Worksheets(Worksheets.Count)).Name = myarr(i) & ""
Else
Sheets(myarr(i) & "").Move after:=Worksheets(Worksheets.Count)
End If
ws.Range("A" & titlerow & ":A" & lr).EntireRow.Copy Sheets(myarr(i) & "").Range("A1")
Sheets(myarr(i) & "").Columns.AutoFit
Next
ws.AutoFilterMode = False
ws.Activate
End Sub
How would I get those tabs to only display the data pertaining to that particular person?
Something like this should be ok for a beginning:
Public Sub TestMe()
Dim defaultColumn As Long
defaultColumn = 5 'column E
Dim wks As Worksheet
Dim sourcelastRow As Long
Set wks = Worksheets(1) 'the main worksheet
sourcelastRow = lastRow(wks.Name)
Dim cnt As Long
Dim checkCell As Range
For cnt = 2 To sourcelastRow
Set checkCell = wks.Cells(cnt, defaultColumn)
If WorksheetFunction.IsErr(Evaluate("'" & Trim(checkCell) & "'!A1")) Then
Dim newSheet As Worksheet
Set newSheet = ThisWorkbook.Worksheets.Add(after:=Worksheets(Worksheets.Count))
newSheet.Name = Trim(checkCell.Value2)
End If
Dim targetLastRow As Long
targetLastRow = lastRow(checkCell.Value2) + 1
Worksheets(checkCell.Value2).Rows(targetLastRow).Value2 = wks.Rows(cnt).Value2
Next cnt
End Sub
Public Function lastRow(Optional strSheet As String, _
Optional column_to_check As Long = 1) As Long
Dim shSheet As Worksheet
If strSheet = vbNullString Then
Set shSheet = ActiveSheet
Else
Set shSheet = Worksheets(strSheet)
End If
lastRow = shSheet.Cells(shSheet.Rows.Count, column_to_check).End(xlUp).Row
End Function
What the code does:
loops through every cell in column number 5 of the first worksheet;
checks whether there is a worksheet with the name of this column";
WorksheetFunction.IsErr(Evaluate("'" & Trim(checkCell) & "'!A1"))
if there is no such worksheet - it creates it;
then finds the worksheet with this column and at the row after the last used row of the worksheet it writes the values of the corresponding row;

Trying to look at values in one column and update value in anotehr column

I'm trying create a macro to look at column F and then update Column I all the way down from the first row until the last.
so read column F for SBS and then update the corresponding row on column I with "National"
THis is my code.
Dim LastRow As String
Dim d As String
LastRow = Range("F" & Rows.Count).End(xlUp).Row
For d = SBS To LastRow
If Not IsEmpty(Range("F" & d).Value) Then
Range("I" & d).Value = "National"
End If
Next d
Made minor changes in your macro in Dim statement for d and if condition. Also not empty condition is not required:-
Sub Macro1()
Dim LastRow As Long
Dim d As Long
LastRow = Range("F" & Rows.Count).End(xlUp).Row
For d = 1 To LastRow
If Range("F" & d).Value = "SBS" Then
Range("I" & d).Value = "National"
End If
Next d
End Sub
Here is quick example
Option Explicit
Public Sub Example()
Dim Sht As Worksheet
Dim Rng As Range
Set Sht = ThisWorkbook.Sheets("Sheet1")
For Each Rng In Sht.Range("F1", Sht.Range("F9999").End(xlUp))
If Rng.Value = "SBS" Then
Rng.Cells.Offset(0, 3).Value = "National"
End If
Next
End Sub

Type mismatch error in VBA

This code to is to search each element from column A in worksheet 6 to be existing in Column A in worksheet 3
Sub checkpanvalueS()
Dim lastRow1 As Long
Dim lastRow2 As Long
lastRow1 = Sheet3.Cells(Rows.Count, 1).End(xlUp).Row
lastRow2 = Sheet6.Cells(Rows.Count, 1).End(xlUp).Row
Dim myArr As Variant
'Dim myArr2 As Variant
'For i = 2 To lastRow1
'myArr(i) = Sheet3.Cells(i, 1)
myArr = Sheet3.Range("A2:A" & lastRow1)
'myArr2 = Sheet6.Range("A2:A" & lastRow2)
'Next i
' For i = 2 To lastRow1
For m = 2 To lastRow2
'if UBound(Filter(myArr, Sheet6.Cells(m, 1))) > -1 and then
' MsgBox "All Yellow highlighted pan number (Column A ) should not be one from ptimary Cards ."
' If UBound(Filter(myArr, myArr(i))) >= 0 And myArr(i) <> "" Then
' If IsInArray(Sheet6.Cells(m, 1), myArr) Then
If Filter(myArr, Sheet6.Cells(m, 1)) = "" Then
' MsgBox ("Search Term SUCCESSFULLY located in the Array")
Range("A" & m).Interior.Color = vbYellow
MsgBox (" These pan numbers should'nt be equal to existing primary cards")
End If
Next m
' Next i
End Sub
Try this code - you should use the Find method of the Range object to look for a specific value:
Public Sub HighlightItems()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rngSearch1 As Range
Dim rngSearch2 As Range
Dim rngCell As Range
Dim rngFound As Range
Set ws1 = ThisWorkbook.Worksheets("Sheet6")
Set ws2 = ThisWorkbook.Worksheets("Sheet3")
Set rngSearch1 = ws1.Range("A1:A" & ws1.Cells(Rows.Count, 1).End(xlUp).Row)
Set rngSearch2 = ws2.Range("A1:A" & ws1.Cells(Rows.Count, 1).End(xlUp).Row)
For Each rngCell In rngSearch1
Set rngFound = rngSearch2.Find(rngCell.Value)
If Not rngFound Is Nothing Then
rngCell.Interior.Color = vbYellow
Debug.Print ws1.Name & "!" & rngCell.Address & " equals " & ws2.Name & "!" & rngFound.Address
End If
Next
End Sub

Excel VBA - Check Values in Sheet1 Against Sheet2, then Copy Notes If Matching

I have two sheets. I want to check the value in one column against the value in the same column in the second sheet. If they match, then I want to migrate the string data from the Notes column to the new sheet. (essentially I'm seeing if last week's ticket numbers are still valid this week, and carrying over the notes from last week).
I am trying to do this with the following code (using columns Z for the data, BE for the notes):
Sub Main()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
Dim partNo2 As Range
Dim partNo1 As Range
Dim partNo3 As Range
For Each partNo2 In ws1.Range("Z1:Z" & ws1.Range("Z" & Rows.Count).End(xlUp).Row)
For Each partNo1 In ws2.Range("Z1:Z" & ws2.Range("Z" & Rows.Count).End(xlUp).Row)
For Each partNo3 In ws1.Range("BE1:BE" & ws2.Range("BE" & Rows.Count).End(xlUp).Row)
If StrComp(Trim(partNo2), Trim(partNo1), vbTextCompare) = 0 Then
ws2.Range("BE" & partNo1.Row) = partNo3
End If
Next
Next
Next
'now if no match was found then put NO MATCH in cell
For Each partNo1 In ws2.Range("E1:F" & ws2.Range("A" & Rows.Count).End(xlUp).Row)
If IsEmpty(partNo1) Then partNo1 = ""
Next
End Sub
Untested:
Sub Main()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rng1 As Range, rng2 As Range
Dim c As Range, f As Range
Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
Set rng1 = ws1.Range("Z1:Z" & ws1.Range("Z" & Rows.Count).End(xlUp).Row)
Set rng2 = ws2.Range("Z1:Z" & ws2.Range("Z" & Rows.Count).End(xlUp).Row)
For Each c In rng1.Cells
Set f = rng2.Find(c.Value, , xlValues, xlWhole)
If Not f Is Nothing Then
f.EntireRow.Cells(, "BE").Value = c.EntireRow.Cells(, "BE").Value
End If
Next c
'now if no match was found then put NO MATCH in cell
For Each c In ws2.Range("E1:F" & ws2.Range("A" & Rows.Count).End(xlUp).Row)
If Len(c.Value) = 0 Then c.Value = "NO MATCH"
Next
End Sub
This accomplishes the same result (maybe with the exception of the columns E & F at the bottom with NO MATCH). It's just a different way of going about it. Instead of using ranges, I'm just looking at each cell and comparing it directly.
TESTED:
Sub NoteMatch()
Dim lastRow1 As Long
Dim lastRow2 As Long
Dim tempVal As String
lastRow1 = Sheets("Sheet1").Range("Z" & Rows.Count).End(xlUp).row
lastRow2 = Sheets("Sheet2").Range("Z" & Rows.Count).End(xlUp).row
For sRow = 2 To lastRow1
tempVal = Sheets("Sheet1").Cells(sRow, "Z").Text
For tRow = 2 To lastRow2
If Sheets("Sheet2").Cells(tRow, "Z") = tempVal Then
Sheets("Sheet2").Cells(tRow, "BE") = Sheets("Sheet1").Cells(sRow, "BE")
End If
Next tRow
Next sRow
Dim match As Boolean
'now if no match was found, then put NO MATCH in cell
For lRow = 2 To lastRow2
match = False
tempVal = Sheets("Sheet2").Cells(lRow, "Z").Text
For sRow = 2 To lastRow1
If Sheets("Sheet1").Cells(sRow, "Z") = tempVal Then
match = True
End If
Next sRow
If match = False Then
Sheets("Sheet2").Cells(lRow, "BE") = "NO MATCH"
End If
Next lRow
End Sub

how to get a % value after matching same name from 2 different worksheets

I was wondering if someone can help me solve the following problem. Someone has previously helped me on get the % of dctest/In value on the same worksheet. But right now, i need to do the same thing but on a different worksheet.
Say Sheet1
this is copied Sheet1 (1) after taking the %
Sub marco1()
'start making Sheet1 into %
'~~> Add/Remove the text here which you want to ignore
Excludetext = "In,test1,test2,test3,test4,test5,test6"
MyArray = Split(Excludetext, ",")
Set ws = Sheets("Sheet1")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
'Set Column B into %
For i = 1 To LastRow
boolContinue = True
For j = 0 To UBound(MyArray)
SearchText = UCase(Trim(MyArray(j)))
If UCase(Trim(ws.Range("A" & i).Value)) = SearchText Then
boolContinue = False
Exit For
End If
Next j
If boolContinue = True Then
With Range("B" & i)
.Formula = _
"=OFFSET(INDIRECT(ADDRESS(INDEX(MATCH(A" & i & _
",$A$1:$A$45,0),1,0),1,1,1,'Duplicated_Sheet1')),0,1)/$B$5"
.NumberFormat = "0.00%"
End With
End If
Next i
End sub
There is some error showing at the formula, did i make a mistake with the formula? Thank you in advance!
Is this what you are trying?
TRIED AND TESTED
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim wsData As String
Dim SearchText As String, Excludetext As String
Dim LastRow As Long, i As Long, j As Long
Dim MyArray() As String
Dim boolContinue As Boolean
'~~> Add/Remove the text here
Excludetext = "In,Test1,Test2,Test3,Test4,Test5,Test6"
'~~> Change this to the relevant sheetname which has the data
wsData = "Sheet1"
MyArray = Split(Excludetext, ",")
Set ws = Sheets("Sheet2")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LastRow
boolContinue = True
For j = 0 To UBound(MyArray)
SearchText = MyArray(j)
If ws.Range("A" & i).Value = SearchText Then
boolContinue = False
Exit For
End If
Next j
If boolContinue = True Then
With ws.Range("B" & i)
.Formula = _
"=OFFSET(INDIRECT(ADDRESS(INDEX(MATCH(A" & i & _
"," & wsData & "!$A$1:$A$11,0),1,0),1,1,TRUE,""" & _
wsData & """)),0,1)/" & wsData & "!B1"
.NumberFormat = "0.00%"
End With
End If
Next i
End Sub
When using ADDRESS() for a cell in different sheet, you have to specify additional arguments.
Straight from Excel's help
Syntax of ADDRESS Function
ADDRESS(row_num, column_num, [abs_num], [a1], [sheet_text])
Where [sheet_text] is the name of the sheet which we are referring to. I would recommend reading more about it in Excel Help.
This is the actual formula for say dctest
=OFFSET(INDIRECT(ADDRESS(INDEX(MATCH(A7,Sheet1!$A$1:$A$11,0),1,0),1,1,TRUE,"Sheet1")),0,1)/Sheet1!B1
HTH
Sid