This question already has an answer here:
How to automatically input an array formula as string with more than 255 characters in length into an excel cell using VBA?
(1 answer)
Closed 2 years ago.
yesterday I posted about this problem because I didn't know an array had a limit of 255 characters. Today I've been trying for the last 9 hours to break it down as shown here:
Overcoming the 255 char. limit for formulaArray in Excel VBA and on google search for other posts doing exactly the same thing.
I followed the instructions about in order for the trick to work, you need to write complete formulas, but still haven't figured it out.
Dim TR As Double
Dim TC As Double
Dim Formula1 As String
Dim Formula2 As String
Dim Formula3 As String
Dim Rows2Copy As Integer
Dim PValue As String
Dim str As String
str = OpenBook.Sheets(1).Cells(2, 1).Value
PValue = Mid(str, 20, 2)
Rows2Copy = (PValue / 5) - 10
TR = ActiveWorkbook.Sheets(2).Cells(22 + Rows2Copy, 2).Value
TC = WorksheetFunction.Match("*" & "W" & Cells(2, 3) & "*", Worksheets(1).Range("5:5"), 0)
For j = 300 To TotalRows * 300 Step 300
With ActiveWorkbook.Sheets(2).Range("B24")
Formula1 = "=LOOKUP(2,1/('Raw Data'!R11C" & TC & ":R1048576C" & TC & "=VLOOKUP(INDEX('Raw Data'!R" & TR - j & "C1:R" & TR - j + 300 & "C1,MATCH(MIN(+FX2),+FX2,0)),'Raw Data'!R11C1:R1048576C131,+FX3,FALSE)),'Raw Data'!R11C1:R1048576C1)"
Formula2 = "ABS('Raw Data'!R" & TR - j & "C" & TC & ":R" & TR - j + 300 & "C" & TC & "-((R[1]C[2]-0.05)*R7C4))"
Formula3 = "MATCH(""*""&""W""&R2C3&""*"",'Raw Data'!R5,0)"
.FormulaArray = Formula1
.Replace "+FX2", Formula2, xlPart
.Replace "+FX3", Formula3, xlPart
End With
Next j
Sample
In the Picture, the Load case is extracted as well as the maximum force applied, the first thing is based on W Tested, it looks for the column containing that data, then returns the maximum value on "Applied Load" Column and finally returns the ID of that value. The applied load value represents 75.7%
The next step is based on a 300 upward row range from the starting position (in this case row 44834) find the closest match to 70% of the initial test and then return the value, then with 65% then 60% and so on until 10%. Everything else will populate with those values and create the intended charts. All I'm missing is this formula for it to work
Thank you! I really appreciate the help!
Maybe this doesn't help, but I hope it gives you some insight.
Excel charts work best when they are charting data from a worksheet. If you can define an array in VBA, you can easily dump that array into a sheet somewhere, then insert that range into the chart series formula. This eliminates the problem with array character limits, and makes it easier to debug the chart data.
I managed to solve it but thanks to everyone anyways:
TR = ActiveWorkbook.Sheets(2).Cells(22 + Rows2Copy, 2).Value
TC = WorksheetFunction.Match("*" & "W" & Cells(2, 3) & "*",
Worksheets(1).Range("5:5"), 0)
TCL = Split(Cells(1, TC).Address, "$")(1)
TotalRows = (PValue / 5) - 3
ActiveWorkbook.Sheets(2).Activate
Rows2Copy2 = Rows2Copy
For i = 0 To TotalRows Step 1
With ActiveWorkbook.Sheets(2).Cells(19 + Rows2Copy - i, 2)
'Split formula
Formula1 = "=LOOKUP(2,1/('Raw Data'!$" & TCL & TR - PRange & ":" & TCL & TR & "=VLOOKUP(INDEX('Raw Data'!A" & TR - PRange & ":A" & TR & ",MATCH(MIN(+FX2),+FX2,0)),'Raw Data'!$A" & TR - PRange & ":EA" & TR & ",+FX3,FALSE)),'Raw Data'!$A" & TR - PRange & ":A" & TR & ")"
Formula2 = "ABS('Raw Data'!" & TCL & TR - PRange & ":" & TCL & TR & "-((D" & 20 + Rows2Copy2 & "-0.05)*$D$7))"
Formula3 = "MATCH(""*""&""W""&$C$2&""*"",'Raw Data'!$5:$5,0)"
.FormulaArray = Formula1
.Replace "+FX2", Formula2, lookat:=xlPart
.Replace "+FX3", Formula3, lookat:=xlPart
.VerticalAlignment = xlCenter
.HorizontalAlignment = xlCenter
End With
Rows2Copy2 = Rows2Copy2 - 1
Next i
Related
It may seem to be an easy one as i am novice at VBA. I am trying to fill the filtered blank visible cells with previous non blank hidden cell value in the same column. The Lookup formula is working fine on excel sheet but using it with variable range in VBA is giving Application defined or object defined Error on lookup formula line.
nlr = Cells(Rows.Count, 9).End(xlUp).Row
With Activehseet
Application.DisplayAlerts = False
Range("A1:K" & nlr).AutoFilter Field:=2, Criteria1:=""
Range("A1:K" & nlr).AutoFilter Field:=1, Criteria1:="P * Suite *"
Range("B2:B" & nlr).SpecialCells(xlCellTypeVisible).ClearContents
For Each c In Range("B1:B" & nlr).Offset(1, 0).SpecialCells(xlCellTypeVisible)
n = c.Row - 1
c.Formula = "=LOOKUP(2,1/($B$2:B&n&<>""),$B$2:B&n&)"
I have already tried it with below too, but it didn't work
c.Formula = "=LOOKUP(2,1/($B$2:B " & n & "<>""),$B$2:B" & n & ")"
Please help me resolve this
EDIT: I have already tried this approach, but it didn't work either
c.Formula = "=LOOKUP(2,1/($B$2:B " & n & "<>""""),$B$2:B" & n & ")"
This is the correct approach for concatenating a string into another string:
c.Formula = "=LOOKUP(2,1/($B$2:B " & n & "<>""),$B$2:B" & n & ")"
However, there is a problem with this part of it:
... & n & "<>""), ...
That will place one double quote into the text. I presume you are expecting it to look like <>"" when parsed, which means the line should look like this:
... & n & "<>""""), ...
Why? Because in a string to put one double quote you need to put a double, double quote: "" = " (in finished string). Therefore a double, double quote becomes: """" = "".
Here is the corrected original code:
c.Formula = "=LOOKUP(2,1/($B$2:B " & n & "<>""""),$B$2:B" & n & ")"
I searched on internet, without any help coming out of that...
I simply would like to be able to have my VBA code to write this formula in a cell :
=IF(C4="-"; "-"; Cars!C4*C4*Data!$C$8)
As you guessed, there is a page called "Cars" and one called "Data" where I pick the informations needed.
Of course, as it is a VBA code, the C4 will be 2 variables, one for the C and one for the 4 that will evolve...
Actually, I tried this :
Worksheets("Calculation").Range(Column & PosStartCalc + 1).Formula = "=" & "IF(" & Column & PosStartCalc & " = " & "" - "" & ";" & " - " & ";" & "Cars!" & Column & PosStart & "*" & Column & PosStartCalc & "*" & "Data!" & "C" & "8" & ")"
(The variable Column contains the column letter and the variable PosStartCalc contains the row number)
This hurts my eyes and apparently VBA's ones too as it gives the error "Run-Time error '13': Type Mismatch'
Could anyone tell me how to do that?
Thanks in advance !
Try the following, assuming the column variable is a string and row a long variable. I might not have all the variables right, but you'll be able to get what I meant to do here.
Sub test()
Dim Col As String: Col = "C"
Dim Rw As Long: Rw = 4
With ThisWorkbook.Sheets("Calculation")
Debug.Print "=IF(" & Col & Rw & "=""-"",""-"",Cars!" & Col & Rw & "*" & Col & Rw & "*Data!$C$8)"
.Cells(Rw + 1, Col).Formula = "=IF(" & Col & Rw & "=""-"",""-"",Cars!" & Col & Rw & "*" & Col & Rw & "*Data!$C$8)"
End With
End Sub
So what you might forget easily is to use the , as parameter delimiter in a VBA programmed formula. When you put this on your sheet Excel will automatically replace that with the appropriate delimiter for your region.
Another thing to keep in mind; whenever you about to use a string value in such an function, don't forget to wrap it in double quotes!
Don't forget to remove the Debug.print .... line. It was merely there to show the output :)
I have a table with several operation codes and its hours, and I need to sum every Thursday the hours spent on each code.
Despite being able to figure it out an IF formula would do the job I got stuck with the sum of the ranges, I could get it working via VBA but I can´t apply that same solution on Formula:
WorksheetFunction.Sum(Range("E" & cCell.Row & ":E" & cCell.Row - 6))
Dim Counter As Integer
Dim cCell As Range
Dim intToday As Integer
Dim CountDate As Integer
Dim strWsName As String
strWsName = ActiveSheet.Name
Dim xWs As Worksheet
Set xWs = Worksheets(strWsName)
'Clause 101
For Counter = 4 To 34
Set cCell = xWs.Cells(Counter, 4)
If WorksheetFunction.WeekDay(cCell.Value) = 5 Then
If cCell.Row = 4 Then
xWs.Range("Q" & cCell.Row) = WorksheetFunction.Sum(Range("E" & cCell.Row & ":E" & cCell.Row))
Else
If cCell.Row >= 34 Then
xWs.Range("Q" & cCell.Row) = WorksheetFunction.Sum(Range("E" & cCell.Row & ":E" & cCell.Row))
Else
If cCell.Row - 6 <= 0 Then
xWs.Range("Q" & cCell.Row) = WorksheetFunction.Sum(Range("E" & cCell.Row & ":E4"))
Else
xWs.Range("Q" & cCell.Row) = WorksheetFunction.Sum(Range("E" & cCell.Row & ":E" & cCell.Row - 6))
End If
End If
End If
End If
Next Counter
End Sub
I would like to know how I could transform that piece of code to a formula on Excel.
After doing a research on formulas and variables ranges I have managed to set this formula and it is working like a charm:
=IF(WEEKDAY($D9)=5;IF(ROW(E9)<=6;SUM(OFFSET(E9;;;-ROW()));SUM(OFFSET(E9;;;-7)));"")
NOTE: My locale settings uses ";" instead of "," on formulas, so bear in mind you must change it to your locale settings.
Explanation:
The "=IF(WEEKDAY($D9)=5" formula will assess if the date is a Thursday, if it is not it will exhbit "" on the column;
The IF(ROW(E9)<=6 clause will serve to identify the limits to the top of my spreadsheet, depding on its value it will SUM diferent ranges:
If the date´s row is smaller than 6 it will run this:
SUM(OFFSET(E9;;;-ROW())). The offset will be the same as the row where the
date is.
Now if the row is bigger than 6 then I will be able to set a fixed
offset of 7 rows: SUM(OFFSET(E9;;;-7))
If one needs to change the calculation to Wednesday all you have to do is to change the number "5" on "=IF(WEEKDAY($D9)=5" numbers of the weekdays intended, here is the list:
1 to Sundays
2 to Mondays
3 to Tuesdays
4 to Wednesdays
5 to Thursdays
6 to Fridays
7 to Saturdays
Thanks to articles found on Excel Jet and Extend Office I managed to build this solution!
I hope more people can use this solution!
The best way to solve this was by creating a UDF, I had the help from WideBoyDixon at ExcelForum:
Public Function SumWeek(sumRange As Range, dateRange As Range, endDate)
Application.Volatile
Dim prevSheetName As String
Dim prevSheet As Worksheet
SumWeek = Application.WorksheetFunction.SumIfs(sumRange, dateRange, "<=" & CStr(CLng(endDate)), dateRange, ">" & CStr(CLng(endDate - 7)))
prevSheetName = Mid("DecJanFebMarAprMayJunJulAugSepOctNov", Month(endDate) * 3 - 2, 3) & CStr(Year(endDate) - IIf(Month(endDate) = 1, 1, 0))
On Error Resume Next
Set prevSheet = Worksheets(prevSheetName)
If Not (prevSheet Is Nothing) Then
SumWeek = SumWeek + Application.WorksheetFunction.SumIfs(prevSheet.Range(sumRange.Address), prevSheet.Range(dateRange.Address), "<=" & CStr(CLng(endDate)), prevSheet.Range(dateRange.Address), ">" & CStr(CLng(endDate - 7)))
End If
End Function
I have the following string "123 - 456789". What I am trying to do is find a way to only capture the remaining characters after the second space - "456789" - regardless the length of the string.
I have the follow set of code:
For leftLoop = 2 To leftNumberOfCells
Range("A" & iRow) = Split(Range("B" & iRow).Value, " ")
Range("B" & iRow) = Mid("B" & iRow, InStr("B" & iRow, " "), 100)
iRow = iRow + 1
Next leftLoop
The code line "Range("B" & iRow) = Mid("B" & iRow, InStr("B" & iRow, " "), 100)" is what I tried, among other ways (from searching online, but I can't seem to get it to work.
I have two questions:
Does someone know what the correct code should be? and...
Can I reference the cell where the string is located and replace it in that same cell after doing the mid function without having to temporarily put it into another cell and copy and paste it back? For example, my string "123 - 456789" is in cell B2, is there a way to code it so I can reference cell B2 and simultaneous replace the cell B2 with "456789" and not having to place it in another cell then copy and paste it back into B2. I hope you get what i'm asking.
Thanks for you help!
This addresses part 2.
Sub strings()
Dim replace As String
Dim bCell As Range
For leftLoop = 2 To leftNumberOfCells
Set bCell = Range("B" & iRow)
replace = Mid(bCell, InStr(bCell, "-") + 2, 100)
Range("B" & iRow) = replace
iRow = iRow + 1
Next leftLoop
End Sub
Try this:
result = Split(TextToSplit, " ", 3)(2)
Split(TextToSplit, " ", 3) will split the text on spaces, returning a zero-based array. The last argument 3 limits the splitting to 3 portions: before the first space, between the first and second space, and everything else. The (2) at the end of the statement returns the last element of the array.
Hope that helps
I have an excel sheet with a column for Car number. It is currently downloaded as a report with the format "58 58" for car number 58.
I would like to replace each occurrence down column H and replace "58 58" with a numeric "58" | "60 60" with "a numeric "60" | "90 90" with a numeric "90" and so on.
This is all done in VBA.
Thank you
UPDATED CODE:
Dim X As Long
For X = 2 To Range("I" & Rows.Count).End(xlUp).Row 'Change 1 to 2 if you have a heading in row 1
Range("I" & X).Formula = Split(Range("I" & X).Text, " ")(0)
I used the above code, but it gave me runtime error (9) subscript out of range
Probably the easiest way is to loop. Going backwards alleviates possible issues with 101 being messed up if you start at 1.
Sub DoubleToSingle()
Dim X As Long
For X = 100 To 1 Step -1
Cells.Replace X & " " & X, X
Next
End Sub
This works on all cells, if you want it on just a column replace Cells. with Columns(2). where 2 is column B and 3 would be C etc.
Edit: use this code for your updated questions:
Sub DoubleToSingle2()
Dim X As Long
For X = 2 To Range("I" & Rows.Count).End(xlUp).Row
If InStr(1, Range("I" & X).text, " ") > 0 Then Range("I" & X).Formula = Split(Range("I" & X).text, " ")(0)
Next
End Sub
How it works: It polls through all cells in column I that have data and for each cell it splits the text of the cell into an array using space, then it takes just the first element in the array (basically everything before the first space) and posts that back to the cell.