I use the following code to cycle through rows in a spreadsheet, and save unique items into a 2D array. I know the number of unique items, and the arrLen variable holds that number.
If a row with the same prNr (unique number identifying a set of items) as a previous row is found, a check is done to see which has the lower priority. If it has a lower priority, it should replace the item in the 2D array.
My problem is that the prArrCount variable increments past the number of unique prNr entries in my spreadsheet. According to me it should not do this, but can someone help me find out why?
'Cycle through PRs, store values in 2D array
'Create 2D array
Dim prData() As String
ReDim prData(arrLen, 6)
'Find the last row in the spreadsheet to iterate through all entries
Dim lastRow As Integer
lastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
'Create data variables
Dim i, prArrCount As Integer
Dim prNr As String
Dim description As String
Dim Value As Double
Dim srmRFQ As String
Dim requisitionDate As Date
Dim deliveryDate As Date
Dim delivery As Integer
Dim delta As Integer
Dim priority As Integer
Dim newPR As Integer
Dim initFlag As Integer
'Set initial values
initFlag = 1
prArrCount = 0
newPR = 1
'Start for loop to iterate through all entries in the spreadsheet
For i = 2 To lastRow
'Read in the PR line values
prNr = Range("B" & i).Value
description = Range("G" & i).Value
srmRFQ = Range("E" & i).Value
requisitionDate = DateValue(Range("O" & i).Value)
Value = Range("R" & i).Value
If Not Left(Range("P" & i).Value, 1) = "0" Then
deliveryDate = DateValue(Range("P" & i).Value)
Else
deliveryDate = 0
End If
If Range("S" & i).Value = "" Then
delivery = 0
Else
delivery = Range("S" & i).Value
End If
If Range("Z" & i).Value = "Invalid" Then
priority = 9999
delta = 0
Else
priority = Range("Z" & i).Value
delta = Range("Y" & i).Value
End If
'Check if it is the first iteration of the loop
If initFlag = 1 Then
initFlag = 0
ElseIf Not prNr = prData(prArrCount, 0) Then
prArrCount = prArrCount + 1
newPR = 1
End If
'Check if values should be written into 2D PR array
If newPR = 1 Then
prData(prArrCount, 0) = prNr '(0) PR Number
prData(prArrCount, 1) = description '(1) Description
prData(prArrCount, 2) = priority '(2) Days left to order
prData(prArrCount, 3) = deliveryDate '(3) Delivery date
prData(prArrCount, 4) = delivery '(4) Lead time
newPR = 0
ElseIf priority < prData(prArrCount, 2) Then
prData(prArrCount, 0) = prNr '(0) PR Number
prData(prArrCount, 1) = description '(1) Description
prData(prArrCount, 2) = priority '(2) Days left to order
prData(prArrCount, 3) = deliveryDate '(3) Delivery date
prData(prArrCount, 4) = delivery '(4) Lead time
End If
Next i
I like to use scripting dictionaries to manage duplicates. The below creates a scripting dictionary and adds a 5 row 1D array as the value for any new prNr. If the prNr exists, it checks if the priority of the prior version is greater, and if so, stores the new array as the value of that key in the dictionary.
'Cycle through PRs, store values in 2D array
'Create 2D array
Dim prData() As String
ReDim prData(arrLen, 6)
'Find the last row in the spreadsheet to iterate through all entries
Dim lastRow As Integer
lastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
'Create data variables
Dim i as Integer, prArrCount As Integer
Dim prNr As String
Dim description As String
Dim Value As Double
Dim srmRFQ As String
Dim requisitionDate As Date
Dim deliveryDate As Date
Dim delivery As Integer
Dim delta As Integer
Dim priority As Integer
Dim newPR As Integer
Dim initFlag As Integer
Dim dict As New Scripting.Dictionary 'Note you need the Microsoft Scripting Runtime Library
Dim x(4) as Variant
Dim Key as Variant
Dim Anchor as Range
'Set initial values
initFlag = 1
prArrCount = 0
newPR = 1
'Start for loop to iterate through all entries in the spreadsheet
For i = 2 To lastRow
'Read in the PR line values
prNr = Range("B" & i).Value
description = Range("G" & i).Value
srmRFQ = Range("E" & i).Value
requisitionDate = DateValue(Range("O" & i).Value)
Value = Range("R" & i).Value
If Not Left(Range("P" & i).Value, 1) = "0" Then
deliveryDate = DateValue(Range("P" & i).Value)
Else
deliveryDate = 0
End If
If Range("S" & i).Value = "" Then
delivery = 0
Else
delivery = Range("S" & i).Value
End If
If Range("Z" & i).Value = "Invalid" Then
priority = 9999
delta = 0
Else
priority = Range("Z" & i).Value
delta = Range("Y" & i).Value
End If
x(0) = prNr
x(1) = description
x(2) = priority
x(3) = deliveryDate
x(4) = delivery
If Not dict.Exists(prNr) Then
dict.Add prNr, x
Else
If priority < dict(prNr)(2) Then
dict(prNr) = x
End If
End If
Next i
With Workbooks("Workbook Name").Sheets("Sheet Name") 'Change references to match what you need
For Each Key in dict.Keys
Set Anchor = .Range("A" & .Rows.Count).End(xlUp).Offset(1,0)
For i = Lbound(dict(key),1) to Ubound(dict(key),1)
Anchor.Offset(0,i) = dict(key)(i)
Next i
Next key
End With
Please see my edit. This will output each key in a new line, and each element in the array related to the key starting in column A. You just need to update the workbook, worksheet and range to match your needs.
Related
I am currently reading a range into an array to perform a few calculations before outputting into another worksheet. My reason for using the array is speed as I am often dealing with thousands of rows.
I have one particular calculation that I am struggling with for some reason.
This is the part I am struggling with (rest of sample of this code is further down):
For i = non_rev_rows To 2 Step -1.
**' Remove Blank Rows from array
If data_range(i, 2) = "No WBS/CC" Then
If Application.WorksheetFunction.CountA(Range("C" & i & ":M" & i)) = 0 Then
Rows(i).Delete
End If
So basically when a row in column 2 is equal to "No WBS/CC" then I need to run a CountA or any other method you can recommend to calcuate the total value of columns C to M on that row. I am essentially looking for any row that = "No WBS/CC" and where columns C:M have no value. If so, then delete the entire row. If there is a value in columns C:M then I would not wish to delete the row.
'Row Count
With Sheets("array")
non_rev_rows = .Range("E" & .Rows.Count).End(xlUp).Row
End With
' Remove Blank Rows from array
' Replace "NO WBS/CC" with Co Code Over-Ride if supplied
' Set Debit / Credit
' Round to 2 decimal places
Set data = array_sheet.Range("A1:M" & non_rev_rows)
data_range = data.Value
For i = non_rev_rows To 2 Step -1.
**' Remove Blank Rows from array
If data_range(i, 2) = "No WBS/CC" Then
If Application.WorksheetFunction.CountA(Range("C" & i & ":M" & i)) = 0 Then
Rows(i).Delete
End If
' Replace "NO WBS/CC" with Co Code Over-Ride if supplied
If data_range(i, 13) <> 0 Then
data_range(i, 2) = data_range(i, 13)
End If
End If**
' Set Debit / Credit
data_range(i, 3) = Replace(data_range(i, 3), "Debit", 41)
data_range(i, 3) = Replace(data_range(i, 3), "Credit", 51)
' Round to 2 decimal places
data_range(i, 5) = WorksheetFunction.Round(data_range(i, 5), 2)
' If data_range(i, 3) = "Debit" Then
' data_range(i, 3).Value = 41
' ElseIf data_range(i, 3) = "Credit" Then
' data_range(i, 3).Value = 51
' End If
'data_range(i, 5).Value = Application.WorksheetFunction.Round(Range(data_range(i, 5)).Value, 2)
'Range("E" & i).Value = Application.WorksheetFunction.Round(Range("E" & i).Value, 2)
Next i
**' Remove Blank Rows from array
If data_range(i, 2) = "No WBS/CC" Then
If Application.WorksheetFunction.CountA(Range("C" & i & ":M" & i)) = 0 Then
Rows(i).Delete
End If
This code does not result in an error but it also does not have the desired impact. I have several rows in my test data that contain "No WBS/CC" in column 2 and zero values in columns C:M but the code is not deleting those rows.
If you want learning/understanding how an array row can be deleted (adapted for your case), please test the next way. It will return the array without deleted rows starting from "O2" of the same sheet, so the range after M:M column must be empty. You can easily adapt last code line to return wherever you need (in other sheet, other workbook...):
Sub DeleteArrayRows()
Dim array_sheet As Worksheet, non_rev_rows As Long, Data As Range, count2 As Long, data_range, arrRow, i As Long
Set array_sheet = ActiveSheet 'worksheets("array")
non_rev_rows = array_sheet.Range("E" & array_sheet.rows.count).End(xlUp).row
Set Data = array_sheet.Range("A1:M" & non_rev_rows)
data_range = Data.Value
For i = 1 To UBound(data_range)
count2 = 0
If data_range(i, 2) = "No WBS/CC" Then
With Application
arrRow = .Transpose(.Transpose(.Index(data_range, i, 0))) 'extract a slice of the row array
End With
Debug.Print Join(arrRow, ""): Stop 'just to see the joinned respecitve slice In Immediate Window
'comment it after seeing what it represents and press F5
If data_range(i, 1) <> "" Then count2 = Len(data_range(i, 1))
If Len(Join(arrRow, "")) - count2 = Len(data_range(i, 2)) Then
data_range = DeleteArrayRow_(data_range, i): i = i - 1
End If
End If
If i = UBound(data_range) Then Exit For
Next i
'drop the array (without deleted rows) in a range:
array_sheet.Range("O1").Resize(UBound(data_range), UBound(data_range, 2)).Value = data_range
End Sub
Private Function DeleteArrayRow_(arr As Variant, RowToDelete As Long) As Variant 'interesting...
'It does not work to eliminate the first array row...
Dim Rws As Long, cols As String
Rws = UBound(arr) - LBound(arr)
cols = "A:" & Split(Columns(UBound(arr, 2) - LBound(arr, 2) + 1).address(, 0), ":")(0)
DeleteArrayRow_ = Application.Index(arr, Application.Transpose(Split(Join(Application.Transpose(Evaluate("Row(1:" & _
(RowToDelete - 1) & ")"))) & " " & Join(Application.Transpose(Evaluate("Row(" & _
(RowToDelete + 1) & ":" & UBound(arr) & ")"))))), Evaluate("COLUMN(" & cols & ")"))
End Function
It is not extremely fast, I tried showing it only for didactic purpose. To see that it is and how it is possible...
Note: I did not pay attention to all at the code lines after deletion. It can be easily adapted to include that part...
You can do both tests on the array rather than partially in array and partially in the worksheet.
Only delete the row in the worksheet when you find a full match.
Public Sub Test2()
Dim data_range As Variant
Dim lRows As Long
Dim lColumns As Long
Dim lCounter As Long
data_range = Sheet1.Range("A1:M6")
' Add the data to an array
For lRows = UBound(data_range) To LBound(data_range) Step -1
'Step through the array in reverse
If data_range(lRows, 2) = "No WBS/CC" Then
'Check for the "No WBS/CC" value in the second column of the array
lCounter = 0
'Reset the counter
For lColumns = 3 To 13
If Not IsEmpty(data_range(lRows, lColumns)) Then
lCounter = lCounter + 1
End If
Next lColumns
'Check columns in the array row to see if they have data
'Add to the counter for each cell having value
If lCounter = 0 Then
Sheet1.Rows(lRows).EntireRow.Delete
End If
'If the counter is zero delete the current row in the Workbook
End If
Next lRows
End Sub
Sample data before the macro is run. The row we expected to be removed highlighted in green.
Sample data after the macro is run. The expected row has been removed.
An alternate option is to write the valid rows to a new array.
Clear the data on the worksheet, then write the new array to the worksheet.
Remove Rows
Sub DoStuff()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = wb.Worksheets("Array")
Dim LastRow As Long: LastRow = ws.Cells(ws.Rows.Count, "E").End(xlUp).Row
Dim rg As Range: Set rg = ws.Range("A2", ws.Cells(LastRow, "M"))
Dim rCount As Long: rCount = rg.Rows.Count
Dim cCount As Long: cCount = rg.Columns.Count
Dim Data() As Variant: Data = rg.Value
Dim sr As Long
Dim dr As Long
Dim c As Long
For sr = 1 To rCount
If Not IsRowBlank(Data, sr, 3, 13) Then ' is not blank
' Replace "NO WBS/CC" with Co Code Over-Ride if supplied
If CStr(Data(sr, 1)) = "No WBS/CC" Then
If Data(sr, 13) <> 0 Then
Data(sr, 2) = Data(sr, 13)
End If
End If
' Set Debit / Credit
Data(sr, 3) = Replace(Data(sr, 3), "Debit", 41)
Data(sr, 3) = Replace(Data(sr, 3), "Credit", 51)
' Round to 2 decimal places
Data(sr, 5) = Application.Round(Data(sr, 5), 2)
' Copy source row to destination row.
dr = dr + 1
For c = 1 To cCount
Data(dr, c) = Data(sr, c)
Next c
'Else ' is blank; do nothing
End If
Next sr
' Clear bottom source data.
If dr < rCount Then
For sr = dr + 1 To rCount
For c = 1 To cCount
Data(sr, c) = Empty
Next c
Next sr
End If
rg.Value = dData
End Sub
Function IsRowBlank( _
Data() As Variant, _
ByVal DataRow As Long, _
ByVal StartColumn As Long, _
ByVal EndColumn As Long) _
As Boolean
Dim c As Long
For c = StartColumn To EndColumn
If Len(CStr(Data(DataRow, c))) > 0 Then Exit For
Next c
IsRowBlank = c > EndColumn
End Function
I have a code that matches a cell value in Column C on Sheet1 to a pivot table on Sheet3 and then copies certain columns over.
Code will check how many entries there are on Sheet1 that need to be checked
Loop 2: For every value in Column C/Sheet1 with a match in Column A on Sheet 2 it will then copy over the corresponding data from Column B,C,D,E.
Since there are multiple matches possible by value/Sheet I am limiting the data pull to three matches (three loops in the code). To achieve that I am increasing i +1 or i+2 to get the next row in the pivot table.
The table on Sheet 2 is sometimes 10,000+ rows and excel crashes.
Does anyone have an idea how to speed up the loop codes (Loop2,3,4 are the same) to make it less work intensive e.g. array possibly? They are causing the lock up since I think the code keeps running up and down column A.
Set sheet3 = Sheets("OrbitPivotTable")
CellChanged = Sheet1.Range("A1").Value + 1
LastRow = sheet3.Cells(Rows.Count, "A").End(xlUp).Row
LastData = Sheet1.Cells(Rows.Count, "C").End(xlUp).Row
'Loop1
For i = 1 To LastRow
If Sheet1.Range("C" & CellChanged).Value = "" Then GoTo Nextstep2
If Sheet1.Range("C" & CellChanged).Value = sheet3.Range("A" & i) Then
Sheet1.Range("H" & CellChanged).Value = sheet3.Range("B" & i).Value 'Customer
Sheet1.Range("I" & CellChanged).Value = sheet3.Range("C" & i).Value 'Rate Val start
Sheet1.Range("J" & CellChanged).Value = sheet3.Range("D" & i).Value 'ATA All in
Sheet1.Range("K" & CellChanged).Value = sheet3.Range("E" & i).Value 'Special Remarks
Found = True
End If
If Found = True Or i = LastRow Then
If CellChanged = LastData Then
Exit For
End If
If Found = True Then
Found = False
Nextstep2:
CellChanged = CellChanged + 1
End If
i = 0
End If
Next i
'Loop2
etc....
Excel File
I might have misunderstood the process in the file you shared, but this should be faster (and much less code overall).
I put the pivot table lookup in a loop, switched to Match(), and reduced the number of read/writes using arrays where possible.
EDITED to fix an embarrassing bug where I forgot to adjust the Match() result m to account for the starting row of the range I run match() against...
Sub HB_IPT_Rate_Check()
Dim wsReport As Worksheet, wsCPK As Worksheet, wsOrbitPivot As Worksheet
Dim c As Range, rwReport As Range, lastPivotRow As Long
Dim ata, m, numMatches As Long, matchFrom As Long, matchRow As Long
Set wsReport = ThisWorkbook.Worksheets("Comparison Report")
Set wsCPK = ThisWorkbook.Worksheets("CPK")
Set wsOrbitPivot = ThisWorkbook.Worksheets("OrbitPivotTable")
'loop over the rows in the report sheet
For Each c In wsReport.Range("C3", wsReport.Cells(Rows.Count, "C").End(xlUp)).Cells
ata = c.Value 'read this once....
Set rwReport = c.EntireRow
'1st Database Match "CPK"
m = Application.Match(ata, wsCPK.Columns("A"), 0)
If Not IsError(m) Then
With wsCPK.Rows(m)
rwReport.Columns("D").Resize(1, 4).Value = _
Array(.Columns("B").Value, .Columns("C").Value, _
.Columns("F").Value, .Columns("H").Value)
'Sum of HB CWGT (KG),Sum of MB CWGT (KG),Achiev CPK,Density
End With
Else
'no match...
End If
'2nd Database Match "Orbit"
lastPivotRow = wsOrbitPivot.Cells(Rows.Count, "A").End(xlUp).Row
numMatches = 0 'reset match count
matchFrom = 2
m = Application.Match(ata, wsOrbitPivot.Range("A" & matchFrom & ":A" & lastPivotRow), 0)
'keep going while we still have a match and we've not reached the max result count
Do While Not IsError(m) And numMatches < 3
numMatches = numMatches + 1
matchRow = matchFrom + (m - 1) 'adjust the matched row index according to where we started looking...
'sanity check
Debug.Print "Matched " & ata & " on row " & matchRow
rwReport.Columns("H").Offset(0, (numMatches - 1) * 4).Resize(1, 4).Value = _
wsOrbitPivot.Cells(matchRow, "B").Resize(1, 4).Value
'find the next match if any, starting below the last match
matchFrom = matchRow + 1
m = Application.Match(ata, wsOrbitPivot.Range("A" & matchFrom & ":A" & lastPivotRow), 0)
Loop
Next c 'next report row
End Sub
Use Dictionary to set row and column number.
Data is assigned to fit rows and columns in a virtual array.
Sub test()
Dim Ws(1 To 4) As Worksheet
Dim DicR As Object ' Dictionary
Dim DicC As Object ' Dictionary
Dim vDB, arr()
Dim s As String
Dim i As Long, n As Long, j As Integer
Dim r As Long, c As Integer
Set Ws(1) = Sheets("Comparison Report")
Set Ws(2) = Sheets("CPK")
Set Ws(3) = Sheets("OrbitPivotTable")
Set Ws(4) = Sheets("Orbit")
'Row index dictionary
Set DicR = CreateObject("Scripting.Dictionary") 'New Scripting.Dictionary
'Column index dictionary
Set DicC = CreateObject("Scripting.Dictionary") ' New Scripting.Dictionary
vDB = Ws(1).UsedRange
For i = 3 To UBound(vDB, 1)
s = vDB(i, 3)
If s <> "" Then
If DicR.Exists(s) Then
'DicC(s) = DicC(s) + 1
Else
n = n + 1
DicR.Add s, n 'row index
DicC.Add s, 0 'column index
End If
End If
Next i
'Create an array of virtual tables based on the number of dictionaries.
'Since the number of columns cannot be predicted, a specific number of 1000 was entered.
'in my test, number 100 is too small
ReDim arr(1 To DicR.Count, 1 To 1000)
For j = 2 To 4
vDB = Ws(j).Range("a1").CurrentRegion
For i = 2 To UBound(vDB, 1)
s = vDB(i, 1)
If DicR.Exists(s) Then
r = DicR(s)
c = DicC(s) * 4 + 1
DicC(s) = DicC(s) + 1
arr(r, c) = vDB(i, 2)
arr(r, c + 1) = vDB(i, 3)
arr(r, c + 2) = vDB(i, 4)
arr(r, c + 3) = vDB(i, 5)
End If
Next i
Next j
With Ws(1)
.Range("d3").Resize(UBound(arr, 1), UBound(arr, 2)) = arr
End With
End Sub
Result image
Please look at my sample data and code to understand what I'm trying to do.
I need to use the value of Cells(, 3) to define a range to populate a Trialnumber(18) array. I need the array to iterate through a For loop, to count filled cells in column H for each trial and print the count to column T in the last row of each trial. I will also need the array for further data analysis in future(Unless someone can come up with a better solution).
At the moment I am experimenting with 3 modules of code, trying to get the desired solution.
Module 2 is the only one with no errors, and prints the value in the right cell, but it is printing the total filled cell count (562), rather than per trial (expected value = 1 or 2).
Module 1 is as follows:
Sub dotcountanalysis()
Dim startpoint As Long
startpoint = 1
Dim lastrow As Long
lastrow = Cells(Rows.Count, 3).End(xlUp).Row
Dim i As Long
With Worksheets("full test")
For i = 1 To 18
For n = startpoint To lastrow + 1
If Cells(n, 3).Value <> "Trial, " & CStr(i) Then
Dim nMinusOne As Long
nMinusOne = n - 1
Dim trialCount As Long
'Set Trialnumber(i-1) = Range(cells(startpoint, 3), cells(n-1, 3))
trialCount = Application.WorksheetFunction.CountA(Range("H" & CStr(startpoint) & ":" & "H" & CStr(nMinusOne)))
Range("T" & CStr(startpoint) & ":" & "T" & CStr(nMinusOne)).Value = trialCount
startpoint = n
Exit For
End If
Next n
Next i
End With
End Sub
It returns a "method _range of object _global falied" error on line: trialCount = Application.WorksheetFunction.CountA(Range("H" & CStr(startpoint) & ":" & "H" & CStr(nMinusOne)))
Module 3 is as follows:
Sub dotcountanalysis3()
Dim pressedCount As Long
Dim myCell As Range
Dim pressedRange As Range
'create trials array
Dim t(18) As Range
'set range for trialnumber (t)
Dim startpoint As Long
startpoint = 1
Dim lastrow As Long
lastrow = Cells(Rows.Count, 3).End(xlUp).Row
For i = 1 To 18
For n = startpoint To lastrow
startpoint = 7
If Cells(n, 3).Value <> "Trial, " & CStr(i) Then
Set t(i - 1) = Range(Cells(startpoint, 3), Cells(n, 3))
n = n + 1
startpoint = n
Exit For
End If
Next n
Next i
'count presses in each trial
With Worksheets("full test")
For i = 0 To 17
pressedCount = Application.WorksheetFunction.CountA _
(.Range(.Cells(t(), "H"), .Cells(.Rows.Count, "H")))
If pressedCount = 0 Then Exit Sub
'make sure there are cells or else the next line will fail
Set pressedRange = .Columns("H").SpecialCells(xlCellTypeConstants)
For Each myCell In pressedRange.Cells
'only loop through the cells containing something
.Cells(myCell.Row, "T").Value = pressedCount
Next myCell
Next i
End With
End Sub
It returns a run-time "type mismatch" error on line: pressedCount = Application.WorksheetFunction.CountA _
(.Range(.Cells(t(), "H"), .Cells(.Rows.Count, "H")))
Edit: I have updated code in mod 3 and updated error.
When counting things I like to use a dictionary object, and arrays are faster than going row by row on the sheet.
This will count unique combinations of Block+Trial: to count only by trial you would just use k = d(r, COL_TRIAL)
Dim dBT As Object 'global dictionary
Sub dotcountanalysis()
'constants for column positions
Const COL_BLOCK As Long = 1
Const COL_TRIAL As Long = 2
Const COL_ACT As Long = 7
Dim rng As Range, lastrow As Long, sht As Worksheet
Dim d, r As Long, k, resBT()
Set sht = Worksheets("full test")
lastrow = Cells(Rows.Count, 3).End(xlUp).Row
Set dBT = CreateObject("scripting.dictionary")
Set rng = sht.Range("B7:H" & lastrow)
d = rng.Value 'get the data into an array
ReDim resBT(1 To UBound(d), 1 To 1) 'resize the array which will
' be placed in ColT
'get unique combinations of Block and Trial and counts for each
For r = 1 To UBound(d, 1)
k = d(r, COL_BLOCK) & "|" & d(r, COL_TRIAL) 'create key
dBT(k) = dBT(k) + IIf(d(r, COL_ACT) <> "", 1, 0)
Next r
'populate array with appropriate counts for each row
For r = 1 To UBound(d, 1)
k = d(r, 1) & "|" & d(r, 2) 'create key
resBT(r, 1) = dBT(k) 'get the count
Next r
'place array to sheet
sht.Range("T7").Resize(UBound(resBT, 1), 1) = resBT
'show the counts in the Immediate pane (for debugging)
For Each k In dBT
Debug.Print k, dBT(k)
Next k
End Sub
After a lot of googling and trying I am asking you for help regarding the following Problem.
Worksheet 1 (Database) has the ID's and a lot of Information in Column D
Worksheet 2 (Skills) has the ID's in Row 1 and all respective skills in the rows below the column for each ID.
Worksheet 3 (Output) is needed to populate Listboxes and Graphs and can be considered empty
For Illustration purposes: http://imgur.com/a/Nt88C
Via comboboxes, the skill the user is looking for is selected. This Skill then needs to be matched against the skills of each ID on Worksheet 2.
If a match is found, the respective ID shall be found on Worksheet 1 and certain Information from there copied to Worksheet 3.
My take on this has been to find each ID in Worksheet 1, match it with all ID's on Worksheet 2 and look through the respective rows for a match. However, every more efficient way is welcome.
Here my code:
The comboboxes
Dim wb As Workbook
Set wb = ActiveWorkbook
Dim ws1, ws2, ws3, ws4 As Worksheet
Set ws1 = wb.Worksheets("Meta DB")
Set ws2 = wb.Worksheets("Criteria")
Set ws3 = wb.Worksheets("Supplier Criteria TreeView")
Set ws4 = wb.Worksheets("Supplier Skills")
'1. - - get all Suppliers for the selected Input
'Redefine for clarity
Dim strFind As String
'1.0. - - Clear previously used ranges
ws3.Range("A2:L28").Clear
ws3.Range("A30:L100").Clear
ws3.Range("V2:V20").Clear
'1.1. - - Get value to search for and range to go through (depending on combobox selections)
If Me.comboProduct.ListIndex = -1 And Me.comboSubGroup.ListIndex = -1 And Me.comboCG.ListIndex <> -1 Then
strFind = Me.comboCG.value
ElseIf Me.comboProduct.ListIndex = -1 And Me.comboSubGroup.ListIndex <> -1 And Me.comboCG.ListIndex <> -1 Then
strFind = Me.comboSubGroup.value
ElseIf Me.comboProduct.ListIndex <> -1 And Me.comboSubGroup.ListIndex <> -1 And Me.comboCG.ListIndex <> -1 Then
strFind = Me.comboProduct.value
End If
Try 1:
Dim rng1, rng2 As Range
Dim lRow, j, k As Long
Dim IDrow As String
'Paste starting at row 2 or 30 in ws3, respectively (Active / Inactive)
j = 2
k = 30
For Each rng1 In ws1.Range("D4:D500")
If Rng <> "" Then
For Each rng2 In ws4.Range("A1:ZZ1")
If rng2 <> "" Then
If rng1.value = rng2.value Then
For lRow = 2 To ws4.UsedRange.Rows.Count
IDrow = ws4.Cells(lRow, rng2).value
If InStr(1, IDrow, strFind, vbTextCompare) > 0 Then
'Check for active Supplier in current Database-row
If ws1.Range("E" & rng1) = "Yes" Then
'Copy row of Database to row j of ws3 then increment j
ws1.Range("B" & rng1 & ":" & "E" & rng1).Copy Destination:=ws3.Range("B" & j & ":" & "E" & j) 'Copy Name, Potential Supplier, ID, Active
j = j + 1
'ElseIf inactive Supplier, post further down from 30 onwards. Second listbox populates from there
ElseIf ws1.Range("E" & rng1) = "No" Then
ws1.Range("B" & rng1 & ":" & "E" & rng1).Copy Destination:=ws3.Range("B" & k & ":" & "E" & k) 'Copy Name, Potential Supplier, ID, Active
k = k + 1
Else
Exit Sub
End If
End If
Next lRow
End If
End If
Next rng2
End If
Next rng1
Try 2:
Dim IDAr, MyAr, TempAr As Variant
Dim lRow, lastRow, entryrow, LCol, e As Long
Dim ColumnLetter As String
entryrow = ws3.Range("B" & Rows.Count).End(xlUp).row + 1
ws1LRow = ws1.Range("D" & ws1.Rows.Count).End(xlUp).row
IDAr = ws1.Range("D4:D" & lRow).value
Set f = ws4.Range("A1:ZZ1").Find(What:=IDAr, LookIn:=xlValues, Lookat:=xlWhole, MatchCase:=False)
ColumnLetter = Split(f.Address, "$")(1)
lastRow = ws4.Range(ColumnLetter & "2:" & ColumnLetter & "50").End(xlUp).row
MyAr = ws4.Range(ColumnLetter & "1:" & ColumnLetter & lastRow).value
With ws3
'If IsArray(MyAr) Then
For i = LBound(MyAr) To UBound(MyAr)
TempAr = Split(MyAr(i, 1), "\")
For e = 0 To 2
TempAr(e) = ValueToCompare
If InStr(1, ValueToCompare, strFind, vbTextCompare) > 0 Then
ws3.Range("B" & entryrow).value = "Test if it works"
'.
'.
'.
End If
Next e
Next i
Thank you all in advance for any tips!
Sub CodeForLazyPoster()
Dim rIDs As Excel.Range
Dim ID As Excel.Range
Dim lFoundRow As Long
Set rIDs = Worksheets("Sheet1").Range("a1:a10")
For Each ID In rIDs
lFoundRow = FindRow(ID)
If lFoundRow > 0 Then
If FindSkill("Maths", Worksheets("Sheet2").Range("B" & lFoundRow)) Then
' Copy here
End If
End If
Next ID
End Sub
Function FindRow(strFind) As Long
FindRow = 0
On Error Resume Next
FindRow = Application.WorksheetFunction.Match( _
strFind, Worksheets("Sheet2").Range("a1:a10"), False)
End Function
Function FindSkill(strSkill As String, rngLookIn As Excel.Range) As Boolean
Dim tmp As Integer
FindSkill = False
On Error GoTo eHandle
tmp = Application.WorksheetFunction.Match(strSkill, rngLookIn, False)
FindSkill = True
Exit Function
eHandle:
End Function
I'm trying to write a macro that will reconcile two trade reports by putting a yellow fill on trades listed on Sheet1 that are missing from Sheet2 and vice versa. I am a beginner to VBA and have been learning as I go along. Basically the approach that I have taken has been to iterate through each row on Sheet1, creating an array for each of the main fields being compared (TradeDate, Ticker, and Quantity). These arrays contain the row numbers of every match with that field in Sheet1 that is found in Sheet2. Once the arrays are created, I would like to compare the arrays and check if the same row number is contained in each. If so, the program should move on to the next trade. If not, that row should be marked with a yellow fill on Sheet1. I keep getting run-time errors for type mismatch, any input as to why?
Sub Reconciliation()
Sheets("Sheet1").Select
Dim LastRow As Long
Dim LastRow2 As Long
Dim rowCounter As Long
Dim isZero As Long
LastRow = ActiveSheet.UsedRange.Rows.Count
With Sheets("Sheet2").Select
LastRow2 = ActiveSheet.UsedRange.Rows.Count
End With
isZero = 1
'Loops through every row on Sheet 1
For rowCounter = 2 To LastRow
Dim DateValue As String
DateValue = ActiveSheet.Cells(8, rowCounter)
'Search Sheet2 for TradeDate and add matched rows to DateArray
Dim DateArray() As Long
ReDim DateArray(0 To LastRow2)
Sheets("Sheet2").Select
If Application.WorksheetFunction.CountIf(Range("E2:E" & LastRow2), DateValue) > 0 Then
isZero = isZero * 0
Else
DateArray(0) = Application.Match(DateValue, Range("E2:E" & LastRow2), 0)
Dim i As Integer
Dim x As Integer
x = 1
For i = 1 To LastRow2
If Application.Match(DateValue, Range("E" & DateArray(x - 1) & ":E" & LastRow2), 0) = "IsError" Then
Exit For
Else
DateArray(x) = Application.Match(DateValue, Range("E" & DateArray(x - 1) & ":E" & LastRow2), 0)
x = x + 1
End If
Next i
End If
Dim tickerValue As String
tickerValue = ActiveSheet.Cells(4, rowCounter)
Dim TickerArray() As Long
ReDim TickerArray(0 To LastRow2)
Sheets("Sheet2").Select
If Application.Match(tickerValue, Range("D2:D" & LastRow2), 0) = "IsError" Then
isZero = isZero * 0
Else
TickerArray(0) = Application.Match(tickerValue, Range("D2:D" & LastRow2), 0)
Dim i1 As Integer
Dim x1 As Integer
x = 2
For i1 = 1 To LastRow2
If Application.Match(tickerValue, Range("D" & TickerArray(x1 - 1) & ":D" & LastRow2), 0) = "IsError" Then
Exit For
Else
ReDim Preserve TickerArray(0 To x1)
TickerArray(x1) = Application.Match(tickerValue, Range("D" & TickerArray(x1 - 1) & ":D" & LastRow2), 0)
x1 = x1 + 1
End If
Next i1
End If
Dim quantityValue As Long
quantityValue = ActiveSheet.Cells(3, rowCounter)
Dim QuantityArray() As Long
ReDim QuantityArray(0 To LastRow2)
Sheets("Sheet2").Select
If Application.Match(quantityValue, Range("E2:E" & LastRow2), 0) = "N/A" Then
isZero = isZero * 0
Else
QuantityArray(0) = Application.Match(quantityValue, Range("E2:E" & LastRow2), 0)
Dim i2 As Integer
Dim x2 As Integer
x2 = 2
For i2 = 1 To LastRow2
If Application.Match(quantityValue, Range("E" & QuantityArray(x2 - 1) & ":E" & LastRow2), 0) = "IsError" Then
Exit For
Else
ReDim Preserve QuantityArray(0 To x2)
QuantityArray(x2) = Application.Match(quantityValue, Range("E" & QuantityArray(x2 - 1) & ":E" & LastRow2), 0)
x2 = x2 + 1
End If
Next i2
End If
Next rowCounter
End Sub
So there's quite a few problems with your code. Some of it is style, some of it is syntax.
First every place that you have a:
Application.Match(...)
It needs to have WorksheetFunction in between Application and Match so it reads:
Application.WorksheetFunction.Match(...)
Second try to avoid dimensioning variables inside your loops. There's no need for it, and if you want to reset your variables after each loop then add something like a var = 0 at the end.
Move DateValue and DateArray out of the For Loop. Dim DateArray once after LastRow(2) since that's the only time you do and LastRow(2) never changes.
The same goes for TickerArray, tickerValue, QuantityArray, and quantityValue.
Third avoid variables with numbers in them, i.e. LastRow2. This is when you need to use either a more descriptive variable name or an Array. Instead of LastRow2 use
Dim LastRow(1 to 2) as Long
LastRow(1) = Sheet1.UsedRange.Rows.count
LastRow(2) = Sheet2.UsedRange.Rows.count
Fourth why are you using
isZero = isZero*0
instead of
isZero = 0
Fifth, don't use the select() method so much. Its slow and clunky compared to just referencing the sheet you want directly
i.e. instead of doing this
LastRow = ActiveSheet.UsedRange.Rows.count
With Sheets("Sheet2").Select
LastRow2 = ActiveSheet.UsedRange.Rows.count
End With
do this instead
LastRow(1) = Sheet1.UsedRange.Rows.count
LastRow(2) = Sheet2.UsedRange.Rows.count
Sixth you don't need three separate x and i variables, you can dim them once outside of the highest For loop and reuse them for your lower For loops in the If-Else statements.