Excel Formulas to excel-vba - vba

I have this formula that looks at various criteria across multiple columns and checks to see that if all the all the criteria match, it will paste data from one column to another. I've tried a couple ways to get it into VBA, but I can't seem to get anything to work. Thanks!
=IFERROR(INDEX(Sheet1!A$2:A$205,SMALL(IF(ISNUMBER(SEARCH("ECR Approval",Sheet1!$C$2:$C$205)),ROW(Sheet1!$A$2:$A$205)-ROW(Sheet1!$A$2)+1),ROWS(A$2:A2))),"")

This is an Array Formula and you can place this formula on the Sheet by using the following code...
Dim ws As Worksheet
Set ws = Sheets("Sheet2") 'Sheet where formula would be placed
ws.Range("A2").FormulaArray = "=IFERROR(INDEX(Sheet1!A$2:A$205,SMALL(IF(ISNUMBER(SEARCH(""ECR Approval"",Sheet1!$C$2:$C$205)),ROW(Sheet1!$A$2:$A$205)-ROW(Sheet1!$A$2)+1),ROWS(A$2:A2))),"""")"
ws.Range("A2").AutoFill ws.Range("A2:A205"), xlFillDefault

To use a function in VBA, you need to use before each function Application.WorksheetFunction.
x = Application.WorksheetFunction.Sum(y,z)
To reference a cell in a sheet in VBA you can use Rage
x = Application.WorksheetFunction.Sum(Range("A1:A2"))
Put this to things Together and it would look like this:
=Application.WorksheetFunction.IFERROR(Application.WorksheetFunction.Index(Worksheet(1).Range("A2:A205"),Application.WorksheetFunction.SMALL(Application.WorksheetFunction.IF(Application.WorksheetFunction.ISNUMBER(....

Related

Vlookup data in certain external excel file using a function

I use a lot of Vlookup to find prices in a certain file (c:/pricelist.xlsx), using the product code. I would like to simplify it by creating a new VBA function, but it does not work.
Function findprice(codes)
Dim price
Dim pricebook As Workbook
Dim pricesheet As Worksheet
Dim pricerange As Range
Dim coderange As Range
Set pricebook = Workbooks("c:\info\pricelist.xlsx")
Set pricesheet = pricebook.Sheets("Sheet2")
Set pricerange = pricesheet.Range("FF1:FF20000")
Set coderange = pricesheet.Range("H1:H20000")
findprice = Application.Index(pricerange, Application.Match(codes, coderange, 0))
End Function
I can't say for sure since your question didn't include any sample data or explanation of what/why you're trying to do, but I suspect you're over-complicating this is a few ways.
You don't need a custom function to call a worksheet function from the worksheet. Just use the worksheet functions instead.
In this case VLookup is easier and saves a step (as you suggested in your title.)
Instead of referring to huge ranges of two cells, just refer to the whole columns.
Referring to cells in another Excel file is just like any other formula that refers to another file. (More infor here)
You should be able to use something like this on the worksheet:
=VLOOKUP(A1,'c:\info\pricelist.xlsx'!$H:$FF,155,FALSE)
...where A1 is the value to match (ie., codes).
If you do need this is VBA for some reason, it's easy to adapt just like your sample function.
More Information:
Office Support : Create or change a cell reference
Office Support : Create an external reference (link) to a cell range in another workbook

Referencing multi-sheet named range in VBA

I have a named range in Excel that I am trying to clear using VBA.
The problem with the range is that spans across multiple sheets and I am not sure how to properly reference the named range in VBA since it covers multiple sheets.
The Named Range "Counts" is set to Workbook scope with the following cell references:
=Sheet1!$A$1, Sheet2!$A$1, Sheet3!$A$1
When clearing a named range where it only has cells referenced on one sheet I use the following:
ThisWorkbook.Sheets("Sheet1").Range("Counts").ClearContents
I have tried the following but neither seemed to work.
ThisWorkbook.Range("Counts").ClearContents
and
Range("Counts").ClearContents
The last gives me a global error.
Instead of a range that goes across multiple sheets (which does not work, as we have established), you need a worksheet scoped range in each sheet.
When defining a range name you can set its scope to workbook or the current sheet. This way you can have the same range name in many sheets.
Use VBA to loop through all worksheet, access the ws.Range("TheRangeName") on the current sheet and clear its contents.
That's a cleaner approach.
I would write something that displays the names and you can use that to remove it...
In the immediate window:
For i = 1 to names.count:Debug.print i, Names(i).RefersTo, Names(i).name:next
You can then either use th name or the index to remove the particular name you want.
Hope that helps.
Rory Archibald states on his website post about named ranges that: A Range object can only refer to cells on one worksheet. You cannot refer to cells on different sheets with one Range object.
So I am unable to accomplish what I was wanting but I should be able to just create multiple named ranges and just clear them one at a time.
I was able to complete the code for this as follows with inspiration from teylyn
Public Sub ClearRanges()
'Checks if named Range exists on sheet if it does then clear contents
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Worksheets
If Len(ws.Range("Counts").Name) <> 0 Then
ws.Range("Counts").ClearContents
End If
Next ws
Application.DisplayAlerts = True
End Sub

Excel VBA for hiding cells in one sheet if they match cells in another sheet

I am new to VBA and am having problems learning the rules of variables (I think that's the problem here).
I have two worksheets in a spreadsheet. I need to make code that automatically hides a row on worksheet 2 if that same value in column a is on worksheet 1, column a.
Here's one of the variations of code I've tried:
Dim Sheet2Value As Variant
Dim Sheet1Value As Variant
'
Sheet2Value = Sheets("Sheet2").Range("A:A").Value
Sheet1Value = Sheets("Sheet1").Range("A:A").Value
'
If Sheet2Value = Sheet1Value Then
Sheets("BMAC=N").EntireRow.Hidden = False
Else
Sheets("BMAC=N").EntireRow.Hidden = True
End If
I get a type mismatch error but I'm not sure exactly why. I chose variant because I don't know what I'm doing, but both columns in excel will be set to "General".
Can anyone help with this? What concept am I missing?
Thanks so much for your time.
Few things:
you cannot compare entire column:
Sheet2Value = Sheets("Sheet2").Range("A:A").Value
you need to loop through the collection of cells, see this: Fast compare method of 2 columns
you cannot hide row without defining a range to hide
Sheets("BMAC=N").Range("Some_address").EntireRow.Hidden
Finally, i'd suggest to change your code to shortest way:
Sheets("BMAC=N").Range("A1").EntireRow.Hidden = (value1<>value2)
Good luck!

Excel VBA: Find string variable in range on separate sheet, then set range variable to that column

I'm trying to find a string variable A within the range Lists.Range("I2:AD2"). Once that cell is found, I'd like to set a new range variable A_backup to be the range from the cell in "I2:AD2" that was found, down to the last not empty cell in the column. I've been able to do this using select, activecell, etc., but I'd like to avoid that as it causes problems when I run it from different sheets in the workbook.
Here's the code that seems to be working:
A = OrderForm.Range("C15").Value
If Len(A) > 0 Then
Set A_backup = Lists.Range("I2:AD2").Find(A)
From here I want it to do something like:
Set A_backup = Range(A_backup.Address, A_backup.Address.End(xlDown)
End If
Can't seem to figure that part out though. Thanks for the help!
Figured it out, but would love to hear if there's a better way:
Set A_backup = Range(Lists.Range("I2:AD2").Find(A), Lists.Range("I2:AD2").Find(A).End(xlDown))
End If

How do I export specifc rows of data to another spreadsheet

I'm totally new to VBA and have a requirement to export specific rows based on a value within a specific column to another spreadsheet. Unfortunately I can't post an example so I'll try to explain. I have a sheet that consists of columns A to D and any number of rows. I need to copy all rows that contain [DV] anywhere in column C to another sheet in another spreadsheet.
I have another couple of variants that I would also need to do but I'm hoping if I get this one I can then modify it to suit my needs for the others.
You will have to read, each row under the column C (for each). At each row you will need to sub string the characters in that cell. If they match your condition (DV), than copy it to your desired worksheet.
You didn't provide any code, so this is the best I can help you. If you want more help, you will need to provide some code, and show where it is exactly that you are stuck.
this should do it :
Dim cl As Range
dim wb as workbook
dim ws as worksheet
set wb = workbooks.open("Your other workbook path & name")
set ws = wb.Sheets("destination sheet name")
For Each cl In Range("D1:D" & Range("D1").End(xlDown).Row)
If InStr(cl.Offset(0, -1).Value, "[DV]") > 0 Then
cl.EntireRow.Copy
ws.Range("A50000").End(xlUp).Offset(1, 0).PasteSpecial
End If
Next cl