VBA parse string Place values to new columns - parsing

I have a sheets with the rows of data like.
NOM(LSL,USL)=207.3980(206.1990,208.5970) NOM(LSL,USL)=207.3980(206.1990,208.5970) NOM(LSL,USL)=18.8200(18.4400,19.2100)
I would like to just grab the Values and place them in their own cells like
207.3980 207.3980 18.8200
206.1990 206.1990 18.4400
208.5970 208.5970 19.2100
I continue to recieve "ByRef Argument Mismatch" errors. I believe relating to how I am defining the reference cell.
Sub Parse_Replace()
Dim i As Double
Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet
Dim Col As Range
Dim rLastCell As Range
Set rLastCell = ws.Cells.Find(What:="*", After:=ws.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
For i = rLastCell.Column To 1 Step -1
Col = ColLett(rLastCell.Column)
Columns(i).Cells(4) = SplitString(Col3, ",", 4)
Columns(i).Cells(5) = SplitString(Col3, ",", 5)
Columns(i).Cells(6) = SplitString(Col3, ",", 6)
Next i
End Sub
Function ColLett(Col As Integer) As String
If Col > 26 Then
ColLett = ColLett((Col - (Col Mod 26)) / 26) + Chr(Col Mod 26 + 64)
Else
ColLett = Chr(Col + 64)
End If
End Function
Function SplitString(pValue As String, pChar As String, pIndex As Integer) As Variant
Dim YString As Variant
YString = Replace(Replace(Replace(Replace(pValue, " ", ""), "=", ""), "(", ","), ")", ",")
SplitString = Split(YString, pChar)(pIndex - 1)
End Function
Process
Establish number of Columns with Data
Loop through each column
Convert column index to Column with ColLett
Set cell value with SplitString
Loop
Thank You
EDIT : replaced SplitString value with inteded.

You declare Col to be a range here:
Dim Col As Range
You then try to set Col to a string here:
Col = ColLett(rLastCell.Column)
When you set a range you have to set it to a range. Furthermore, you have to use the SET keyword to do so:
Set Col = <a range>
When you set that Col you set it only to the rLastCell.Column repeatedly in each loop of your For. If you just need that column letter for the last column, then do it before entering your for loop.
All of that is pointless anyway. At no point do you use the Column letter that you went through the trouble retrieving in your function. And really, for what you are doing you don't need the Column Letter. Column Letters are for humans; the column number is what is important in VBA any how.

Related

How do I make it look like the picture in Lua

This is my code
local level = 5
for i = 1, level do
local text = ""
for j = 1, i do
text = text..""
end
for j = 1, level-i, 1 do
text = text.." "
end
for j = 1+level, level+(level-i) do
text = text.." "
end
for j = 1, level + i-level do
text = text..""
end
print(text)
end
I want the result to be similar to the one in the picture.
Here is what your code looks like with proper formatting.
local level = 5
for i = 1, level do
local text = ""
for j = 1, i do
text = text..""
end
for j = 1, level-i, 1 do
text = text.." "
end
for j = 1+level, level+(level-i) do
text = text.." "
end
for j = 1, level + i-level do
text = text..""
end
print(text)
end
Your current code prints... well... an empty string. You haven't yet added the characters it's to display to be on par with the image.
The amount of characters per row is 9. So you ideally need 9 characters per row. You will also be incrementing the number once per row. The amount of characters per row also increases by 2; one on each side.
We can use the string.rep(string, number) function to duplicate a 'string' 'number' times. You can feed in your current level into that so it generates 1 2 or 3 depending on the line the number of times. Then you have whitespace to worry about. You can use string.rep again along with a bit of distance math to calculate the amount of whitespace you need from what you take up. Then finally throw everything together concatenated trailing with the first string and print.
local levels = 5
local columns = 9
for i=1, levels do
local str = string.rep(i, i)
local padding = columns - (#str * 2) + 1
print(str .. string.rep(" ", padding) .. str)
end

How to populate listbox1 (without userform) with different colums from different sheets

My listbox is populated with 1 column of a specific sheet.
How can I get a second and third column in my listbox?
I mean... Column B from sheet1, column C from sheet2 and column B from sheet3, each with a second column from the sheets.
In this case I have a textbox where you can type your search.
This already works for me for just 1 specific column of 1 sheet:
Private Sub textbox1_change() 'zoekt via zoekterm boxpositie de mogelijke boxposities op
Dim i As Long
Dim x As Integer
Dim Sheet1 As Worksheet
TextBox1 = Format(StrConv(TextBox1, vbUpperCase))
Set Sheet1 = Worksheets("Cytolab")
a = TextBox1.TextLength
ListBox1.Clear
ListBox1.Height = 101
ListBox1.Top = 290
ListBox1.Width = 512
For i = 5 To Sheet1.Range("B238").End(xlUp).row
For x = 1 To Len(Sheet1.Cells(i, 2))
If UCase(Mid(Sheet1.Cells(i, 2), x, a)) = TextBox1 And TextBox1 <> "" Then
ListBox1.AddItem CStr(Sheet1.Cells(i, 2)) 'CStr() toegevoegd
ListBox1.List(ListBox1.ListCount - 1, 1) = "" & Sheet1.Cells(i, 3) & " · " & Sheet1.Cells(i, 7) & " · " & Sheet1.Cells(i, 5) & " · " & Sheet1.Cells(i, 4)
ListBox1.List(ListBox1.ListCount - 1, 2) = "" & Sheet1.Cells(i, 9)
End If
Next x
Next i
End Sub
Thank you.

How can I generate an array of numbers between multiple ranges defined in separate cells in Sheet?

A1: 1 | B1: 4
A2: 3 | B2: 6
How can I get {1, 2, 3, 3, 4, 4, 5, 6} out of this?
– – – – – –
I know this way:
=ArrayFormula({ROW(INDIRECT(A1&":"&B1)); ROW(INDIRECT(A2&":"&B2))})
That does the job perfectly but what if I don't know, how many ranges there will be? I want to generate an array of all the numbers between values specified in cells from A1:B1 all the way to A:B.
Thank you in advance!
Here is a relatively simple formula to generate the array you're talking about based on an infinite number of ranges in columns A and B.
=ARRAYFORMULA(QUERY(SPLIT(FLATTEN(SEQUENCE(1,MAX(B1:B10-A1:A10)+1,0)+A1:A10&"|"&B1:B10),"|",0,0),"Select Col1 where Col1<=Col2 order by Col1",0))
You can see it demonstrated in the tab called Demo 2 on this sheet.
In Excel 365 with your data in columns A and B, pick a cell and enter:
="{" & TEXTJOIN(",",TRUE,SEQUENCE(,MAX(A:B),MIN(A:B))) & "}"
EDIT#1:
Try this VBA macro:
Sub MakeArray()
Dim I As Long, N As Long, J, k
Dim strng As String
Dim arr As Variant
N = Cells(Rows.Count, "A").End(xlUp).Row
For I = 1 To N
For J = Cells(I, 1) To Cells(I, 2)
strng = strng & "," & J
Next J
Next I
strng = Mid(strng, 2)
strng = "{" & Join(fSort(Split(strng, ",")), ",") & "}"
MsgBox strng
End Sub
Public Function fSort(ByVal arry)
Dim I As Long, J As Long, Low As Long
Dim Hi As Long, Temp As Variant
Low = LBound(arry)
Hi = UBound(arry)
J = (Hi - Low + 1) \ 2
Do While J > 0
For I = Low To Hi - J
If arry(I) > arry(I + J) Then
Temp = arry(I)
arry(I) = arry(I + J)
arry(I + J) = Temp
End If
Next I
For I = Hi - J To Low Step -1
If arry(I) > arry(I + J) Then
Temp = arry(I)
arry(I) = arry(I + J)
arry(I + J) = Temp
End If
Next I
J = J \ 2
Loop
fSort = arry
End Function
The macro:
creates a comma-separated string from each A/B pair
sorts the string
outputs the string

VBA SUMIF on Array

OK, so I am wanting to do a sumif on a column on an array because I don't want to print to the worksheet in order to obtain a range type for a Worksheet.function.Sumif, the idea is to stay completely out in VBA code and write to the worksheet as little as possible. I am trying to optimize for speed: 4814 rows by 40 columns X 60.
The first column total is total of 197,321,164 is correct, the next columns are low and instead of going to quarter 40 the Else kicks in and everything after 8 is 0. The first "To_Quarter" in the array is 9 so with the >= I would think it would go to 9. I tried putting my Next I before the End IF but then it just asks for the For.
image of locals box: https://ibb.co/cgFQhxY
Any help would be much appreciated.
Sub SumifONarray()
Dim arrQuarters, arrNumber_of_Assets As Variant
Dim I As Long, J As Long
arrNumber_of_Assets = Range("Costs_Number_of_Assets")
arrQuarters = Range("Quarters_1to40")
Dim MaxRecov_If_result, arr_Row10_Resolution_Physical_Possession_Expenses_To_Quarter, arr_Row10_Resolution__Max_Recovery_Grid As Variant
arr_Row10_Resolution_Physical_Possession_Expenses_To_Quarter = Range("_Row10_Resolution_Physical_Possession_Expenses_To_Quarter")
arr_Row10_Resolution__Max_Recovery_Grid = Range("_Row10_Resolution__Max_Recovery_Grid")
ReDim arrIf_Max_Recovery_Amount_Sum(1 To 1, 1 To UBound(arrQuarters, 2))
For J = LBound(arrQuarters, 2) To UBound(arrQuarters, 2)
For I = LBound(arrNumber_of_Assets, 1) To UBound(arrNumber_of_Assets, 1)
If arr_Row10_Resolution_Physical_Possession_Expenses_To_Quarter(I, 1) >= arrQuarters(1, J) Then
MaxRecov_If_result = MaxRecov_If_result + arr_Row10_Resolution__Max_Recovery_Grid(I, J)
Else: MaxRecov_If_result = 0
End If
Next I
arrIf_Max_Recovery_Amount_Sum(1, J) = MaxRecov_If_result
MaxRecov_If_result = 0
Next J
End Sub
I've uploaded a sample below with code with 10 rows.
https://easyupload.io/wfixds

How do I add the total the value of a range of cells, when they contain both numbers and letters?

for example my cell contains T 6.5 I want to look for all cells in a row that contain T and add the values of the numbers also contained in that cell.
If you don't mind a row beneath your data to help total things, I would suggest using the following as a quick solution.
Assuming that your data is in Row 1 (starting in cell A1), insert the below formula in Row 2 (cell A2) and copy it to the right as far as you have data in Row 1.
=IF(IFERROR(SEARCH("T ",A1),0)<>0,VALUE(SUBSTITUTE(A1,"T ","")),0)
From there you can total the values across Row 2 using this:
=SUM(2:2)
Note that I assumed that there was a space after "T" in your example above and that this is explicitly included in the first formula above. It simply strips that text from the cell and adds up the remaining numerical value IF the cells in Row 1 have a "T " in them.
Hope this helps or points you in the right direction.
Cheers!
Here is an example for row #7
Sub SumARow()
Dim roww As Long, r As Range, _
Zum As Double, v As Variant
roww = 7
For Each r In Cells(roww, 1).EntireRow.Cells
v = CStr(r.Value)
If InStr(1, v, "T") > 0 Then
Zum = Zum + GetNumber(v)
End If
Next r
MsgBox Zum
End Sub
Public Function GetNumber(s As Variant) As Double
Dim msg As String, i As Long
GetNumber = 0
msg = ""
For i = 1 To Len(s)
ch = Mid(s, i, 1)
If ch Like "[0-9]" Or ch = "." Then
msg = msg & ch
End If
Next i
If msg = "" Then Exit Function
GetNumber = CDbl(msg)
End Function

Resources