VBA eats my zeros INSIDE a string? - string

So here is a problem that I have never come across before. I Import an ISIN (for example DE0002635307) from a cell, which is defined as a text. I need to use this to reference to a cell by that name. So:
sub ISINWriter
dim ISIN as String
ISIN = ThisWorkbook.Sheets(i).Cells(j, 4).Value()
ThisWorkbook.Sheets(i+1).Cells(f, 4).Formula = "=" & ISIN
End Sub
For most of the ISINs this works fine, except if there are 4 or more zeros in a row. If that happens - for example FR0000120073 - it writes "=FR120073" into the cell. It just eats the zeros INSIDE the string! Any ideas?
I use Excel 2010 and Windows 7.
Thanks a lot.

If you use the Name Manager to try to create a named range (or more accurately a named formula) called "FR0000120073" then you should find that you get an error. Something like (from Excel 2007 on my machine):
The name that you entered is not valid.
Reasons for this can include:
- The name does not begin with a letter or an underscore
- The name contains a space or other invalid characters
- The name conflicts with an Excel built-in name or the name of another object in the workbook
The clue is in the last part of the third reason. FR120073 is a valid cell address in these days of 16Kx1m cell worksheets.
The first reason above may be useful, though: _FR0000120073 is a valid name. Could you use that?

EDIT: Removing my last answer so this makes sense.
Basically your cell references are an AlphaNumeric value. Take A1 for example A is the column 1 is the row. This issue is occuring because 0001 is the same as 1. so a reference to cell A001 is going to be the same as cell A1.
It looks like Excel has some built in functionality to remove leading 0's from your references to cells.

Related

Excel data set formatting issues

I have a client data set I am struggling to organise. I was previously helped on here with a question on how to split a cell with words that had been formatted with no space. This worked successfully for the names of clients but now my data is a little more tricky in regards to their job title and company name as the variables are so different for each cell.
For example;
Cell 1 - First Name
Cell 2 - Last Name
Cell 3 - ManagerCompanyName
or
Cell 3 - Principle ManagerCompany Name
My problem is with cell 3 and the spacing. I would like it to be as:
Cell 3 - Manager
Cell 4 - Company Name
Is there any way of doing this without manually adding the spaces?
The code for seperating first and last name was:
=SMALL(IFERROR(FIND({"A","B","C","D","E","F","G","H","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"},A2,2),""),1)
and then
=MID(F2, (G2), 60)
I presume it is similar, however the inconsistancy is making me struggle.
Thank you
=LEFT(A2,IFERROR(SMALL(FIND(CHAR(65+SEQUENCE(26,,0)),A2,2),1)-1,LEN(A2)))
Can be used to get the first word up to the next capital.
=IFERROR(MID(A2,SMALL(IFERROR(FIND(CHAR(65+SEQUENCE(26,,0)),A2,2),""),1),LEN(A2)),"")
To get the remainder from the 2nd found capital.
You can replace the CHAR(65+SEQUENCE()) part with the array you used if you don't have Office 365

Excel Dynamic Hyperkink using Cell Value as part of Address

I have a column of part numbers which I would like to associate with a column of links to their corresponding autocad drawings. The drawings are on a server, I would like to be able to insert the part number into the file path.
My attempt:
=HYPERLINK(\\servername\folderofparts\partprefix\['Lists for Calculations'!B8].dwg","Link")
where cell B8 within the sheet Lists for Calculations contains the name of the part.
try,
=HYPERLINK("\\servername\folderofparts\partprefix\" & 'Lists for Calculations'!B8 & ".dwg", "Link")
tbh, it's a little unclear whether the square brackets were intended to be part of the filename or not. I removed them above.

Search cell for text and add if not found

I have a product spreadsheet that contains a row with file names of images. Some have the file extension and some don't. I am trying to figure out a way to avoid adding ".jpg" to an entire column of cells in my excel worksheet. Is there a method that can help me search to see if the specified filename in the cell is already ending with .jpg? At the same time is it possible to add .jpg if its not there? I have tried with Concatenate and IF statement, but I am not having any luck.
Is there a function or method that can search if my cell contains the file extension(.jpg to be specific) and if nothing specified, is it possible to add one such as ".jpg"?
I created the following spreadsheet, as you can see some of the values have the extension ".jpg" and some don't
A
1 one.jpg
2 two
3 three.jpg
4 four.jpg
5 five
6 six
7 seven.jpg
8 eight
9 nine.jpg
I am assuming that you meant you had a spreadsheet similar to the sample I typed here, with more entries
I google for a way to read the last 4 characters of a cell in excel, this gave me the =RIGHT(A1, 4), this means that wherever you type this formula you will get the last four characters of A1
So using IF + RIGHT + CONCATENATE I was able to read the values in the A column
If the last 4 characters in the A1 are the same as the string ".jpg" then just give me back the value of A1, if the last 4 character of A1 are NOT the same as the string ".jpg" then give me back the concatenation of A1 and the string ".jpg"
the formula is as follows
=IF(RIGHT(A1,4)=".jpg",A1,CONCATENATE(A1,".jpg"))
instead of using the string ".jpg" you can type the string in a cell and use the cell number.
I have this formula in column E, I first wrote in E1, I then copied it to the rest of the cells in column E, this means that a copy of column A is being created, I do not know if the formula can be changed so that the formula searches the cell/column where it is written.
I hope this is of some help, and if you see any errors, or know of a better way to do this feel free to correct/share, as I myself am learning
Good luck
I'm assuming you want to solve this using worksheet functions rather than VBA code, I would use some variation of the following:
=IF(RIGHT(A1, 4)=".jpg", A1, A1&".jpg")
This formula looks at cell A1 and adds .jpg to it if it doesn't already end in .jpg
I can't guess how to integrate it into your sheet but should give you a good start

How to get sheet name using formula excel 2007

I am trying to get excel 2007 sheet name in a cell of that sheet using formula. I tried to Google and found this:
=MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,255)
But that does not work for excel 2007.
CELL("filename";A1)
Works but it gets the whole path.
My question is is there a way to get only a sheet name?
You can use the following formula:
=RIGHT(CELL("filename"),LEN(CELL("filename"))-FIND("]",CELL("filename")))
* Workbook must be saved first!
The formula is doing this:
RIGHT will return the characters after a numerical amount of characters in a string.
LEN returns the number of characters in a string.
FIND returns a number based on where a string is found within a string.
So all in all, the formula is saying take away the place that the first found ] is within the filename from the total length of the filename. We then just need the right hand portion of the filename after the numerical value we've just calculated.
You can "translate" 2010 formula to 2007:
=MID(CELL("filename";A1);FIND("]";CELL("filename";A1);1)+1;LEN(CELL("filename";A1)))
You may thing shorter way but that one works well.

How to use relative names in Excel VBA

Many "advanced" (aka: VBA) excel tutorials on the web or even excel's vba help encurage us to use the
Range("B2:B10")
method (to be precise: object) for selecting cells or getting values. In the same place they often add it's totally ok to use predefined names as well:
Range("valuesabove")
On the other hand I fell in love with the incredible power of relatively defined cell names. They make it so much easier to write and handle big composite formulas, and basically to refer to nearly anything.
However, relative names don't work in the Range("valuesabove") method the way we are used to it.
Usually (when used on the worksheet) relative names are relative to the currently selected cell or to the cell in which they are used.
In VBA's Range() object this is not true. Range is relative to a WorkSheet object, by default to the ActiveSheet. But ActiveSheet is represenetd by its leftupper cell, A1. And this is what Range turns out to be relative to. And this is why absolute names ($C$23) do work with it, and relative ones ("one column to the left, two rows up") don't.
So my question is:
How can I harness the power of relative names in VBA then?
EDIT:
Realising that my question was rather unclear (thx's go to you guys commenting tirelessly) let me try to put it in a specific form and clarify terms:
IMHO on an excel worksheet it is very comfortable to use names in order to refer to cells or define calculated values by functions based on cell values.
In excel a reference to a cell can be either relative, absolute, or mixed. This is true also when creating names. Thus we can speak about absolute, relative or mixed names (in terms of referring of course).
Here an absolute name is used a couple times (created using excel's Trace Dependents function):
Name "name" = $D$2
A relative name is used a couple times here:
Name "upright24" while, e.g. cell A7 is selected = C3 (without $ signs!). But this changes constantly according to the selected cell or region. You can check it in the name manager! (Ctrl+F3)
And this is what we can consider as a mixed name:
Name "rel_serialnumber" while, e.g. cell C6 is selected = $B6. The row of which (6) changes constantly according to the selected cell or region.
The creation of a relative or a mixed name is explicitly based on the active cell at the moment of creating the name. The creation of an absolute name naturally doesn't rely on the cursor position.
Note, that
absolute names mean a dinamic offset from the referenced cell, which is one and only
relative names mean a static offset from the referenced cell, which thus changes always corresponding to the place where the name is used
mixed names mean a mixed (or half-dynamic) offset from the referenced cell, the row or column of which thus changes always corresponding to the place where the name is used while the other remains always the same (the offset in one or the other direction remains zero).
Okay, now here is the thing. I have a database-like excel sheet where I handle the rows like records and the columns as fields for properties. The user uses this thing as follows: he "selects a record" by placing the cursor in any cell of the row of the desired record. Then he presses a big command button which starts my VBA macro. This intends to open a prepared skeleton file and fill some specific cells in it (which are btw defined by absolute names) with some values (which are defined by mixed names) from the selected record.
Since Range("name") is considered ok to use in VBA (see above) I thought Range("relativename") or Range("mixedname") will work just as fine while automatically relying on the active cell.
I couldn't be worse.
Only Range("absolutename") works in the way one would expect! Explanation see above.
So I'm after a function / method / object that is possibly as comfortable to use with a "relativename" or a "mixedname" as Range("absolutename") is.
It appears you are looking for Range.Offset() http://msdn.microsoft.com/en-us/library/office/ff840060%28v=office.15%29.aspx
However you could do it as:
'Your example Range(Col_B_in_current_row) as
Range("B" & ActiveCell.Row).Select
'Your example Range("B2:B10") -> Range("valuesabove") as
Range("B2:B10").Offset(-1, 0).Select
Just seems like a relatively simple syntax already exists for this.
I think I've found a proper and compact solution. It's
Names("mixedname").RefersToRange
Not as short as Range("mixedname") would be but it is really providing the expected values.
UPDATE:
This solution is mostly unuseful if you want to copy relative-named cell values in a source workbook to relative-named cells in a dest workbook with a single codeline. This is because Names() relies on the actual position of the cursor which is depending on which workbook is currently the active one and in most cases this won't be ok for the other.
In this case the non-fixed part of the name has to be stored:
sourcerow = ActiveCell.Row
[...]
'opening a wbk, this also makes it the active one
[...]
Names("dest").RefersToRange = mysheet.Cells(sourcerow, mybook.Names("src").RefersToRange.Column)
To reference a Range relative to another Range you can use this syntax:
myRange.Range("namedRange")
Note: This only works if both the Row offset AND the Column offsets are positive. For example if the "Refers to" formula for the named range is "=Offset(A1,r,c)", then the above syntax will throw an error if Either r Or c is negative. But, it will work if both are positive.
The asymmetry is unfortunate but business as usual for VBA...
To Reference the third column in the row of the current ActiveCell:
ActiveCell.EntireRow.Range("C1")
To reference a cell offset by (for example) 1 row and 3 columns relative to the ActiveCell:
ActiveCell.Range("C2")
Obviously, you can use the same syntax with the Selection Object or any other Range value in VBA.
Private Sub Worksheet_Change(ByVal Target as Range)
If Not Intersect(Target.Address,ThisWorkbook.Sheets('sheetname).Range('RangeName)) Is Nothing Then _
'Do whatever you want down here.
ThisWorbook.Sheets('sheetname).Range('RangeName).Offset(0,Target.Row)
End If
End Sub
This should send you on the right path to what you want (which is super unclear). Use the worksheet change event to bring in user worksheet selections and changes into VBA modules. Put it into the relevant sheet.
I had the same problem, but I did get it to work - sort of. I don't know what is different about the simple example below, but it works. At first I thought selection mattered, but no - it works without changing the active cell.
(I still can't get it to work in my main spreadsheet.)
Named range: "TestName" = Sheet1!$H1
Values in H1:H10 = 1,2,3,4,5,6,7,8,9,10
Sub Test()
Dim x As Integer
For x = 0 To 10
Range("A1").Offset(x, 0).Value = Range("A1").Offset(x, 0).Range("Testname").Value
Next x
End Sub
Result: A1:A10 = 1,2,3,4,5,6,7,8,9,10

Resources