I have macro that is attempting to check a column to see if there are any blank cells. In any blank cells I need to place an array formula.
The array formula functions fine on sheet and it is:
=INDEX($CJ$1:$CJ$100,MATCH(1,INDEX(($CL$1:$CL$100=CL2)*($CJ$1:$CJ$100<>""),0),0))
In the macro I cannot use fixed ranges so CJ100 and CL100 Need to be CL "LastRow" and CJ "LastRow" as the data ranges are dynamic.
Thus far I have this code:
Dim ws as worksheet
Dim LastRow as long
Dim i As Long
set ws = worksheets("Sheet1")
With ws
LastRow = ws.Range("B" & rows.Count).End(xlUp).Row
For i = 1 To LastRow
If ws.Range("CJ" & i).Value = "" Then
Range("CJ" & i).FormulaArray = "=index(CJ1:CJ ""& LastRow"",MATCH(1,INDEX((CL1:CL ""& LastRow"" = CL ""&i"")*(CJ1:CJ ""& LastRow <>""""),0),0))"
End If
Next i
End With
I am getting an error on this line: Range("CJ" & i).FormulaArray = "=index(CJ1:CJ ""& LastRow"",MATCH(1,INDEX((CL1:CL ""& LastRow"" = CL ""&i"")*(CJ1:CJ ""& LastRow <>""""),0),0))"
The error is the dreaded "Unable to set the FormulaArray property of the Range class".
After doing some research, it appeared that because I am applying to formula to one cell at a time, perhaps I do not need to use .FormulaArray I change the problem line to use .formula = "-=.... but this resulted in a "Application defined or object defined error" message.
Thinking the issue may be with my use of range I tried the following variation:
Dim LastRow as long
Dim ws as worksheet
set ws = worksheets("Sheet1")
LastRow = ws.Range("B" & rows.Count).End(xlUp).Row
Set rng = ws.Range("CJ2:CJ" & LastRow)
For Each cell In rng
'test value in column CJ
If cell.Value = "" Then
'inserts formula
cell.FormulaArray = "=index(CJ1:CJ ""& LastRow"",MATCH(1,INDEX((CL1:CL ""& LastRow"" = CL2)*(CJ1:CJ ""& LastRow <>""""),0),0))"
'cell.arrayformula
End If
Next
End With
This results in the same Formula Array error as my code at the top of the post.
I don't believe the formula is over 250 characters unless I am counting characters incorrectly? Perhaps there is a syntax error somewhere?
Your implementation of Array formula is incorrect and you have also not qualified the range reference correctly as preceding dot is missing.
Range("CJ" & i).FormulaArray = "=index(CJ1:CJ ""& LastRow"",MATCH(1,INDEX((CL1:CL ""& LastRow"" = CL ""&i"")*(CJ1:CJ ""& LastRow <>""""),0),0))"
Here's updated code which should work for you:
Sub ApplyArrayFormula()
Dim ws As Worksheet
Dim LastRow As Long
Dim i As Long
Set ws = Worksheets("Sheet1")
With ws
LastRow = .Range("B" & Rows.Count).End(xlUp).Row
For i = 1 To LastRow
If .Range("CJ" & i).Value = "" Then
.Range("CJ" & i).FormulaArray = "=INDEX($CJ$1:$CJ$" & LastRow & ",MATCH(1,INDEX(($CL$1:$CL$" & LastRow & "=CL" & i & ")*($CJ$1:$CJ$" & LastRow & "<>""""),0),0))"
End If
Next i
End With
End Sub
Related
I get the 'Object Required' error when trying to parse a value from a list into an array. The weird thing is, is that I do it once and it works, but when I try and parse the second value with a different offset it gives me the 'Object Required' error.
Sub Run_Click()
Dim ArrVal() As Variant
Dim DateRange As Range
Dim ComValue() As Variant
Dim LastRow As Long
Dim i As Long
Dim numRow As Variant
Dim sh2 As Worksheet
Dim ConvertVal As String
Dim check As Variant
Dim DutyTest() As Variant
Set sh2 = Sheets(2)
With Sheets("Sheet1")
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row - 7
ReDim Preserve ComValue(1 To LastRow)
ReDim Preserve ArrVal(1 To LastRow)
ReDim Preserve DutyTest(1 To LastRow)
Set DateRange = .Range("A" & 8 & ":A" & 7 + LastRow)
i = 1
Do While i <= LastRow
If i > LastRow Then GoTo ErrorHandler
For Each RowCount In DateRange
'On Error GoTo ErrorHandler
ComValue(i) = .Range("A" & i + 7).Value
ConvertVal = CStr(ComValue(i))
numRow = Application.Match(ConvertVal, sh2.Range("A1:A990000"), 0)
ArrVal(i) = sh2.Range("A" & numRow).Offset(0, 2).Value
DutyTest(i) = sht2.Range("A" & numRow).Offset(1, 1).Value
If i = LastRow Then
Range("B8").Resize(UBound(ArrVal), 1).Formula = Application.Transpose(ArrVal)
End If
i = i + 1
Next RowCount
Loop
ErrorHandler:
End With
End Sub
The error is pulled on the DutyTest(i) line below the ComValue(i) line which gets returned fine. The only difference I can think of is that ComValue(i) is a percent that is returned, in the offset field for DutyTest(i) it should return a string instead, could that be causing the issue?
You have written sht2 as the sheet reference instead of sh2. This is the object reference error here, I think.
I believe it is good practice in VBA to enforce variable declaration with Option Explicit:
https://riptutorial.com/excel-vba/example/3554/always-use--option-explicit-
https://learn.microsoft.com/en-us/office/vba/Language/Reference/User-Interface-Help/option-explicit-statement
Hello there I am very new to VBA coding and coding in general so I hope you can come up with a quick answer to my problem.
I am trying to get a XLookup-Formula into my vba-Code. The code is referencing to another Sheet ("Chart Plan" and is supposed to take the values in column "D" and "E" (starting from row 2) as fixed arrays down to the last row for the "Lookup array" and "return array". I want this to be variable as the "Chart Plan" is updated with different row numbers according to what I am working on. The formula then is supposed to return the values into the active worksheet (Column "J") and go through all the rows ("B" given as RC[-8] = Lookup value).
The problem, I guess, is that I don't really know how the syntax is for giving the arrays into the formula or is it something else entirly? Mixing between RC-Annotation and A1-Annotation maybe?
Thank you.
Dim aEndKP As Variant
Dim aStartKP As Variant
Dim aCN As Variant
Sub ChartPlanScript()
Dim row As Long
Dim last_row As Long
Dim rng As Range
Dim ws As Worksheet
'Array End KP
LReKP = Sheets("Chart Plan").Cells(Rows.Count, "D").End(xlUp).row
aEndKP = Sheets("Chart Plan").Range("D2:D" & LReKP)
'Array Start KP
LRsKP = Sheets("Chart Plan").Cells(Rows.Count, "E").End(xlUp).row
aStartKP = Sheets("Chart Plan").Range("C2:C" & LRsKP)
'Array Chart Plan
LRCN = Sheets("Chart Plan").Cells(Rows.Count, "E").End(xlUp).row
aCN = Sheets("Chart Plan").Range("E2:E" & LRCN)
Set ws = Sheets(1)
ws.Activate
last_row = ws.Range("A5000").End(xlUp).row
For row = 2 To last_row
If Range("A" & row).Value > 0 Then
ws.Range("J" & row).Value = "=XLOOKUP(RC[-8],[aEndKP],[aCN],,1,1)"
Else
ws.Range("J" & row).Value = ""
End If
Next row
End Sub
I don't have XLOOKUP on my version of Excel but this would be using VLOOKUP
Option Explicit
Sub ChartPlanScript()
Dim ws As Worksheet
Dim i As Long, last_row As Long
Dim sCN As String
'Chart Plan
With Sheets("Chart Plan")
last_row = .Cells(Rows.Count, "D").End(xlUp).row
sCN = "'Chart Plan'!" & .Range("D2:E" & last_row).Address
End With
Set ws = Sheets(1)
ws.Activate
last_row = ws.Range("A5000").End(xlUp).row
For i = 2 To last_row
If ws.Cells(i, "A") > 0 Then
ws.Range("J" & i).Formula = "=VLOOKUP(B" & i & "," & sCN & ",2,0)"
Else
ws.Cells(i, "J") = ""
End If
Next
MsgBox i - 1 & " rows processed"
End Sub
In worksheet wsb,I am trying to copy column B and Column having ParName in header and pasting it to columns B & H respectively of worksheet wso. The problem is It's running only for first Item and also for the first matched value of i for that item and not for all the matched item-i values.
Dim ws, wsa, wsb, wsc, wso As Worksheet
Dim index1b, LastRow, MOLastRow, wsoLastRow As Long
Dim ColLtr1b As Variant
Dim MoNameArr
Set wsb = Workbooks(Y).Sheets("REF")
wsb.Activate
LastRow = GetLastRow(wsb, 2)
Arr = Array("Abc", "Def")
Set wso = Workbooks(W).Sheets("Output")
For Each Item In Arr
For i = 2 To LastRow
If Cells(i, 2).Value = Item Then
wsb.Activate
ParName = wsb.Cells(i, 3).Value
Set wsc = Workbooks(M).Sheets(Item)
wsc.Activate
index1b = Application.Match(ParName, wsc.Rows(1), 0)
If Not IsError(index1b) Then
ColLtr1b = Replace(wsc.Cells(1, index1b).Address(True, False), "$1", "")
MOLastRow = wsc.Cells(Rows.Count, 2).End(xlUp).Row
Range("B2:B" & GetLastRow(wsc, 2)).Copy
wso.Activate
wsoLastRow = GetLastRow(wso, 2)
Range("B" & wsoLastRow + 1).Select
ActiveSheet.Paste
wsc.Activate
Range(ColLtr1b & "2:" & ColLtr1b & GetLastRow(wsc, 2)).Copy
wso.Activate
Range("H" & wsoLastRow + 1).Select
ActiveSheet.Paste
End If
End If
Next i
Next Item
Declare your variables like this:
Dim ws As Worksheet, wsa As worksheet, wsb as Worksheet
Dim wsc as Worksheet, wso As Worksheet
Dim index1b as Long, LastRow as Long, MOLastRow as Long, wsoLastRow As Long
Then start debugging with pressing F8. It goes line by line and you may see where is the problem in the nested loop. It can be in one of these 3:
you need to write Trim(Cells(i, 2)) in the If Cells(i, 2).Value = Item Then condition;
you are not calculating LastRow correctly;
you have On Error Resume Next somewhere in your code and you are entering an error w/o noticing;
I want to optimize the following code, as it is very slow.
I am using the code found in this answer:
https://stackoverflow.com/a/27108055/1042624
However, it is very slow when looping through +10k rows. Is it possible to optimize my code below? I have tried to modify it a bit, but it does not seem to work.
Sub DeleteCopy2()
Dim LastRow As Long
Dim CurRow As Long
Dim DestLast As Long
Dim strSheetName As String
Dim arrVal() As Long
Application.ScreenUpdating = False
Application.Calculation = xlManual
strSheetName = "Week " & ISOWeekNum(Format(Date)) - 1
LastRow = Sheets("MatchData").Range("A" & Rows.Count).End(xlUp).Row
DestLast = Sheets(strSheetName).Range("A" & Rows.Count).End(xlUp).Row
ReDim arrVal(2 To LastRow) ' Headers in row 1
For CurRow = LBound(arrVal) To UBound(arrVal)
If Not Sheets(strSheetName).Range("A2:A" & DestLast).Find(Sheets("MatchData").Range("A" & CurRow).Value, LookIn:=xlValues, LookAt:=xlWhole) Is Nothing Then
Sheets("MatchData").Range("A" & CurRow).Value = ""
Else
End If
Next CurRow
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
Can you try this for me? I have commented the code so that you will not have a problem understanding it. Also check how much time it takes for 10k+ rows
Logic
Store search values in array 1
Store destination values in array 2
Loop through the first array and check if it is present in the second array. If present, clear it
Clear the search values from sheet1
Output the array to the sheet1
Sort Col A so that the blanks go down.
Code
Sub Sample()
Dim wbMatch As Worksheet, wbDestSheet As Worksheet
Dim lRow As Long, i As Long
Dim MArr As Variant, DArr As Variant
Dim strSheetName As String
Dim rng As Range
strSheetName = "Sheet2" '"Week " & IsoWeekNum(Format(Date)) - 1
'~~> Set your worksheets
Set wbMatch = Sheets("MatchData")
Set wbDestSheet = Sheets(strSheetName)
'~~> Store search values in 1st array
With wbMatch
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
Set rng = .Range("A2:A" & lRow)
MArr = rng.Value
End With
'~~> Store destination values in the 2nd array
With wbDestSheet
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
DArr = .Range("A2:A" & lRow).Value
End With
'~~> Check if the values are in the other array
For i = LBound(MArr) To UBound(MArr)
If IsInArray(MArr(i, 1), DArr) Then MArr(i, 1) = ""
Next i
With wbMatch
'~~> Clear the range for new output
rng.ClearContents
'~~> Output the array to the worksheet
.Range("A2").Resize(UBound(MArr), 1).Value = MArr
'~~> Sort it so that the blanks go down
.Columns(1).Sort Key1:=.Range("A2"), Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
End With
End Sub
'~~> function to check is a value is in another array
Function IsInArray(stringToBeFound As Variant, arr As Variant) As Boolean
Dim j As Long
For j = 1 To UBound(arr, 1)
On Error Resume Next
IsInArray = Application.Match(stringToBeFound, Application.Index(arr, , i), 0)
On Error GoTo 0
If IsInArray = True Then Exit For
Next
End Function
Edit
Another way. Based on the sample file, this code runs in approx 1 minute.
Start : 8/4/2016 08:59:36 PM
End : 8/4/2016 09:00:47 PM
Logic:
It uses CountIf to check for duplicates and then deletes the duplicates using .Autofilter
Sub Sample()
Dim wbMatch As Worksheet, wbDestSheet As Worksheet
Dim lRow As Long
Dim strSheetName As String
Dim rng As Range
Debug.Print "Start : " & Now
strSheetName = "Week " & ISOWeekNum(Format(Date)) - 1
'~~> Set your worksheets
Set wbMatch = Sheets("MatchData")
Set wbDestSheet = Sheets(strSheetName)
'~~> Store search values in 1st array
With wbMatch
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
.Columns(2).Insert
Set rng = .Range("B2:B" & lRow)
lRow = wbDestSheet.Range("A" & wbDestSheet.Rows.Count).End(xlUp).Row
rng.Formula = "=COUNTIF('" & strSheetName & "'!$A$1:$A$" & lRow & ",A2)"
DoEvents
rng.Value = rng.Value
.Range("B1").Value = "Temp"
'Remove any filters
.AutoFilterMode = False
With .Range("A1:E" & lRow) 'Filter, offset(to exclude headers) and delete visible rows
.AutoFilter Field:=2, Criteria1:=">0"
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
'Remove any filters
.AutoFilterMode = False
.Columns(2).Delete
End With
Debug.Print "End : " & Now
End Sub
Looks like #SiddarthRout and I were working in parallel...
My code example below executes in less than 2 secs (eyeball estimate) over almost 12,000 rows.
Option Explicit
Sub DeleteCopy2()
Dim codeTimer As CTimer
Set codeTimer = New CTimer
codeTimer.StartCounter
Dim thisWB As Workbook
Dim destSH As Worksheet
Dim matchSH As Worksheet
Set thisWB = ThisWorkbook
Set destSH = thisWB.Sheets("Week 32")
Set matchSH = thisWB.Sheets("MatchData")
Dim lastMatchRow As Long
Dim lastDestRow As Long
lastMatchRow = matchSH.Range("A" & matchSH.Rows.Count).End(xlUp).Row
lastDestRow = destSH.Range("A" & matchSH.Rows.Count).End(xlUp).Row
'--- copy working data into memory arrays
Dim destArea As Range
Dim matchData As Variant
Dim destData As Variant
matchData = matchSH.Range("A1").Resize(lastMatchRow, 1)
Set destArea = destSH.Range("A1").Resize(lastDestRow, 1)
destData = destArea
Dim i As Long
For i = 2 To lastDestRow
If Not InMatchingData(matchData, destData(i, 1)) Then
destData(i, 1) = ""
End If
Next i
'--- write the marked up data back to the worksheet
destArea = destData
Debug.Print "Destination rows = " & lastDestRow
Debug.Print "Matching rows = " & lastMatchRow
Debug.Print "Execution time = " & codeTimer.TimeElapsed & " secs"
End Sub
Private Function InMatchingData(ByRef dataArr As Variant, _
ByRef dataVal As Variant) As Boolean
Dim i As Long
InMatchingData = False
For i = LBound(dataArr) To UBound(dataArr)
If dataVal = dataArr(i, 1) Then
InMatchingData = True
Exit For
End If
Next i
End Function
The timing results from my code are (using the timer class from this post ):
Destination rows = 35773
Matching rows = 23848
Execution time = 36128.4913359179 secs
I am using the following code to search in the A column of a row for a name. If the name is found, it is placed in a column 2 over. I am trying to search against a list of names rather than one name. The names are listed in sheet1, I am searching text stored in column A on sheet4. Each row has a paragraph of text I want to search. When a match is found, the matching name(s) is put in cell c of the same row.
Sub test()
Dim ws1, ws2 As Worksheet, rng1, rng2, cel1, cel2 As Range
Dim i, lrow As Long
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet4")
'i only assumed that your data is both in column A of sheet 1 and 2
lrow = ws1.Range("A" & Rows.Count).End(xlUp).Row
Set rng1 = ws1.Range("A1:A" & lrow) 'this contains the names
lrow = ws2.Range("A" & Rows.Count).End(xlUp).Row
Set rng2 = ws2.Range("A1:A" & lrow) 'this contains list of text you want to search
i = 0
For Each cel2 In rng2
For Each cel1 In rng1
If InStr(cel1.Value, cel2.Value) <> 0 Then cel1.Copy ws2.Range("c1").Offset(i, 0): i = i + 1
Next cel1
Next cel2
End Sub
Cheers!
If my comment is correct then this should work for you:
say I start with this set up:
I would first add my look up values to a named range as follows:
then you can add this code:
Sub Sample()
Application.ScreenUpdating = False
With Range("A2", Range("A" & Rows.Count).End(xlUp)).Offset(, 2)
.FormulaR1C1 = _
"=IFERROR(LOOKUP(1E+100,SEARCH(LookUpValues,RC[-2]),LookUpValues),"""")"
.Value = .Value
End With
Application.ScreenUpdating = True
End Sub
and this should result in the following:
this is another way to get what you want but not really using formula.
Option Explicit
Sub test()
Dim ws1, ws2 As Worksheet, rng1, rng2, cel1, cel2 As Range
Dim i, lrow As Long
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")
'i only assumed that your data is both in column A of sheet 1 and 2
lrow = ws1.Range("A" & Rows.Count).End(xlUp).Row
Set rng1 = ws1.Range("A1:A" & lrow) 'this contains the names
lrow = ws2.Range("A" & Rows.Count).End(xlUp).Row
Set rng2 = ws2.Range("A1:A" & lrow) 'this contains list of text you want to search
i = 0
For Each cel2 In rng2
For Each cel1 In rng1
If InStr(cel1.Value, cel2.Value) <> 0 Then cel1.Copy ws1.Range("B1").Offset(i, 0): i = i + 1
Next cel1
Next cel2
End Sub
I proposed above approach since you are open to using VBA.
hope this is what or somewhat close to what you want.