How do I pass a range in my UDF for calculating range size? - range

Coulds someone do me the favor of correcting the coding of my user defined function? I'm simply trying to measure the dimensions of a range specified by the user. I'm having trouble passing the range.
Function PageSize(MyArea As Range) As String
Dim r As Range
Application.Volatile
Set r = Range("MyArea") ' did not work without quotes
Debug.Print r.Width
Debug.Print r.Height
PageSize = r.Width & " x " & r.Height
End Function
Thank you. The examples I am seeing are far more complicated than what I believe I need.

I changed
Set r = Range("MyArea")
to
Set r = Range(MyArea.Address)
and it worked.

Related

Replace text with field in MS Word

I'd like to replace every instance of
Figure X
with the correct field:
Figure 1
equivalent to
Figure { SEQ Figure * ARABIC }
Can I do this natively or do I need a macro? How can I do such a macro?
This might help:
Sub InsertSeqNo()
Dim Rng As Range
Set Rng = ActiveDocument.Range
Do While Rng.Find.Execute(FindText:="Figure X", Forward:=True, Format:=False, Wrap:=wdFindStop) = True
Rng.MoveStart Unit:=wdCharacter, Count:=7
Rng.Fields.Add Range:=Rng, Type:=wdFieldEmpty, Text:="SEQ Fig \* ARABIC", PreserveFormatting:=True
Loop
ActiveDocument.Fields.Update
End Sub

how to pass cellrange to a user defined macro paramenter

i would like to work with cellranges within my macro.
Function SumIfColor(SumRange)
Dim oRange as object
Dim oSheet as object
' Get Access to the Active Spreadsheet
oSheet = ThisComponent.CurrentController.ActiveSheet
' Get access to the Range listed in Sum Range
oRange = oSheet.getCellRangeByName(SumRange).RangeAddress
End Function
The question is how can I call this function with real cellRange object instead of String. Because getCellRangeByName works only with String variable.
Because when I call the function like this
sumifcolor(B1:B3)
I got the following error:
"Object variable not set"
I read some hint here but it did not helped me.
It is not possible to pass an actual CellRange object. One solution is to pass the row and column number, similar to the second part of #Axel Richter's answer in the link:
Function SumIfColor(lcol1, lrow1, lcol2, lrow2)
sum = 0
oCellRange = ThisComponent.CurrentController.ActiveSheet.getCellRangeByPosition(_
lcol1-1,lrow1-1,lcol2-1,lrow2-1)
For lCol = 0 To oCellRange.Columns.Count -1
For lRow = 0 To oCellRange.Rows.Count -1
oCell = oCellRange.getCellByPosition(lCol, lRow)
If oCell.CellBackColor > -1 Then
sum = sum + oCell.Value
End If
Next
Next
SumIfColor = sum
End Function
To call it:
=SUMIFCOLOR(COLUMN(B1:B3),ROW(B1),COLUMN(B3),ROW(B3))
The sum will be recalculated whenever a value in the range B1:B3 is changed, because of COLUMN(B1:B3). However, apparently changing only the color of a cell does not cause it to be recalculated.

How to Give int-string-int Input as Parameter for Matlab's Matrix?

I would like to have short-hand form about many parameters which I just need to keep fixed in Matlab 2016a because I need them in many places, causing many errors in managing them separately.
Code where the signal is 15x60x3 in dimensions
signal( 1:1 + windowWidth/4, 1:1 + windowWidth,: );
Its pseudocode
videoParams = 1:1 + windowWidth/4, 1:1 + windowWidth,: ;
signal( videoParams );
where you cannot write videoParams as string but should I think write ":" as string and everything else as integers.
There should be some way to do the pseudocode.
Output of 1:size(signal,3) is 3 so it gives 1:3. I do not get it how this would replace : in the pseudocode.
Extension for horcler's code as function
function videoParams = fix(k, windowWidth)
videoParams = {k:k + windowWidth/4, k:k + windowWidth};
end
Test call signal( fix(1,windowWidth){:}, : ) but still unsuccessful giving the error
()-indexing must appear last in an index expression.
so I am not sure if such a function is possible.
How can you make such a int-string-int input for the matrix?
This can be accomplished via comma-separated lists:
signal = rand(15,60,3); % Create random data
windowWidth = 2;
videoParams = {1:1+windowWidth/4, 1:1+windowWidth, 1:size(signal,3)};
Then use the comma-separated list as such:
signal(videoParams{:})
which is equivalent to
signal(1:1+windowWidth/4, 1:1+windowWidth, 1:size(signal,3))
or
signal(1:1+windowWidth/4, 1:1+windowWidth, :)
The colon operator by itself is shorthand for the entirety of a dimension. However, it is only applicable in a direct context. The following is meaningless (and invalid code) as the enclosing cell has no defined size for its third element:
videoParams = {1:1+windowWidth/4, 1:1+windowWidth, :};
To work around this, you could of course use:
videoParams = {1:1+windowWidth/4, 1:1+windowWidth};
signal(videoParams{:},:)

Create a CoffeeScript range with a length instead an endpoint?

I want to create a CoffeeScript range (like [4...496]) but using a length instead of an end range. This can be done with a loop like
myNum = getBigNumber()
newArray = ( n + myNum for n in [0...50] )
but I'm wondering if there is range-related shortcut that I'm missing. Is there something like
[getBigNumber()...].length(50) available in CoffeeScript?
You can just do
range = [myNum...myNum + 50]
Edit: As mu points out in the comments, CoffeeScript will add some complexity whether you use the snippet above or the original code. If performance is an issue, it might be better to drop down to plain JS for the loop (using backticks in the CoffeeScript code).
Assuming you want an ascending (i.e. low to high) range, you can do:
myNum = getBigNumber()
length = 50
range = new Array length
i = 0
`for(; i < length ; i++) { range[i] = i + myNum }` # raw, escaped JS
It's a lot faster than CoffeeScript's way of doing things, but note that CoffeeScript's range syntax also supports creating descending ranges by just flipping the boundary values. So CoffeeScript is (as always) easier on the eyes and simpler to work with, but raw JS is 3.5x faster in my test.

How to increase observation value by one?

I use the following code to find a specific entry in my dataset and set this value to obsNum:
originalSet(strcmp(originalSet.ABC, subset.DEF{row}) & strcmp(originalSet.FGH, task),21) = dataset(obsNum);
However, I would like to increase the current value by obsNum instead of setting it to obsNum, but I can't find a syntax for it. In Java I would simply write +=, but how can I do that in Matlab?
There is no Matlab syntax support for the += operator, you'll need to index into the variable twice:
idx = strcmp(originalSet.ABC, subset.DEF{row}) & strcmp(originalSet.FGH, task);
originalSet(idx,21) = originalSet(idx,21) + dataset(obsNum);