Lookup - Type mismatch - excel

I cannot find the correct type for my lookup function in vba.
My Excel formula is as following and works fine.
=IF(INDIRECT("'Enclosure4-Workflow_Structure'!C"&MATCH('Enclosure2-Accesses'!A8;
'Enclosure4-Workflow_Structure'!A:A; 0))="Create";
IF(LOOKUP(2; 1/('Enclosure5-Workflow_Steps'!A:A=INDIRECT("'Enclosure4-Workflow_Structure'!D"
&MATCH('Enclosure2-Accesses'!A8; 'Enclosure4-Workflow_Structure'!A:A; 0)));
'Enclosure5-Workflow_Steps'!D:D) = "Task"; 'Enclosure2-Accesses'!B8; FALSE); FALSE)
The first if-clause works fine for me but the second if-clause contains the lookup function. This lookup function should come up with "Task", therefore I thought I should set the DIM as String but I constantly receive the error message: "Type mismatch".
The line which throws the error should get the last occurence of a value. This value should correspond to "Task" in the D column.
lOccurence = WorksheetFunction.Lookup(2, 1 / (Enc5.Range("A:A") = Enc4.Cells(MatchCrt, "D").Value), Enc5.Range("D:D"))
I am curious why the above line causes the error. In Excel the line works without problem. Is the line incorrect or is the DIM type (String) incorrect?
My VBA code is:
Public Sub CopyUserAR2Data()
Dim Enc2 As Worksheet
Dim Enc4 As Worksheet
Dim Enc5 As Worksheet
Dim Enc9 As Worksheet
Dim MatchCrt As Double
Dim lOccurence As String
Set Enc2 = Sheets("Enclosure2-Accesses")
Set Enc4 = Sheets("Enclosure4-Workflow_Structure")
Set Enc5 = Sheets("Enclosure5-Workflow_Steps")
Set Enc9 = Sheets("Enclosure9-Dependency")
MatchCrt = WorksheetFunction.Match(Enc2.Cells(9, "A"), Enc4.Range("A:A"), 0)
lOccurence = WorksheetFunction.Lookup(2, 1 / (Enc5.Range("A:A") = Enc4.Cells(MatchCrt, "D").Value), Enc5.Range("D:D"))
If Enc4.Cells(MatchCrt, "C") = "Create" Then
Enc9.Cells(2, 1).Value = lOccurence
End If
End Sub

Your string will cause an error due to you pulling a long value from the Lookup
Best thing to do is call
Dim lOccurence As Variant
If you still the get error, there's something else going on - may a null return value

I went with the suggestion from Rory in the comment section and am looping through the array.
If sheet1.Cells(FirstCondition, "C") = "Create" Then
For d = 1 To numberOfRows
If (sheet1.Cells(FirstCondition, "D").Value = sheet2.Cells(d, "A").Value And sheet2.Cells(d, "D").Value = "Task") Then
{"Cell values are entered"}
End If
Next d
End If
This works now as intended.
#Rory, thank you very much for your patience and assistance!

Related

New Line in Cell to New Row Macro [duplicate]

I am trying to write an array to a range and I have tried several ways but no matter what, I always get only the FIRST value of the array over and over again.
Here is the code:
Option Explicit
Sub test()
ActiveWorkbook.Worksheets("Sheet1").Cells.Clear
Dim arrayData() As Variant
arrayData = Array("A", "B", "C", "D", "E")
Dim rngTarget As Range
Set rngTarget = ActiveWorkbook.Worksheets("Sheet1").Range("A1")
'this doesn't work
rngTarget.Resize(UBound(arrayData, 1), 1).Value = arrayData
Dim rngTarget2 As Range
Set rngTarget2 = ActiveWorkbook.Worksheets("Sheet1").Range(Cells(1, 5), Cells(UBound(arrayData, 1), 5))
'this doesn't work either
rngTarget2.Value = arrayData
End Sub
What I expect to see is:
(Col A) (Col E)
A A
B B
C C
D D
E E
What I actually see is:
(Col A) (Col E)
A A
A A
A A
A A
A A
What am I doing wrong here?
I tried to follow Chip Pearson's suggestions, as found HERE
But no luck...
Okay, so adding in the second part of this problem:
I have a 1D array with 8,061 elements that I am passing to the following function as such:
Call writeArrayData7(strTabName, arrayBucketData, 7)
Sub writeArrayData7(strSheetName As String, arrayData As Variant, intColToStart As Integer)
Dim lngNextRow As Long
lngNextRow = 1 ' hard-coded b/c in this instance we are just using row 1
' Select range for data
Dim rngData As Range
Set rngData = Sheets(strSheetName).Range(Cells(lngNextRow, intColToStart), Cells(lngNextRow - 1 + UBound(arrayData, 1), intColToStart))
' Save data to range
Dim arrayDataTransposed As Variant
arrayDataTransposed = Application.Transpose(arrayData)
rngData = arrayDataTransposed
End Sub
So when I run this, the transpose function is properly converting into an:
Array(1 to 8061, 1 to 1)
The range appears to be a single column with 8,061 cells in Column G.
But I get the following error:
Run-time error '1004':
Application-defined or object-defined error
The error is thrown on the following line:
rngData = arrayDataTransposed
--- UPDATE ---
So one thing I left out of my sample code (b/c I honestly didn't think it mattered) was that the contents of my array are actually formulas. Here is the line that I'm using in the actual live code:
arrayData(i) = "=IFERROR(VLOOKUP($D" + CStr(i) + "," + strSheetName + "!$D:$F,3,FALSE),"")"
Well, what I found (with Excel Hero's help) was that the above statement didn't have the double sets of quotes required for a string, so I had to change to this instead:
arrayBucketData(i) = "=IFERROR(VLOOKUP($D" + CStr(i) + "," + strSheetName + "!$D:$F,3,FALSE),"""")"
I can chalk that up to late-night bonehead coding.
However, one other thing I learned is that when I went back to run the full code is that it took FOREVER to paste the array to the range. This would ordinarily be a very simple task and happen quickly, so I was really confused.
After much debugging, I found that the issue came down to the fact that I was turning off all the alerts/calculations/etc and when I pasted in these formulas, the strSheetName sheet was not there yet b/c I'm developing this code separate from the main file. Apparently, it throws up a dialog box when you paste the code in, but if you have all that stuff shut off, you can't see it but it REALLY slows everything down. It takes about 6mins to paste the range if those tabs are not there, and if they exist it takes seconds (maybe less). At any rate, to refine the code a bit further, I simply added a function that checks for the required sheet and if it doesn't exist, it adds the tab as a placeholder so the entire process doesn't slow to a crawl.
Thanks to everyone for their help! I hope this helps someone else down the road.
Do this:
arrayData = Array("A", "B", "C", "D", "E")
[a1].Resize(UBound(arrayData) + 1) = Application.Transpose(arrayData)
The important bit is the Transpose() function.
But it is better to work with 2D arrays from the get go if you plan on writing them to the worksheet. As long as you define them as rows in the first rank and columns in the second, then no transposition is required.
This:
Sub test()
ActiveWorkbook.Worksheets("Sheet1").Cells.Clear
Dim arrayData(1 To 5, 1 To 1) As Variant
arrayData(1, 1) = "A"
arrayData(2, 1) = "B"
arrayData(3, 1) = "C"
arrayData(4, 1) = "D"
arrayData(5, 1) = "E"
Dim rngTarget As Range
Set rngTarget = ActiveWorkbook.Worksheets("Sheet1").Range("A1:A5")
rngTarget = arrayData
End Sub
will produce:
If I may expand the accepted answer, I propose:
[a1].Resize(UBound(arrayData) - LBound(arrayData) + 1) = Application.Transpose(arrayData)
That would be the safe way. This will work no matter if you declare your array variable as:
Dim arrayData(0 to 2)
or
Dim arrayData(1 to 3)
The accepted answer works only in the second case.
The proposed way might be useful if the size of array is unknown and you declare your arrayData as:
Dim arrayData()

How, on a table, do I do a listobject.listColumns(1).find.row

I have an excel table, with one of the columns is named ID.
I am trying to do a search of that column to find what row contains a certain value. I have tried many different commands, (all of which check our with the editor's syntax checking), but I cannot figure out how to get it to work.
My line of code that I am trying to make work is toward the bottom of the program and is foundrow = summObj.ListColumns("ID")...
Here is my code so far:
Private Sub Reload_Click()
'Routine to move data to table from Molding Table
'Check to not overwrite current records but add new ones.
Dim summObj As ListObject
Dim moldObj As ListObject
Dim I, X As Integer
Dim summObjRows As Integer
Dim moldObjRows As Integer
Dim key As String
Dim foundrow As Integer
' Get the table reference
Set summObj = Worksheets("Summary").ListObjects("SummaryTable")
Set moldObj = Worksheets("MoldingData").ListObjects("MoldingTable")
summObjRows = summObj.ListRows.Count
moldObjRows = moldObj.ListRows.Count
'Check if table is empty
If summObjRows = 0 Then
'Set up the first row
summObj.ListRows.Add
summObj.ListColumns("ID").DataBodyRange(1) = "New"
End If
X = 1
For I = 1 To moldObj.ListRows.Count
key = moldObj.DataBodyRange(I, moldObj.ListColumns("ID").DataBodyRange.Column) & "," & moldObj.DataBodyRange(I, moldObj.ListColumns("6-Way").DataBodyRange.Column)
If moldObj.DataBodyRange(I, moldObj.ListColumns("Volume").DataBodyRange.Column) <> "" Then
If Not (key = summObj.ListColumns("ID").DataBodyRange(X)) Then
'Insert row into Summary Table unless this is the first row in a blank table.
If Not summObj.ListColumns("ID").DataBodyRange(X) = "New" Then
summObj.ListRows.Add (X)
End If
summObj.ListColumns("ID").DataBodyRange(X) = key
summObj.ListColumns("Volume").DataBodyRange(X) = moldObj.ListColumns("Volume").DataBodyRange(I)
summObj.ListColumns("Item Name").DataBodyRange(X) = moldObj.ListColumns("Item Name").DataBodyRange(I)
summObj.ListColumns("Data1").DataBodyRange(X) = moldObj.ListColumns("Data1").DataBodyRange(I)
summObj.ListColumns("Data2").DataBodyRange(X) = moldObj.ListColumns("Data2").DataBodyRange(I)
Else
summObj.ListColumns("Volume").DataBodyRange(X) = moldObj.ListColumns("Volume").DataBodyRange(I)
summObj.ListColumns("Data2").DataBodyRange(X) = moldObj.ListColumns("Data2").DataBodyRange(I)
End If
X = X + 1
Else
'Check it to see if it is in Summary, and if so remove it
foundrow = summObj.ListColumns("ID").DataBodyRange.Find(key).Row
End If
Next I
End Sub
Thanks for helping me to solve this one.
Rich
I made a small change to the line in question.
I removed the (x) from the line.
Now what happens is that if the string is found in the column, then the row number is put in foundrow. If the string is not in the column, then i get the runtime error '91'
Any additional thoughts?
Rich

Excel VBA offset Copy Paste

Hope you're doing well. I'm going to preface this by saying I'm not a programmer and I'm sure the code I have started is riddled with more errors then what I think. Hopefully you can help :D.
I have an Excel sheet that gets generated from another program that comes out like this:
excel sheet
However, the size of this sheet can change with every new generation of this sheet from the other program. (ex, A can have 7 next time, and D could have 9) And the sheet as it is cannot be used easily to do the math required as I only need specific groups of information at a given time, in this example groups B and D only.
What I'm hoping to create is something that will take the sheet as its generated, and turn it into something that looks like this:
result sheet
This is the code I've written so far, but since I don't really know what I'm doing I keep running into numerous problems. Any help would be appreciated.
Option Explicit
Sub Numbers()
Dim matchesFound As Integer
Dim row As Integer
Dim c As Integer
Dim copyRow As Integer
Dim copyLocationColumn As Integer
Dim arr(2) As String
arr(0) = "1"
arr(1) = "2"
arr(2) = "3"
Function arrayContainsValue(array, varValue)
found = false
for each = 0 to array
if array(i) = varValue then
found = true
exit for
arrayContainsValue = found
End Function
row = 1
c = 1
copyLocationColumn = 1
copyRow = 1
matchesFound = 0
Do While matchesFound < 3
if arrayContainsValue(arr, ThisWorkbook.Sheets("Data").Cell(column,row))
matchesFound = matchesFound + 1
Do While ThisWorkbook.Sheets("Data").Cell(column, row)
ThisWorkbook.Sheets("postHere").Cell(copyLocationColumn, copyRow) = _
ThisWorkbook.Sheets("postHere").Cell(c + 1, row)
copyRow = copyRow+1
row = row + 1
Loop
End If
row = row + 1
Loop
End Sub
There are many logic errors to numerate in a comment, Excel highlights them automatically I'll do a summary explaining them:
1. Function can't be "in the middle" of the sub, finish the Sub (take the Function from the sub and paste until it says end sub.
2.array is a forbidden name, try with another variable name
3.For each =0 ? to array? what do you try to mean like that? For Each has to be element in something For each element in Array for example For and To are for something defined in numbers (for counter=1 to 15)
Function arrayContainsValue(***array***, varValue) '2nd problem
found = false
for each = 0 to array '3rd problem
if array(i) = varValue then
found = true
exit for
arrayContainsValue = found
End Function
....
4. you're missing a then at the end
if arrayContainsValue(arr, ThisWorkbook.Sheets("Data").Cell(column,row))
I don't get the coding logic on how relates to the problem stated (?)

Compare two cells in different worksheets, set different cell as ok

Sub test1()
Dim Str As String
Dim Search As String
Dim Status As String
Str = Cells(2, 5).Value
Search = FDSA!Cells(2, 5).Value
Status = FDSA!Cells(2, 10).Value
If InStr(Search, Str) = True Then
Status = "ok"
Else
End If
End Sub
I will be building up from this with loops. I want to check if what is in Cells(2,5) is contained in FDSA!Cells(2,5). If it is true then I would like to mark FDSA!Cells(2,10) as ok. I am getting an object required message. This is what I could come up with after looking at examples and tutorials. Let me know if you have questions
Only second time working on VBA.
Thanks in advance, Alexis M.
Your syntax for referencing the worksheet is incorrect. That is probably throwing the error. You need to call to Worksheets("FDSA") and not use the FDSA! call like you have.
Also, you will have to set the cell value equal to Status for this to work. Just changing Status will not write it back into the workbook.
Also InStr returns the location of the match. If you want to know if there was a match, you need to check that the return is >0. This code should run and hopefully is closer to correct than your current code.
Sub test1()
Dim Str As String
Dim Search As String
Str = Cells(2, 5).Value
Search = Worksheets("FDSA").Cells(2, 5).Value
If InStr(Search, Str) > 0 Then
Worksheets("FDSA").Cells(2, 10).Value = "ok"
End If
End Sub

Excel VBA WorksheetFunction VLookup multiple range search - is it possible?

I use check boxes on individual worksheets to set ranges for performing VLookup functions. One of the check boxes needs to set two distinct ranges in which to search. I'm out of ideas on how to make this work. All the other possible variants are searching a continuous string of cells (i.e. [S9:T20] or [S55:T66] but not both. If I end up having to u multiple variables and perform the function twice the rest of my code will probably not work. Any ideas would be appreciated including if some sort of Find function might do similar work.
Below are snippets of the code that I use:
Dim rngO As Variant
ElseIf ActiveSheet.Shapes("Check Box 43").ControlFormat.Value = 1 Then
rngO = [S9:T20;S55:T66]
The rngO variant is used as shown below (one example):
Case 2
With ActiveSheet
.Range("U2").Value = "1Y"
.Range("V2").Value = WorksheetFunction.VLookup("1Y", rngO, 2, False)
.Range("U3").Value = "1P"
.Range("V3").Value = WorksheetFunction.VLookup("1P", rngO, 2, False)
.Range("U4").Value = "."
.Range("V4").Value = "."
short answer: Yes - it is!
longer answer:
You wrap the WorksheetFunction.VLookup() by some code looking at each area of your source range individually.
Function MyVLookup(Arg As Variant, Source As Range, ColNum As Integer, Optional CmpSwitch As Boolean = True) As Variant
Dim Idx As Integer
MyVLookup = CVErr(xlErrNA) ' default return value if nothing found
On Error Resume Next ' trap 1004 error if Arg is not found
For Idx = 1 To Source.Areas.Count
MyVLookup = WorksheetFunction.VLookup(Arg, Source.Areas(Idx), ColNum, CmpSwitch)
If Not IsError(MyVLookup) Then Exit For ' stop after 1st match
Next Idx
End Function
and in your original code replace all calls to WorksheetFunction.VLookup() by calls to MyVLookup() with the same parameters.
Alternatively you can use this function directly in a cell formula (that's what I usually do with it ...)

Resources