Find and copy certain values in cells - vba

I have a column with filenames of the following format:
somenumber_sometext_1_100_AA
These filenames are in column A. I need to scan each filename in each cell and copy the number 1 and paste it into column B and copy the number 100 and paste it in column C, all of the same row as the filename. I then want to repeat for the next cell down in column A.
Help would be appreciated!

As this was tagged with VBA, I will give a simple vba macro solution.
The easiest way is to define a VBA function called splitText(test,delim, n) , which will split text by a delimiter, and get the nth column. This is so commonly needed, I wish they'd just make it standard.
Function splitText(txt As String, delim As String, n As Integer)
splitText = split(txt, delim)(n)
End Function
Then, if the string is A1, put this in B1:
=splitText(A1,"_",2)
And this in C1:
=splitText(A1,"_",3)

With:
3141543_junktext_4563_10098_ZZ
in cell A1, in B1 enter:
=TRIM(MID(SUBSTITUTE($A1,"_",REPT(" ",999)),COLUMNS($A:C)*999-998,999))
and in C1 enter:
=TRIM(MID(SUBSTITUTE($A1,"_",REPT(" ",999)),COLUMNS($A:D)*999-998,999))
To display 4563 and 10098

If each section is delimited (ie in the example by _) then Text to Columns with that as the delimiter and then deleting the last and the first two columns may be convenient.
(But copy into a new column first or you will lose your source data.)

Related

Char limit on vlookup AND index/match combination

I created a table/sheet of items consolidated from different lists. All items of the different lists are added and then duplicates are deleted. Then i add a empty column for each of the single lists after the first column. I used vlookup to write "X" in every row of the corresponding column if the item is in this specific list. Each list has a own sheet where i previously wrote an extra column with X in every row, so vlookup can output the 2nd column containing just the x's. At the end i get a table of which list has the item, q
The formula i used: =Iferror(vlookup(A2;'sheetListA'!A:B;2;false);" ")
Apparently vlookup stops working if the looked up value contains more than 255 characters (across different sheets). I now tried for quite a while to rebuild my formula with INDEX and MATCH but without success. I managed to make a working formula but it has the same problem as vlookup and stops working when there are more than 255 chars in the cell.
Another formula not working: =INDEX('630'!A:B;MATCH(A02;'sheetListA'!A:A;0);2)
Rory aswered my question in the comments:
Try: =LOOKUP(2;1/(A2='sheetListA'!A2:A1000);'sheetListA'!B2:B1000) Do
not use entire column references
. – Rory Aug 16 at 12:56
Change sheetListA to whatever your sheet name actually is.
Rory Aug 16 at 14:50
Here is a custom VLOOKUP like function that can search characters over 255 characters.
Function VLOOKUPPLUS(SearchCell, LookUpArray As Range, ColNum As Long) As Variant
For Each cell In LookUpArray.Columns(1).Cells
If cell = SearchCell Then
VLOOKUPPLUS = cell.Offset(0, ColNum - 1)
Exit For
End If
VLOOKUPPLUS = "N/A"
Next
End Function

Copy a number starting with 0 in vba and paste is as such

say cell A1, A2, A3 contains value "00V", cell An contains value "029"
I'm doing a comparison for consecutive cells in column A like:
If A1 not equal to A2 then ill copy the cell value of A2 and paste it into a new worksheet in Column A first consecutively.
When I compare 00V and 029 (i.e. both are unequal) ill copy 029 and paste into new sheet
But "029" gets pasted as "29"
How do I fix this in Excel vba?
You could use Format(val, "000") where val is the value of whatever you're copying.
Or you could use rng.NumberFormat = "000" where rng is the destination range of where you're copying the values to.
Convert the target column format to text and then use paste special values. You'll be able to preserve the zeros.
The easiest way is to add a apostrophe (') in front of the values. When copied, the 0 will be maintained.

Searching and comparing various values using VBA

I have to create a macro which will:
Get the value from the cell A1 search this value in column C.
If the value in cell A1 exists in column C, the macro needs to be compare the value in cell B1 with values in column D.
If the value in cell A1 exists in column C AND the value in cell B1 exists in column D, then the text "Values found" should appear in cell E1.
The above needs to happen for all non empty rows in column A.
I was trying to use the following formula:
=IF(ISERROR(MATCH(A2,$C$2:$C$138,0)),"Load number not found","Load number found")
But it not working as I want. I have limited access to internet so I can't check all web sites. Would you please help me. Thanks a lot.
To check if A1 is in column C and if B1 is in column D (in the same row A1 was found in column C), then you need to do the following:
=IF(ISERROR(MATCH(A1,$C:$C,0)),"Load number not found",IF(B1=INDEX($D:$D,MATCH(A1,$C:$C,0),1),"Load number found","Load number not found"))
The first IF checks if A1 is in column C, the second IF checks if B1 is in column D (in the same row A1 was found in column C)
It will return "Load number found" only if both conditions are true. Otherwise it will return "Load number not found".
You can write a macro to do the same thing. But the easier way is to lock the cells in column E only and protect the sheet so that users will not accidentally change any of the formulas.
Update:
Since Column C can have duplicates, need to use the following array formula:
=IF(ISERROR(MATCH(1,(A1=$C:$C)*(B1=$D:$D),0)),"Load number not found","Load number found")
When you paste this formula to E1, make sure to press CTRL + Shift + Enter instead of just pressing the Enter key.
If I understand, a conventional solution with formulae is to concatenate your C and D column data and then search that. If you insert a new columnC with:
=D2&E2
copied down to suit you could apply (but say in ColumnF rather than ColumnE) your existing formula with only slight modification:
=IF(ISERROR(MATCH(A1&B1,$C$2:$C$138,0)),"Load number not found","Load number found")
subject to quite what is in which row.

Excel VBA - selecting the range from first row to the last row

I have a problem with VBA code. I have a sheet with a lot of data around the cells I want. I need to select data in column F, but the position of the first and last cells between the range is to be selected is changing up and down. I created the non empty cells in row X where there is no data do the LastRow function has any refernece but now I dont know how to select the first row
thx for help
If F1 is empty, this selects the first cell with data in the F column
Worksheets("Sheet1").Range("F1").End(xlDown).Select
If F1 may be non-empty you can add a check to see whether it contains text.

Auto Fill Row B with the last four characters of Row A

So basicly i want a VBA script to fill Row B with the last four characters that are in Row A
RowA contains a telephone number with around 12 numbers in it.
Assuming that you meant to say
I have a series of telephone numbers in column A. I would like to
create a second column in which I have just the last four digits of
these numbers. I am new to Excel. Could someone please help me get
started on this?"
The answer would go like this:
In Excel you can create formulas that compute "something" - often based on the contents of other cells. For your specific situation, there is a function called RIGHT(object, length) which takes two arguments:
object = a string (or a reference to a string)
length = the number of characters (starting from the right) that you want.
You can see this for yourself by typing the following in a cell:
=RIGHT("hello world", 5)
When you hit <enter>, you will see that the cell shows the value world.
You can extend this concept by using a cell reference rather than a fixed string. Imagine you have "hello world" in cell A1. Now you can put the following in cell B1:
=RIGHT(A1, 5)
and you will see the value "world" in B1.
Now here is the cool trick. Assume you have a bunch of numbers in column A (say starting at row 2, since row 1 contains some header information - the title of the column). Then you can write the following in cell B2:
=RIGHT(A2, 4)
to get the last four digits. Now select that cell, and double-click on the little box in the bottom right hand corner:
Like magic, Excel figures out "you want to do this with all the cells in this column, for as many rows as there is data in Column A. I can do that!" - and your formula will propagate to all cells in column B, with the row number adjusted (so in row 3, the formula will be
=RIGHT(A3, 4)
etc.
Try
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
With ws.Range("B2:B99")
.Formula = "=Right(A2, 4)"
.Value = .Value
End With