ExcelScript: getNumberFormat from column A to C - excel

I need to get the NumberFormat from an excel file (appr. 3000 lines) and I wrote a simple Script which copy it from column A to column C each row by row. I tried to use this script with Power Automate after 10 minutes.
I already set the timeout on 60 minutes, but it seems that my tenant configuration abort it always after exactly 10 minutes.
Here is my script:
function main(workbook: ExcelScript.Workbook) {
let sheet = workbook.getWorksheets()[0]
let range = sheet.getUsedRange(true);
let rows = range.getUsedRange(true).getRowCount();
let cols = range.getUsedRange(true).getColumnCount();
for (let row = 2; row <= rows; row++) {
sheet.getRange("C" + row).setValue(sheet.getRange("A" + row).getNumberFormat())
}
}
Is there an easier way to copy the NumberFormat from one column to another and does anybody know a better way?

Please make sure when you ask these questions to put the Office-Scripts tag since this is Office Scripts. You don't need to loop to achieve this. You can use getNumberFormats() like in the code below.
function main(workbook: ExcelScript.Workbook) {
let sheet = workbook.getWorksheets()[0];
let rowCount = sheet.getUsedRange(true).getRowCount();
let numFormats = sheet.getRange("A1:A" + rowCount).getNumberFormats();
let newRng = sheet.getRange("C1:C" + numFormats.length).setValues(numFormats);
}
EDIT 4/23/21
If you just want to get the indent levels, then you could just insert this formula in column C rather than getting the number formats.
=IFERROR(LET(indentLevel,FIND("[",FILTER(A:A,A:A<>""),1),SWITCH(indentLevel,4,1,6,2,8,3,10,4,12,5)),"Other Formula Here")
Here is the Office Scripts
function main(workbook: ExcelScript.Workbook) {
let sheet = workbook.getWorksheet("Sheet1");
sheet.getRange("C1")
.setFormulaLocal("=IFERROR(LET(indentLevel,FIND(\"[\",FILTER(A:A,A:A<>\"\"),1),SWITCH(indentLevel,4,1,6,2,8,3,10,4,12,5)),\"Other Formula Here\")");
}

Here is my VBA code where I use a for loop:
Sub GetHierachyParents()
Set appExcel = holeAnwendung("Excel.Application")
Set wbkExcel = appExcel.Workbooks.Open("C:\impport.xlsx")
Set wksExcel = wbkExcel.Sheets("Hierachy")
countRows = wksExcel.Range("A1").End(xlDown).Row
For i = 2 To countRows
Node = 0
If (Len(wksExcel.Range("A" & i).NumberFormat) - Len(Replace(wksExcel.Range("A" & i).NumberFormat, "[-]", "")) > 0) Then
IndentLevel = (Len(wksExcel.Range("A" & i).NumberFormat) - Len(Replace(wksExcel.Range("A" & i).NumberFormat, " ", "")) - 1) / 2
Node = -1
Else
IndentLevel = (Len(wksExcel.Range("A" & i).NumberFormat) - Len(Replace(wksExcel.Range("A" & i).NumberFormat, " ", "")) - 5) / 2
Node = 0
End If
sql = "INSERT INTO impTemp0 (F1, F2, F3, F4) VALUES ('" & wksExcel.Range("A" & i) & "', '" & Replace(wksExcel.Range("B" & i), "'", "") & "', " & IndentLevel & ", " & Node & ")"
CurrentDb.Execute sql
Next i
CurrentDb.Execute "UPDATE [impTemp0] SET Parent = DMAX('id','[impTemp0]','[impTemp0].id<' & [impTemp0].id & ' AND [impTemp0].Level=' & [impTemp0].Level-1 & '')"
End Sub
And here is an example for the first 8 rows:
" [-] "#
" [-] "#
" [-] "#
" [-] "#
" [-] "#
" "#

Related

If Statement in for loop VBA

Im tring to get my Function to skip words in my list that arent in my string but it doesnt seem to be working
Iv tried using VBA and excel native commands... however i get the same result
Public Function test(range, y)
step = y
For Each cell In range
If InStr(cell, step, vbTextCompare) <> 0 Then
step = Application.WorksheetFunction.Replace(step,Application.WorksheetFunction.Search(cell, step), Len(cell), UCase(cell))
test = step
End If
Next cell
End Function
When I try to use this Function in Excel I get #VALUE, I suspect this is because not all the values in the list are found in the string however that the reason for the if statement
first you have the variable backwards in the Instr. and you must use the firs criterion if you use the last.
Also if you are looking for complete words then you need to use " " on the search and replace.
Lastly use vba's Replace:
Public Function test(rng As range, y As String)
Dim step As String
step = y
Dim cell As range
For Each cell In rng
If InStr(1, " " & step & " ", " " & cell & " ", vbTextCompare) > 0 Then
step = Replace(" " & step & " ", " " & cell & " ", UCase(" " & cell & " "))
End If
Next cell
test = Application.Trim(step)
End Function

How to use Application.match?

I am new to Excel VBA.
I have these random columns:
Range("Y1").Value = "LITIGATE_PERSON_ADDRESS"
Range("Z1").Value = "LITIGATE_PERSON_CITY"
Range("AA1").Value = "LITIGATE_PERSON_TK"
Range("AB1").Value = "LITIGATE_PERSON_ADDRESS_TYPE"
Here's the part of code that is being used for these columns.
Worksheets("MAIN_CONTROL").Cells(i, 25).Value = sourceADR
sourceADR = Replace(sourceADR, "Attica", "")
'...................................................................
sourceADR = Replace(sourceADR, "-", " ")
sourceADR = Replace(sourceADR, " ", " ")
sourceADR = Trim(sourceADR)
auxC = sourceADR
Worksheets("MAIN_CONTROL").Cells(i, 26).Value = sourceADR
'..............................
If (Len(sourceADR) < 1) Then GoTo aseAddr
'..............
mainAddress = Split(sourceADR)
addrAA = ""
Worksheets("MAIN_CONTROL").Cells(i, 24).Value = Str(UBound(mainAddress)) & "##" & Str(LBound(mainAddress))
For jA = UBound(mainAddress) To LBound(mainAddress) Step -1
'......................................................
If (regex.Test(Trim(mainAddress(jA)))) Then
auxC = Replace(auxC, Trim(mainAddress(jA)), "")
destws.Range("BT" & i).Value = Trim(mainAddress(jA))
destws.Range("AA" & i).Value = Trim(mainAddress(jA))
destws.Range("Z" & i).Value = addrAA
auxC = Trim(auxC)
destws.Range("Y" & i).Value = auxC
'--------------------------------------------------------
'-------------------------------------------------------
GoTo aseAddr
End If
auxC = Replace(auxC, Trim(mainAddress(jA)), "")
addrAA = mainAddress(jA) & " " & addrAA
'
'.....................................................
Next jA
'.........................................................
'destws.Range("Y" & i).Value = addrAA
'.....................
aseAddr:
'.................................
My problem is that these columns may change order.
I was suggested to use application.match so that my code may follow but i don't know how to put it inside my code.
Can anyone help?
Thanks in advance
Welcome to SO.
In general, the way you can use an excel function in VBA is the following:
application.WorksheetFunction.Match() 'where match() can be replaced by one of the available worksheet functions.
The function's arguments work pretty much the same way as they do when you use the formula in your worksheet.
The Match functionality is explained thoroughly here
So for example let's say you have an array like the following one in cells A1:A13
And you want to find the location of "Friday". You would do it like so:
Debug.Print Application.WorksheetFunction.Match("Friday", sht.Range("A1:A13"), 0)
And you would get 5 as a result.
That should pretty much cover the "How to use Application.Match()".

Looping Syntax within Formula

In my macro, I want to loop within formulas:
Range("D4").Formula = "=Inputs!D8
transforms successfully to:
For i = 1 To 5
Range("D4").Formula = "=Inputs!D" & i + 7
I want to do the same for the code below:
Range("G4").Formula = "=IF($C4<=Inputs!$E$8+Portfolio_Duration-1,SUM($H4:$DC4),"""")"
I tried
For i = 1 To 5
Range("G4").Formula = "=IF($C4<=Inputs!$E$" & i + 7+Portfolio_Duration-1,SUM($H4:$DC4),"""")"
But I am getting a compile error:
Expected: end of statement.
Could you please assist?
Thanks!
Try this out,
Range("G4").Formula = "=IF($C4<=Inputs!$E$" & i + 7 + Portfolio_Duration - 1 & ",SUM($H4:$DC4),"""")"
You are missing a " and a &

Lotus notes: change view selection formula by script

I'm trying to change view selection formula by script with this selection formula:
formula = "SELECT (#Modified = [" & Left(doc1.LastModified,10) & "]) & form = ["& Cstr(doc1.form(0)) &"]"
But when I try to execute it Notes retrive me the following error:
Notes error: Unknown [KeyWord] for #Function (SELECT (#Modified = [03/02/2015]) & form=[myform])
You should use quotes for form instead of square brackets.
Here is example:
formula$ = {SELECT (#Modified = [} & Left(doc1.LastModified,10) & {]) & form = "} & doc1.form(0) & {"}
Which generate this string:
SELECT (#Modified = [03/02/2015]) & form = "myform"

Excel Sequential Numbering order based on hierarchy position of data

I need to add sequential numbering order in excel based on the list position of data within excel. I have tried many things to get this right but surely there must be an easy solution.
As you can see my data is an ordered list of text where you can see the hierarchy in the offset of data down the rows. I am trying to automatically create the yellow picture based on the data from the left side.
The only solution I have come up with is to add columns in front of each column, filter the records based on non-blanks then work from left to right manually adding the sequence, but even this is too time intensive.
Ill be working on a macro next but thought there may be an easier solution using a formula. - will update if found.
This is probably not the most efficient solution, but it works:
Formula in G2 (fill the rest of the area with it):
=IF(ISBLANK(A2),IF(ISBLANK(B2),G1,G1+1),0)
Formula in L2 (fill the rest of the area with it):
=IF(G2>0,K2&"."&G2,"")
Formula in Q2 (fill the rest of the area with it):
=IF(ISBLANK(B2),"",MID(L2,2,LEN(L2))&" - "&B2)
You can easily use it for any depth by increasing the areas size, but you need an empty top row and empty column on the left of each area to make it work.
You could probably skip L:O area by combining the last two formulas.
Result:
Formulas: (blue are constants, orange are for the first hierarchy level, green are dragged in the box)
Sub Sequential_Numbering()
Dim tmpRange As Range, tmpArray(1 To 4) As Integer, x As Integer
Set tmpRange = Range("A1")
tmpArray(1) = 1
tmpRange.Value = tmpArray(1) & " - " & tmpRange.Value
For x = 2 To 13
Set tmpRange = Rows(x).Find(What:="*")
Select Case tmpRange.Column
Case 1
tmpArray(1) = tmpArray(1) + 1
tmpArray(2) = 0
tmpArray(3) = 0
tmpArray(4) = 0
tmpRange.Value = tmpArray(1) & " - " & tmpRange.Value
Case 2
tmpArray(2) = tmpArray(2) + 1
tmpArray(3) = 0
tmpArray(4) = 0
tmpRange.Value = tmpArray(1) & "." & tmpArray(2) & " - " & tmpRange.Value
Case 3
tmpArray(3) = tmpArray(3) + 1
tmpArray(4) = 0
tmpRange.Value = tmpArray(1) & "." & tmpArray(2) & "." & tmpArray(3) & " - " & tmpRange.Value
Case 4
tmpArray(4) = tmpArray(4) + 1
tmpRange.Value = tmpArray(1) & "." & tmpArray(2) & "." & tmpArray(3) & "." & tmpArray(4) & " - " & tmpRange.Value
End Select
Next x
End Sub

Resources