How to get the number of the first row of a range in OpenOffice Calc (BASIC) - vba

So I have been converting a VBA code to OpenOffice BASIC and a simple task as getting the number of the first row of the range has been consuming hours of research.
in VBA:
Cells(3,2) or "mr" is a cell with an input of the kind "A5:G7", so first I splitted the string in a array with A5 and G7.
Rangesplit = Split(Cells(3, 2), ":")
FirstRow = Range(Rangesplit(0)).Row
So far in OpenOfficeBasic:
Rangesplit(mr,":")
I have not been able so far to determine the row and I want to know which function allows me to do it.

The following functions will probably help you:
Row_int = oCell.getCellAddress.Row
Col_int = oCell.getCell

Related

Using variable sheetname in VBA

I'm a novice at VBA (just starting to learn). In my userform I have a combobox with values from 1 to 12(not string) representing the months. I want the user to pick a month and based on that, the multiple listboxes and labels I have placed should get filled by the relevant values in one of the 12 sheets representing each month. as I am a novice I have a lot of problems here but for starters the following lines do not seem to work on userform_initiate()
For j = 0 To 1
arr_trh(0, j) = Sheets("Sheet6").Cells(4, j + 1)
Next j
I can get it to work for a single sheet by using
arr_trh(0, j) = Sheet6.Cells(4, j + 1)
However, what I'm trying to do later on is to create a string and somehow concatenate "Sheet" and combobox value to pass on to Sheets() function.
Any help would be appreciated
Thanks
Rather than referring to the Sheet object like:
v = Sheet1.Range("A1")
use:
v = Sheets(1).Range("A1")
which you can index like:
v = Sheets(i).Range("A1")
where i is a variable.
I guess, my this answer will help you to figure out how to refer to sheets. Also, it tells about caveats of Index property.

Excel Summing over Rows with for loop - Type mismatch error

so I am currently working on an Excel sheet where I have to calculate confidence intervals. Long story short, I think the only way I can do this automatically, is to write vba code. The first step would be to calculate the average of the cells in a column for several columns in the sheet. What I did:
Dim temp As Double
temp = 0
Dim it_row As Long
for it_row = 1 to 100
if IsBlank(Sheet.Cells(it_row,it_col)) then
temp = temp + 0
else
temp = temp + Sheet.Cells(it_row,it_col).Value
end if
next it_row
Dim Average As Double
Average = temp/100
'writing average in another cell
This code does not work, as the compiler returns Type missmatch, error code 13
in the line
temp = temp + Sheet.Cells(it_row,it_col).Value
I tried to do a CDouble(Sheet.Cells(it_row,it_col).Value) but that did not work.
Any help is appreciated, as I am quite desperate because googling did not really help me.
I should mention that I do have to use vba and this code because this is part of a bigger automated process and my supervisor said I must use vba for automation in the next step.
The Average and Sum Excel Functions ignore text, boolean, and empty values:
Average = Application.Average(Sheet.Cells(1, it_col).Resize(100))
Check
Sheet.Cells(it_row,it_col).Value
...with
if isnumeric(Sheet.Cells(it_row,it_col).Value)
...before adding it to a double type value. If that check fails, then you can chose to skip it or treat it as sth. else.
I would've added this as a comment, but I don't have enough rep. to add comments.

VBA Randomizing Function to change cell formula using variables, named ranges, and indirect functions

I've been searching and searching and can't find a proper way to do this. I'm using multiple variables and values to set the value of another cell.
I use this formula to accomplish what I need right now in cell "E4" on Sheet "Turn_Travel";
=LOOKUP(RAND(),INDEX(INDIRECT("Region"&VLOOKUP($B$4,RegionListTable,MATCH("Region #",RegionListHeader,0),FALSE)&$C$4&"Info"),0,MATCH("Perct.",INDIRECT($C$4&"Header"),0)),INDEX(INDIRECT("Region"&VLOOKUP($B$4,RegionListTable,MATCH("Region #",RegionListHeader,0),FALSE)&$C$4&"Info"),0,MATCH("Weather",INDIRECT($C$4&"Header"),0)))
Right now I just hit F9 and it refreshes my worksheet and generates a new Random number which changes my result.
I'm trying to use VBA to generate the Random number and then have the cell "E4" on Sheet "Turn_Travel" contain the above formula with the VBA variable for the Random number.
This was my attempt but I'm getting an error. Can someone spot where I'm going wrong?
Sub TravelRoll()
Randomize
Dim TheRoll As Long
TheRoll = ((1 - 0 + 1) * Rnd + 0)
Worksheets("Turn_Travel").Range("E4").Formula = "=LOOKUP("&TheRoll&",INDEX(INDIRECT(""Region""&VLOOKUP($B$4,RegionListTable,MATCH(""Region #"",RegionListHeader,0),FALSE)&$C$4&""Info""),0,MATCH(""Perct."",INDIRECT($C$4&""Header""),0)),INDEX(INDIRECT(""Region""&VLOOKUP($B$4,RegionListTable,MATCH(""Region #"",RegionListHeader,0),FALSE)&$C$4&""Info""),0,MATCH(""Weather"",INDIRECT($C$4&""Header""),0)))"
End Sub
You should have space before and after string concatenation operator (&).
Also you need to declare your variable as double to get numbers between 0 and 1. If you declare it as long you get 0 or 1.
You can use Rnd to have a random number between 0 and 1.
If you want to have limited decimals, for example 2 decimals, you can use Format(Rnd, "#0.00")

VBA - draw in numbers from one sheet and get data from another

I am trying to write a VBA to draw data from one sheet to another, but am stuck on something.
I only need some of the data in the original sheet (let's call it s1), in particular, I need data between two rows.
I have these rows written down in another sheet (s2), so I know exactly between which rows I need the data from. As you may expect there are multiple rows between which I need the data.
The problem is now that I am trying to write a VBA that is able to look up these rows in my row sheet (s2), and then goes to the sheet in which all my original data is contained (s1), and then draws out the data between the two rows into a third sheet (s3).
I have not been able to make it draw in the numbers from s2 (can't seem to work out how to tell it that it is these two rows between which I need the data, but from another sheet), and currently have to input the row numbers myself, which is really tedious, since the dataset is large!
Any help would be much appreciated!
Thank you!
Have you tried the .find option.
You could use something like this:
findrow = w1.Cells.Find(what:="Content", MatchCase:=False).Row
An more extensive example would help
Just break down your problem into smaller steps.
You can assign a value on a sheet using cell references:
Sheets("S1").Cells(iRow, iCol) = Value
'or
Sheets("S3").Cells(2, 10) = Sheets("S2").Cells(2, 10)
You can use Sheets("S1").Range("J" & iRow) or Range("J2").
If there are conditions that need to be met for something to happen, use If statements.
If (Value 1 < Value 2) Then
Sheets("S3").Cells(2, 10) = Sheets("S2").Cells(2, 10)
Else
Sheets("S3").Cells(2, 10) = Sheets("S1").Cells(2, 10)
End If
Without more information about the specific choices you are using to determine which rows to copy and from which sheet, it's hard to say. But you can do things in smaller batches or copy whole ranges, by looping.
Dim iRow As Integer
'This would copy all the data from Sheet("S1").Range("J1:J20) to Sheet "S3".
For iRow = 1 to 20
Sheets("S3").Cells(iRow, 10) = Sheets("S1").Cells(iRow, 10)
Next iRow
You could insert an If statement into each row, inside the loop to see if a certain criteria is met, and then decide which sheet in which to copy the data.

Macro to run through 3 conditions and provide value

This is my first time using VBA for Excel (I usually code Java and C++), and I was hoping to get some tips to start out.
I want to write a macro for a large data set that will proceed through the following list of conditions to provide a dollar result:
Collect unit size from column A (Possible values 0-8)
Determine whether single or family unit from Column B (Single- 1, Family- 0)
Collect utility code from Column C (code for type of product being assessed)
From this information, a new value will be placed in the row which determines utility costs by taking into account unit size, type of unit, and the product in question. I have thought about using nested Select Case or nested conditionals in a loop, but overall I am pretty lost.
It seems like a worksheet formula might do the trick, but it's hard to tell without knowing what the calculation is. Below is a user-defined function (UDF) that you would put in a standard module. You would call it from a cell like:
=computecosts(A2,B2,C2)
Obviously the code would change depending on how your data is laid out and what your calculation is.
Public Function ComputeCosts(rSize As Range, rFamily As Range, rCode As Range) As Double
Dim lSizeFactor As Long
Dim lFamilyFactor As Long
Dim dCodeFactor As Double
Dim rFound As Range
Const lFAMILY As Long = 0
'Size factor is a function of 0-8, namely adding 1
lSizeFactor = rSize.Value + 1
'Family factor is computed in code
If rFamily.Value = lFAMILY Then
lFamilyFactor = 3
Else
lFamilyFactor = 2
End If
'Code factor is looked up in a different sheet
Set rFound = Worksheets("Sheet2").Columns(1).Cells.Find(rCode.Value, , xlValues, xlWhole)
If Not rFound Is Nothing Then
dCodeFactor = rFound.Offset(0, 1).Value
End If
'do the math
ComputeCosts = lSizeFactor * lFamilyFactor * dCodeFactor
End Function
Thanks for the responses, they were helpful in understanding VBA for Excel. I just ended up putting possible values in a table and then using Match functions within an Index function to pick out the right value.