Excel 2010 index match VBA - excel

Tried everything and can't seem to get this. Trying to replace values in Row B (SiteTag) of one worksheet with the proper sitetag from an index match in another worksheet.
Worksheet(Site_Visit)
SiteTag
AL27
AS26
GBEM4
...
Worksheet(Sites)
SiteTag Project Name
203AL27 AL27
203AS26 AS26
201GBEM4 GBEM4
... ...
I need to replace the values SiteTag in Sheets("Site_Visit") with the appropriate SiteTag from Sheets("Sites").
For now I've simply tried to get the code to place the correct index value into a variable in which I'll place as the value for each cell, and run it in a loop. But for the life of me can't get it to get a value. This is what I've tried for the variable (everything has been declared).
ST_Cells2 = Application.WorksheetFunction.Index("Sites!A2:A34", Application.WorksheetFunction.Match("Site_Visit!B2", "Sites!B2:B34", 0), 0)
Where "Sites!A2:A34" is the range for the appropriate replacement value
"Sites_Visit!B2" is the lookup value
"Sites!B2:B34" is the lookup range
I'm getting a Run Time error '1004' Unable to get the Match property of the WroksheetFunction class. Anyone have any ideas?

The Index and Match functions are expecting Ranges, but you are sending them strings. The easiest way to turn strings into Ranges is to use Excel's Range function:
st_cells2 = Application.WorksheetFunction.Index(Range("Sites!A2:A34"), Application.WorksheetFunction.Match(Range("Site_Visit!B2"), Range("Sites!B2:B34"), 0), 0)

I had the same error, but it run ok when I changed to "Application" indstead of WorksheetFunction:
Cells(12, 12).Value = Application.Index("Sheet1!B1:9", 2)
Somehow running the Function from Application directly worked...
/K

Related

Can you access a VBA list with an in-cell Excel formula?

I wrote a VBA script/macro which runs when a change is detected in a specific range (n x m) of cells. Then, it changes the values in another range (n x 1) based on what is detected in the first range.
This bit works perfectly ... but then comes the age old erased undo stack problem. Unfortunately, the ability for the user to undo their last ~10 or so actions is required.
My understanding is that the undo stack is only cleared when VBA directly edits something on the sheet - but it is preserved if the VBA is just running in the back without editing the sheet.
So my question is: Is it possible to use an in cell formula (something like below) to pull values from a VBA array?
'sample of in-cell function in cell A3
=function_to_get_value_from_vba_array(vba_array, index_of_desired_value)
Essentially, VBA would store a 1D array of strings with the values needed for the range. And by using a formula to grab the value from the array: I might be able to get around the issue of the undo stack being erased.
Thanks!
Solution
You need to do something like the following: your argument for the function should be calling the array bulding; I created one dummy function that creates some sample arrays to demonstrate it. In your case, likely you will need to store the changes on the worksheet event in a global array variable instead, and as you stated, do nothing on the worksheet (whenever a change happens, just redim or appended it on your global array as needed). However, a new problem may arise and that is when you close/reopen, or by some reason the array is lost, so you need to keep track of it, I would suggest to catch before close event and then convert the formulas to static values.
Function vba_array(TxtCase As String)
Dim ArrDummy(1) As Variant
Select Case TxtCase
Case "Txt": ArrDummy(0) = "Hi": ArrDummy(1) = "Hey"
Case "Long": ArrDummy(0) = 0: ArrDummy(1) = 1
Case "Boolean": ArrDummy(0) = True: ArrDummy(1) = False
End Select
vba_array = ArrDummy
End Function
In your calling function, do the following
Function get_value_from_vba_array(vba_array() As Variant, index_of_desired_value As Long) As Variant
'when parsing, even with option base 0 it starts at 1, so we need to add 1 up
get_value_from_vba_array = vba_array(index_of_desired_value + 1)
End Function
In your book, your formula should be something like:
=get_value_from_vba_array(vba_array("Txt"),1)
Demo
I did some actions before, so you are able to see that the "undo" works

problem with VBA Find function in cells with formula

I have a find function, it is supposed to look for "2021" in the range DP.
Range(DP).Find("2021", LookIn:=xlValues, LookAt:=xlWhole).Offset(1, 0) = t
2021 is in range but it is the result of a formula.
(Cell A2 in range DP is =$B$5 and B5=2021)
All ranges and variables are defined and the code works perfectly.
However, every once in while, I get the "'Run-time Error '91'"!!
I press debug, vba page opens and the Find function is the issue. (the value isn't found)
And the best part is, if I delete a random letter of the code and rewrite the EXACT same letter (basically changing NOTHING), and close vba and run again, the code works again without errors!!!
Any idea how to fix this bug?
Thanks in advance.
Make sure to define the worksheet
ThisWorkbook.Worksheets("Sheet1").Range(DP)
Make sure DP is a defined variable (use Option Explicit) or if you meant to use column DP then make sure it is a string Range("DP").
So maybe it should look something like
Dim t As String
t = "test"
Dim DP As String
DP = "A:A"
ThisWorkbook.Worksheets("Sheet1").Range(DP).Find("2021", LookIn:=xlValues, LookAt:=xlWhole).Offset(1, 0) = t
or something like this
ThisWorkbook.Worksheets("Sheet1").Range("DP").Find("2021", LookIn:=xlValues, LookAt:=xlWhole).Offset(1, 0) = "t"

Identify all cells that have been conditionally formatted

I am trying to remove duplicate values except the first one.
My solution to this was to conditionally format all duplicates, then, working backwards, clear contents of the formatted cells. This would mean that the first one will stop being formatted once all duplicates are removed.
What I have been trying:
For i = LaC To 5 Step -1
LR = ws.Cells(Rows.Count, LaC).End(xlUp).Row
For j = 2 To LR
cond = (ws.Cells(j, i).DisplayFormat.Interior.ColourIndex.Value)
If cond = 22 Then
ws.Cells(j, i).ClearContents
End If
Next
Next
Basically, if I try ws.Cells(j, i).DisplayFormat.Interior.ColourIndex in the immediate window, it returns 22.
However, if I try this code, I get error:
Object doesn't support this property or method (Error 438)
Any assistance would be greatly appreciated.
If it is not necessary to use VBA you can get rid of duplicates in the GUI via “Data” → “Data Tools” → “Remove Duplicates”. Or, in current versions of Excel O365, you can use the UNIQUE-function like this
UNIQUE(A:A)
assuming your source data is in row A. This will keep up with changing data.
Check this line more carefully:
cond = (ws.Cells(j, i).DisplayFormat.Interior.ColourIndex.Value)
ColorIndex is a Property, not an Object. As such, ColorIndex.Value will return an error. Note that you haven't included the .Value when testing in the Immediate Window, and it works as expected:
?ws.Cells(j, i).DisplayFormat.Interior.ColourIndex 'This will work
?ws.Cells(j, i).DisplayFormat.Interior.ColourIndex.Value 'This will err
Also, there is no need for you to put it in brackets, either:
cond = ws.Cells(j, i).DisplayFormat.Interior.ColourIndex
Ans: I was writing Colour instead of Color.

VBScript - Either getting object required or type mismatch errors

I have scoured the web and this site looking for an answer on this, so I would really appreciate some help.
I'm creating a VBScript to do some modifications to a user-specified Excel spreadsheet. I have the first part of my script working fine, but the second part is driving me nuts. I need it to search the first column for a value and, if found, delete the row. Right now I'm not worrying about the deletion statement--I'm doing testing by seeing if I can get the For Each statement to run properly as well as the If Then statement. Here's the specific block of code:
For Each cell in objSheet.Columns("A:A").Cells
Set cell = objSheet.Columns("A:A").Cells
If cell.Value = "60802400040000" then
cell.font.bold = True
End If
Next
I have tried many variations of this and cannot find the right combination. Initially I was getting an "Object Required" messages, and after reading a number of posts, found that I needed to put in a Set statement for cell, which I did. Now I am getting a Mismatch Type error message.
The funny thing is, before I put in the Set statement, the code would execute, but it would throw the Object Required error when I closed the spreadsheet. After adding it, the error for the Type Mismatch pops up immediately.
Most examples I keep finding on the web are for VBA, and I try to modify them for VBS, which I don't know very well. Any assistance anyone can give me will be greatly appreciated.
You are redefining cell, cell is defined automatically in the For Each statement.
Delete this line
Set cell = objSheet.Columns("A:A").Cells
This is an example from Help, unfortunately Help doesn't have any examples that uses For Each, only For x = n to n and other means. For Each is the right thing to do.
Set r = Range("myRange")
For n = 1 To r.Rows.Count
If r.Cells(n, 1) = r.Cells(n + 1, 1) Then
MsgBox "Duplicate data in " & r.Cells(n + 1, 1).Address
End If
Next n
For vba to vbs, you have to create the object and use, as some objects are automatically available in VBA (like app object) - Set exceldoc = CreateObject("c:\blah\blah.xls) then to use Set r = exceldoc.worksheets(0).range("MyRange").
Also you have to use constant values not names as vbscript can't look them up.

VBA function "Object variable missing" error. Number 91

Excel keeps lighting it up with an "Object variable missing" error. Number 91.
Function GetMonthRange(sheetMonth) As Range
GetMonthRange = ActiveCell.Range("A1:AB1")
End Function
I'm pretty sure that Excel is maintaining its own clipboard.
Here's the link to the whole file.
https://github.com/okamura1967/Directors_project_sheet/blob/master/project-sheet-for-directors.vbs
There are several things wrong with your function.
1. If you want to return a range you have to use Set because Range is an object.
2. The parameter sheetMonth is not used
3. The function will return different results depending on whatever the activecell happens to be when the function is executed.
4. If this is a UDF it will not recalculate whenever anything in A1:B1 changes, because the A1:B1 is not a parameter.
What are you actually trying to do?
I changed your function to:
Function GetMonthRange() As Range
Set GetMonthRange = ActiveSheet.Range("A1:AB1")
End Function
This seems to work for me now.

Resources