I am running a vlookup in vba which returns some errors. Some are due to missing values (these should remain #N/A) some are due to the values not being converted to numbers (these need to be fixed). However when I run the below code it converts both numbers and letters into numeric values and returns #VALUE when it should do nothing. I need a way to differentiate between the numbers and any values with a letter in them. In the below example the code should loop through, skip TRD4 and convert 9501 to a number.
L26 BW
VR7F BW
TRD4 #N/A
TRD4 #N/A
9501 #N/A
XDTM BC
UDDP TE
For a = 2 To LCell2 Step 1
v = Cells(a, 9).Value
If IsError(v) Then
Cells(a, 8).Value = Evaluate("H" & a)
End If
Next a
It would also help if you included your full code such as what is v declared as, is Cells(a,9) your listed values such as L26 and what is the formula that is in H & a.
Running a quick check with the sample values you have provided you can used IsNumeric in your loop to test if a value is made of just numbers or also contains text. This will work even if the value is stored as text in the cell. To convert a text value to numeric you can use CInt or any number of conversion functions available.
So if you want to only evaluate if it's a number and ignore text you can do something similar to
For a = 2 to LCell2 Step 1
If IsNumeric(Cells(a,9)) then
Cells(a,8).Value = Evaluate("H" & a)
End If
Next a
Related
I am a beginner in VBA.
I have components which always consist from 2 parts. (Rotor and a stator, each has its own number). When work is with them it can be damaging some of these parts, however it is necessary to keep a list of damaged parts, where the result is inventory e.g. 200 rotors, stators 150 with different numbers. Before I could scrap it, I need to complete them as proper sets. I.e. rotor "a" stator "a", "b" with "b", etc. It's crazy to work with many numbers to compare them, copy …to find the result of sets qty.
It is possible to solve it with Macro, what I try to do, but I was stuck.
What is the task: In the column "A" I have a list of all damaged parts (mix of rotors, stators different numbers). In the column "C" an information only with help of VlookUP, what should be a counterpart number.
What do I need to solve: In row 5, column. „A“ I have component number , but I know that in the same column, somewhere from row 6 to xx I have a counterpart. What I need is … according to information from column C, same row(5) where is info about the counterpart num. to find counerpart in column A, when found, took it out and put into cell B5. Thus,I get a complete set. Then the next row (6), same action. Macro reading num. in „C“,searching in „A“, when found, cut, and put to „B“ next row 7,8,9,… The result should be a certain qty of pairs + some single numbers if not second part found.
The problem I have is that cycle is working until always found relared counterpart. If the counterpart in row A is not available (no match betwen C-A), the code will stop on that row.
What I need help with is, that if code did not find the counerpart based to info from C just skip this row, make it red and continue with next row till end, it means stop on first empty cell in C. Thanks a lot to everybody who is helping me.
Dim pn As Range,
Dim a
Dim x
x = 5
Dim i As Long, Dim radek As Long
a = Cells(x, 3)
For i = 1 To 500
Range("A:A").Select
Set pn = Selection.Find(What:=a)
If Not pn Is Nothing Then
pn.Select
End If
Selection.Cut
Cells(x, 2).Select
ActiveSheet.Paste
x = x + 1
Next
End Sub
I have a macro running on excel which copies a number from one sheet to the other
First I store all the relevant numbers in a string array va() and than assign the array values in the new sheet.
ActiveCell.Offset(0, 5).Value = va(i, 5)
when number is -0,522004, I get the correct result in the new sheet, however when the number is -1,175378 I get -1175378,0000 as output in the excel sheet.
Note that I am using "," as decimal separator and "." as thousand separator.
I also tried to put msgbox va(i,5) and it shows -1,175378 as output but when the code is complete I get decimal ignored values.
Assuming your regional settings match the string data (i.e. comma is decimal separator), you can perform an explicit coercion:
ActiveCell.Offset(0, 5).Value = CDbl(va(i, 5))
I suspect this may be a job for VBA, which is beyond my abilities. But here's the scenario:
Column A in Sheet 1 (CAS1) contains x rows of text values
Column A in Sheet 2 (CAS2) contains x rows of text values
Part A - For each row value in CAS1, I need to know if the string is contained in any of the cells in CAS2. Not exact match, the string can be only part of the searched cells.
Part B - I need to know the cell value of each cell in CAS2 that contains the CAS1 value (if they do exist, they can be listed in the cells adjacent to the cell being searched in CAS1).
I've tried the following to attempt Part A, all to no avail:
vlookup(A1,sheet2!A:A,1,false)
NOT(ISNA(MATCH(A1,sheet2!A:A,0)))
ISNUMBER(MATCH(A1,sheet2!A:A,0))
COUNTIF(sheet2!A:A,A1)>0
IF(ISERROR(MATCH(A1,sheet2!A:A, 0)), "No Match", "Match")
I know some of the cell values in CAS2 contain the cell values in CAS1, so I don't know why they return false or No Match. I suspect it may be down to the nature of the text content. So here's some sample data:
CAS1
LQ056
RV007H
RV008
RV009H
TSN304
TSN305
CAS2
RV009-satin-nickel-CO.jpg
STR314.jpg
STR315.jpg
HCY001.jpg
RV008-oval-rad-CO.jpg
HCY001-BRAC006.jpg
Any help would be appreciated.
This problem can be faced through VBA (at least, I imagine the VBA solution much more easily than the possible Excel one). You need a macro that, for each row in CAS1, search the content in each row of CAS2 and returns you the address.
For Each cell In Sheets("CAS1").Range("A1:A" & Sheets("CAS1").Range("A1").End(xlDown).Row) '<-- check each cell of the range A1:A? of sheet CAS1 (adapt "A" and "1" if they're different)
recFound = 0 '<-- count how many findings there are
For Each cell2 In Sheets("CAS2").Range("A1:A" & Sheets("CAS2").Range("A1").End(xlDown).Row) '<-- check in each cell of the range A1:A? of sheet CAS2 (adapt "A" and "1" if they're different)
If InStr(cell2.Value, cell.Value) <> 0 Then '<-- if the value in cell is contained in the value in cell2..
recFound = recFound + 1 '<-- account the new finding
cell.Offset(0, recFound) = Split(cell2.Address, "$")(1) & Split(cell2.Address, "$")(2) '<--write the address on the right of the currently searched cell
End If
Next cell2
Next cell
All the above should be enclosed in a macro, e.g. Sub makeMySearch(), that should be run to get the results. As commented in my code, I'm assuming that data are in A1:A? of both sheets; but they of course might be, for example, in B5:B? of the sheet 1 and in C7:C? of the sheet 2. You need clearly to adapt the code to your current data.
There's no need for VBA. Some simple array-formulas can do the job.
To see if the entry in CAS1 is present in CAS2:
=OR(ISNUMBER(SEARCH(A2,CAS2_)))
will return TRUE or FALSE. BUT this formula has to be entered by holding down CTRL-SHIFT while hitting ENTER If you do this correctly, Excel will place braces {...} around the formula that you can see in the formula bar.
The SEARCH function returns an array of results, which will be either the #VALUE! error, or a number.
In order to return the address, the following array-formula can be entered adjacent to a cell in CAS1:
=IFERROR(ADDRESS(LARGE(ISNUMBER(SEARCH($A2,CAS2_))*ROW(CAS2_),COLUMNS($A:A)),1),"")
Fill right for the maximum number of addresses possible, then select the group and fill down.
In this case, the array being returned is a string of either 0's, or 1 * the row number (i.e. the row number). I assumend the data in CAS2 was in column A, but you can change the column number if needed (or even compute it if necessary, by replacing the 1 in the ADDRESS function with COLUMN(CAS2_))
CAS1_ and CAS2_ are either named ranges, or absolute range references to the two text groups.
I have a code that creates a number of separate lists under each other. For each of these lists I'm running a for loop to assign an equation to each row at the end column. This formula should multiply the neighbour cell (a relative reference) with cell at the top of that particular list (an absolute referance). The problem is that the lists are of arbitrary length and generated earlier in the code, so I can't assign the absolute referance beforhand.
I was thinking on saving the row number (row 1 = 1, row 2 = 2 etc) in a variable and then use the variable in the R1C1 notation (= "R(variable)C5*RC[-1]), but I cant seem to get this to work... The variable will be the same throughout the for loop given in the example below, but will change next time the same for loop is entered.
Is this even possible?
(I know the parantheses in the R1C1 are not the proper notation, but this is to show where I want my variable)
...
variable = 3
For i = 1 to count
last = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row + 1
Cells(siste, "E").FormulaR1C1 = "=R(variable)C5*RC[-1]"
Next
Just one small change:
Cells(siste, "E").FormulaR1C1 = "=R" & variable & "C5*RC[-1]"
Just had a question about my following code:
For Each c In "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
If Not String.IsNullOrWhiteSpace(exsheet.Range((c & "5")).Value.ToString) Then
pointer = pointer + 1
XRisks(pointer) = exsheet.Range(c & "6").Value.ToString
End If
Next
In essence, the code looks through the 5th row of each column in an excel spreadsheet(called exsheet). If there is something in that cell, it saves it into the array (XRisks).
However, my excel spreadsheet goes past Z and continues to AA AB AC AD etc in the column headings, which means, with the code I have at the moment, it stops reading columns after column Z.
I tried adding & "AA" & "AB" & "AC" etc to the end of my alphabet string but all this did was saved A again, then A again, then A again, then B again as it is just looking at each character in my string.
Hope I have described that enough. Thanks in advance!
Joe
You can simply use .Cells instead of .Range with an index rather than letters.
For i As Integer = 1 To 50
If Not String.IsNullOrWhiteSpace(exsheet.Cells(5, i).Value.ToString) Then
...
Next
To iterate over all columns, you can try usedRange. This may be insecure, however, since the usedRange does not necessarily start on the first column if that is empty (that said, with most table-like sheets it is safe enough).
For i As Integer = 1 To exsheet.UsedRange.Columns.Count
If Not String.IsNullOrWhiteSpace(exsheet.Cells(5, i).Value.ToString) Then
...
Next