Get Cell Content in one sheet to another - excel

I need to get my cell content in excel worksheet to another worksheet by using a user defined function. So I searched stackoverflow and found the following vba code.
Function GetValue(sheetName As String, cellAddress As String) As Variant
GetSheetValue = ThisWorkbook.Sheets(sheetName).Range(cellAddress)
End Function
This is the right thing i expected.But it doesn't work for me. I need to develop a user defined function as like follows.
=GetContent(Sheet1,B6)
I've tried many things but I can't do it. So I'm very appriciate any responses about this matter. (Provide Screenshots about my example)
Thank You
ScreenShot 02
ScreenShot 03

I've found the answer. My function must be correct as following code.
Function GetContent(sheetName As String, cellAddress As String) As String
GetContent = ActiveWorkbook.Sheets(sheetName).Range(cellAddress)
End Function
I didn't concern abot the function name and return as the same. But we have to must do it. Also in string data type we have use quotaions and Case sensitive. So thanks all of you for valuable help. It helps me to fid out the answer. Now wecan use the functions as follows.
=GetContent("Sheet2","b5")
Thank You!

Related

How to express a Double in Shape.SetFormula?

I am trying to input a Double value into a Shape.SetFormula command
for example the following is something which I recorded via the journaling function of NX:
moveObjectBuilder1.TransformMotion.Angle.SetFormula("12.5")
According to the microsoft website, the value inside the () can only be an Integer, and obviously, any value (no matter Integer or Double value) written in this form "xxx" can be executed.
As far as I understood, "" is a String, so I changed the code like the following :
Function value() As String
Return 25/2
End Function
Sub Main(ByVal As String)
.
.
.
moveObjectBuilder1.TransformMotion.Angle.SetFormula(value())
.
.
.
End Sub
However, there will be a syntax error if the code is written like this.
May I ask, how can I let the Shape.Formula() command read a Double value? Or how can I let the Function return a value which will be in this format "..."?
Thank you very much!
I have no experience with that application but, as far as I can tell, the parameter for that method is type String because it accepts a String containing a mathematical formula that will be parsed internally. In that case, something like this should work:
moveObjectBuilder1.TransformMotion.Angle.SetFormula("25 / 2")
and do exactly the same thing as this:
moveObjectBuilder1.TransformMotion.Angle.SetFormula("12.5")
If that's not working for you then please explain EXACTLY what happens when you do it.
I've managed to solve my own problem. It was actually quite stupid, I am just simply not an expert in programming. It only took me several more tries to get the solution to my problem.
moveObjectBuilder1.TransformMotion.Angle.SetFormula("Sqrt(2)")
This is the correct way to express "Rotate this object with the angle of square root of 2."
Okay now let's talk about the problem which I've encountered. What I want is, import a value via a function, and then process it. So, SetFormula only accepts String. Therefore I can do the following :
Function value() As Double
Return 25/2
End Function
Sub Main(ByVal arg() As String)
.
.
.
moveObjectBuilder1.TransformMotion.Angle.SetFormula(value().ToString)
.
.
.
End Sub

String.Split() method creates empty array spot

I am not a professional programmer, just FYI.
I am having trouble splitting a string. The resulting array has a length of 3, with the second spot (index 1) being completely empty.
I could manipulate the array to work the way I'd like, but I would rather understand why it is acting this way and code it properly from the beginning.
Dim defaultSetting() As String
Dim curSetting as String = "MENU_ITEM_ON_OPEN;;OPTIONAL_LEAVE"
defaultSetting = curSetting.Split(";;")
MsgBox(defaultSetting.Length) 'this is 3
MsgBox(defaultSetting(0)) 'this is as expected "MENU_ITEM_ON_OPEN"
MsgBox(defaultSetting(1)) 'this is empty and I do not know why
MsgBox(defaultSetting(2)) 'this is "OPTIONAL_LEAVE" and should be stored in defaultSetting(1)
Any help would be appreciated, thank you.
The problem here is that Option Strict is set to Off.
The overload of Split which is used expects a ParamArray of Char.
Because of this, the string ";;" is "silently" converted to a single char.
You can check this with following code:
Dim x As Char = ";;"
MsgBox(x)
You want to split by a string, which means you have to use another overload:
defaultSetting = curSetting.Split({";;"}, StringSplitOptions.None)
Thanks to a comment made by dbasnett I was able to find code that worked the way I was expecting, although I am not really sure why if anyone care to explain. But if not, this question has been answered, thanks.
defaultSetting = testString.Split(CType(";;", Char()), StringSplitOptions.RemoveEmptyEntries)

"Wrong Data Type" error for setting cell value [duplicate]

I am building a User Defined Function.
I get an error
A Value used in the formula is of the wrong data type
I am trying to build a function that adding one comment, will also add the comment to another location as the comments always come in pairs.
Public Function AddComments(vesselCell As Variant, shopCell As Variant, comment As String) As Variant
Range(vesselCell).AddComment (comment)
Worksheets("Shop").Range(shopCell).Value = comment
End Function
I have singled it out to the third line of code causing the problem.
The setup currently is the Vessel Cell will be a comment added to the sheet, and then the Shop sheet has a section for comments as a column.
Assuming vesselCell and shopCell are both Range objects, Range(vesselCell) and Worksheets("Shop").Range(shopCell) are part of the problem.
Make them Range, not Variant.
vesselCell.AddComment comment
shopCell.Value = comment
Now, this code isn't legal in a UDF. Make your procedure a Sub procedure (remove the As Variant return type), and invoke it from other VBA code, or attach it to a shape or button's OnAction property.
User-Defined Functions take inputs, compute a result, and then return that result to the calling cell: your code not returning anything is a strong indicator that a Function isn't appropriate here.

Simple custom function in Excel, using Vlookup

I want to have a faster process looking up cross references.
Right now I use VLOOKUP, and it works fine - but it takes time when it needs to be done multiple times everyday.
It is always the same sheet I use to lookup the cross references, so the only thing that changes is my input value in the VLOOKUP function.
Therefore I want a function where I only input 1 value to get the VLOOKUP value.
The idea is a function like:
=CROSS(ID)
where
CROSS = vlookup(ID, table_array, col_index_num,[range_lookup])
So the vlookup_value is replaced by ID.
I hope you can provide me with some answers - thanks in advance.
I have tried multiple different things, but with no success.
As I am new, I've googled and recorded macros to look for answers.
You could write a UDF (user defined function) for that, using the WorksheetFunction.VLookup method:
Option Explicit
Public Function CROSS(ID As Variant) As Variant
CROSS = Application.WorksheetFunction.VLookup(ID, table_array, col_index_num, range_lookup)
End Function
I got it working as it should now!
The code ended up like this:
Sub crossref()
Option Explicit
Public Function CROSS(ID As Variant) As Variant
CROSS = Application.WorksheetFunction.VLookup(ID, Worksheets("Sheet1").Range("E:F"), 2, 0)
End Function

Expected array when calling function

I'm trying to learn a little VBA an I'm in the process of saving ranges as CSV files.
However, I'm having trouble with this line of code
Call SaveRangeAsCSV(SelectDown("B5:D5"), "C:\Test\test.csv", True)
And here is a snippet of the SelectDown function.
Private Function SelectDown(Range As String) As Range
SelectDown = Range(Range, ActiveCell.End(xlDown)).Select
End Function
I get the error: Expected array. I cannot seem to understand what the problem is. Any help would be appreciated :-)
It sounds like the function SaveRangeAsCSV is expecting an array, but you are passing in a Range. This function is not a built-in Excel function, so I can't check it. Maybe you could check what arguments it is expecting?
My function now looks like this and is working perfectly.
Private Function SelectDown(RangeAddress As String) As Range
Set SelectDown = Range(RangeAddress, ActiveCell.End(xlDown))
End Function

Resources