evaluating variables in string vba excel - excel

sReport = "EFGHIJKLM"
sSource = "FGHIJKLMN"
For J = 5 To 18
For I = 1 To Len(sReport)
wbTarget.Sheets(sheetTargetName).Range(Mid(sReport, I, 1) & J) = "=HK_USD!(Mid(sSource, I, 1) & J+79)+London_USD!(Mid(sSource, I, 1) & J+79)+Labuan_USD!(Mid(sSource, I, 1) & J+79)+NY_USD!(Mid(sSource, I, 1) & J+79)+NISP_USD!F84+OWH_USD!(Mid(sSource, I, 1) & J+79)"
Next
Next
I have this formula as shown in the line above. Basically, I want that line to evaluated as
wbTarget.Sheets(sheetTargetName).Range("E5") = "=HK_USD!F84+London_USD!F84+Labuan_USD!F84+NY_USD!F84+NISP_USD!F84+OWH_USD!D4"
and the Range will change to F5, G5, H5 etc because of the loops. The left side of the formula works. The problem is the one in the string. How can I change this code so that it will evaluate my variables in the string? Thanks

something like this
strFormula="=HK_USD!" & (Mid(sSource, I, 1) & J+79) & "+London_USD!" & (Mid(sSource, I, 1) & J+79) & " +Labuan_USD!" & (Mid(sSource, I, 1) & J+79) & " +NY_USD!" & (Mid(sSource, I, 1) & J+79) & "+NISP_USD!F84+OWH_USD!" & (Mid(sSource, I, 1) & J+79)
wbTarget.Sheets(sheetTargetName).Range(Mid(sReport, I, 1) & J)=strFormula

Related

Storing SeriesCollection Values in dynamical array

I have the following issue: I create a chart from values that a user will insert in specific cells of a spreadsheet and the chart displays what I expect (The series are collected in the right way). The Series are created consecutively with 2 data points only. I want to store the Xvalues (And YValues) of the series in dynamic arrays for performing calculations later (outside the series collection loop). I tried the following but the arrays only store the last values of the Series (despite using redim preserve everywhere).
Is it because the array is not redimensioned correctly?
Thanks for your help!
i = 1
SeriesAddedA = 1
RangeOccupiedA = Range("A2").End(xlDown).Row
RangeOccupiedB = Range("B2").End(xlDown).Row
MaxRangeOccupied = WorksheetFunction.Max(RangeOccupiedA, RangeOccupiedB)
Dim XValuesA()
Dim YValuesA()
ReDim XValues(RangeOccupiedA + 2)
ReDim YValues(RangeOccupiedA + 2)
Do While i < MaxRangeOccupied
FilledCompA = NextFilled(Cells(i, 1))
PrevCell = PrevFilled(Cells(i + 1, 1))
rowdiff = FilledCompA - PrevCell
If rowdiff < 2 And PrevCell <> 0 And FilledCompA <> 0 Then
ReDim Preserve XValuesA(1 To RangeOccupiedA + 2)
chart.SeriesCollection.NewSeries
chart.FullSeriesCollection(SeriesAddedA).Name = "Reaction Step A " & SeriesAddedA - 1
chart.FullSeriesCollection(SeriesAddedA).XValues = "=" & "'" & ActiveSheet.Name & "'" & "!$E$" & FilledCompA - 1 & ":$E$" & FilledCompA
chart.FullSeriesCollection(SeriesAddedA).Values = "=" & "'" & ActiveSheet.Name & "'" & "!$M$" & FilledCompA - 1 & ":$M$" & FilledCompA
XValuesA() = chart.SeriesCollection(SeriesAddedA).XValues
SeriesAddedA = chart.SeriesCollection.Count + 1
ElseIf PrevCell <> 0 And rowdiff > 0 Then
ReDim Preserve XValuesA(1 To RangeOccupiedA + 2)
chart.SeriesCollection.NewSeries
chart.FullSeriesCollection(SeriesAddedA).Name = "Reaction Step prev A " & SeriesAddedA - 1
chart.FullSeriesCollection(SeriesAddedA).XValues = "=" & "(" & "'" & ActiveSheet.Name & "'" & "!$E$" & FilledCompA - rowdiff & "," & "'" & ActiveSheet.Name & "'" & "!$E$" & FilledCompA & ")"
chart.FullSeriesCollection(SeriesAddedA).Values = "=" & "(" & "'" & ActiveSheet.Name & "'" & "!$M$" & FilledCompA - rowdiff & "," & "'" & ActiveSheet.Name & "'" & "!$M$" & FilledCompA & ")"
XValuesA() = chart.SeriesCollection(SeriesAddedA).XValues
SeriesAddedA = chart.SeriesCollection.Count + 1
End If
loop
'the debug print says:
XvaluesA(1) 310.52
XvaluesA(2) 408.58
With XValuesA() = chart.SeriesCollection(SeriesAddedA).XValues you overwrite the data in XValuesA everytime you call it in your loop. That is why it only contains the last values.
Use XValuesA(i) = chart.SeriesCollection(SeriesAddedA).XValues instead and then
Debug.Print XvaluesA(1)(1)
Debug.Print XvaluesA(1)(2)
gives the first values and
Debug.Print XvaluesA(2)(1)
Debug.Print XvaluesA(2)(2)
the second …

Using VBA in excel - Pad the resulting output of a formula with a 0

I have written a block of code using VBA in excel that counts the number of records produced in a file exported from another platform on the very last line of the file.
This file has to contain a specific number of characters to validate. If the record count is 9 or less then it will operate just fine. However, if the record count is 10 or above the character limit is exceeded and it will fail.
Is there a way to embed a line code into the module that will pad the output from this last line of syntax with a leading 0 if the record count is less than 10? If so, I can then alter the earlier part of the code to remove a static character and meet the files requirements regardless of if the record count is above or below 9.
Code is below...The bolded area on the last line of the code at the very end (" & lastrow - 2 & ") is where I need to pad the 0 of the resulting formula.
Sub Hidden_macro1()
On Error Resume Next
Application.DisplayAlerts = False
Sheets("Testing_Report_Out").Delete
Application.DisplayAlerts = True
On Error GoTo 0
Sheets.Add.Name = "Testing_Report_Out"
Dim lastrow As Long
Dim lastrowfooter As Long
Dim I As Integer
Dim J As Integer
Sheets("Testing Report").Select
lastrow = Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row + 1
Sheets("Testing_Report_Out").Select
Range("A1").Formula = "=CONCATENATE('Testing Report'!A4,"" "",'Testing Report'!B4,'Testing Report'!C4,"" "",'Testing Report'!D4,'Testing Report'!E4,"" "",'Testing Report'!F4)"
I = 4
J = 2
Do Until I = lastrow
'Fill 1st formula (:20:)
Range("A" & J).Formula = "=CONCATENATE('Testing Report'!G" & I & ",'Testing Report'!H" & I & ")"
J = J + 1
'Fill 2nd formula (:23B:)
Range("A" & J).Formula = "=CONCATENATE('Testing Report'!I" & I & ",'Testing Report'!J" & I & ")"
J = J + 1
'Fill 3rd formula (:32A:)
Range("A" & J).Formula = "=CONCATENATE('Testing Report'!K" & I & ",'Testing Report'!L" & I & ",'Testing Report'!M" & I & ",'Testing Report'!N" & I & ",'Testing Report'!O" & I & ")"
J = J + 1
'Fill 4th formula (:50K:)
Range("A" & J).Formula = "=CONCATENATE('Testing Report'!P" & I & ",'Testing Report'!Q" & I & ")"
J = J + 1
'Fill 5th formula (:50K: Addy)
Range("A" & J).Formula = "='Testing Report'!R" & I & ""
J = J + 1
'Fill 6th formula (:57A:)
Range("A" & J).Formula = "=CONCATENATE('Testing Report'!S" & I & ",'Testing Report'!T" & I & ")"
J = J + 1
'Fill 7th formula (:59:)
Range("A" & J).Formula = "=CONCATENATE('Testing Report'!U" & I & ",'Testing Report'!V" & I & ")"
J = J + 1
'Fill 8th formula (:59:/ 3)
Range("A" & J).Formula = "='Testing Report'!W" & I & ""
J = J + 1
'Fill 9th formula (:59:/ 5)
Range("A" & J).Formula = "='Testing Report'!X" & I & ""
J = J + 1
'Fill 10th formula (:70:)
Range("A" & J).Formula = "=CONCATENATE('Testing Report'!Z" & I & ",'Testing Report'!AA" & I & ")"
J = J + 1
'Fill 11th formula (:71A:)
Range("A" & J).Formula = "='Testing Report'!AB" & I & ""
J = J + 1
'Fill 12th formula (:72:)
Range("A" & J).Formula = "='Testing Report'!AC" & I & ""
I = I + 1
J = J + 1
Loop
lastrowfooter = Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
Range("A" & lastrowfooter + 1).Formula = "=CONCATENATE('Testing Report'!AD4,'Testing Report'!AE4,'Testing Report'!AF4,'Testing Report'!AG4,"" "",'Testing Report'!AH4,'Testing Report'!AI4,'Testing Report'!AJ4," **& lastrow - 2 &** ")"
End Sub
You can use Format() as suggested by #BigBen
Range("A" & lastrowfooter + 1).Formula = _
"=CONCATENATE('Testing Report'!AD4,'Testing Report'!AE4,'Testing Report'!AF4," & _
"'Testing Report'!AG4,"" "",'Testing Report'!AH4,'Testing Report'!AI4," & _
"'Testing Report'!AJ4," & Format(lastrow - 2, "00") & ")"

Trouble with proper indexing new lines

I'm looping through columns and storing them in values s and s1. I need to add count order for as many lines as it's needed for which I'm trying to use j index. Getting picture like this all the time:
And the desired result should be as follows:
s = ""
s1 = ""
j = 1
Do
s = j & ". " & s & Workbooks(Filename).Sheets(1).Cells(i, 2).Offset(1, 0).Value & vbCrLf
s1 = j & ". " & s1 & Workbooks(Filename).Sheets(1).Cells(i, 3).Offset(1, 0).Value & vbCrLf
i = i + 1
j = j + 1
Loop While Workbooks(Filename).Sheets(1).Cells(i, 3).Value <> ""
The code should be incremented like this:
s = s & j & ". " & Cells(i, 2).Offset(1, 0) & vbCrLf
Thus, the s value is incrementing over itself.
In general, whenever such problems appear and it is tough to understand why a string is being formatted, try to debug step-by-step. E.g., write Stop on the code and print the string, which is avialable up to now. Like this:
Do
s = s & j & ". " & Cells(i, 2).Offset(1, 0) & vbCrLf
s1 = j & ". " & s1 & Cells(i, 3).Offset(1, 0) & vbCrLf
i = i + 1
j = j + 1
Debug.Print s
Stop
Loop While Cells(i, 3) <> ""
Then the magic becomes easier to understand. Pressing F8 and checking the current values with hovering over them in VBE is another option for step-by-step debugging.
Debugging VBA, Locating Problems, and Troubleshooting Methods

Formula using Variables with VBA

I am wrestling with the syntax of this one line of some code. the formula with "IfError" seems to have the wrong syntax. I believe I have the quotes in the right places.
Dim j As Integer
Dim MidPointE As String
Dim Dist As String
Dim Allocation As String
MidPointE = "AM"
Dist = "AN"
Allocation = "AO"
for J= 1 to 300
Cells(j, Dist).Formula = "=IFERROR(" & MidPointE & j & " / " & MidPointE & CustomerLast & ", "")"
Cells(j, Allocation).Formula = "=" & Allocation & j & "* S" & CustomerLast
Next J
You can also assign multiple formulas at the same time:
Range("AN1:AN300").Formula = "=IFERROR(AM1 / AM$" & CustomerLast & ", """")"
Range("AO1:AO300").Formula = "=AO1 * S$" & CustomerLast
I also recommend looking into Excel Tables and Structured References
To avoid the circular reference issue, you can calculate the formulas and assign the values directly:
Range("AN1:AN300") = Evaluate("IFERROR(AM1:AM300 / AM$" & CustomerLast & ", """")")
Range("AO1:AO300") = Evaluate("INDEX(AO1:AO300 * S$" & CustomerLast & ",)")
Try this:
Cells(j, Dist).Formula = "=IFERROR(" & MidPointE & j & " / " & MidPointE & CustomerLast & ", """")"
Quotes inside a string literal must be escaped - and that's done by doubling them up.
It's at the end here
& ", "")"
The two quotes between the comma and ) end the string and start a new one.
'first string
","
'second string
")"
Since you can't just throw two strings together like that, i would replace the two quotes with the CHR equivalent. Should look something like this:
Cells(j, Dist).Formula = "=IFERROR(" & MidPointE & j & " / " & MidPointE & CustomerLast & ", "& CHR(034) & CHR(034) & ")"
'pretty sure 034 is the ascii code for "

Execute String Formulas from Array

I have two columns which have to be filled with formulas (with 5k to 10k values). Those are simple formulas and they work.
But writing directly into the cells slows the program down too much, so I'm writing the formulas into an array and then pasting the array into the workbook. This works fine too, it just doesn't evaluate the strings. I tried using EVALUATE without success.
If I select any of the copied cells, press F2 and Enter, they work perfectly.
Code sample:
ReDim Schreibblock(FirstRow To LastRowPos, 0 To 1)
For j = FirstRow To LastRowPos
Schreibblock(j, 0) = "=" & SteigungPos.Address & "*$B$" & j & "+" & NullwertPosTren.Address & ""
Schreibblock(j, 1) = "=ABS($A$" & j & "-$C$" & j & ")"
Next j
Range("C" & FirstRow & ":D" & LastRowPos).Formula = [Schreibblock]
and here a "light version" for easier reading:
ReDim array(14 To 5000, 0 To 1)
For j = 14 To 5000
Array(j, 0) = "=$B$10*$B$14+$B$9" <- This is what I see, but w/o ""
Array(j, 1) = "=ABS($A$14-$C$14)" <- This is what I see, but w/o ""
Next j
Range("C14:D5000").Formula = [Array]
There are no errors and it's lightening fast compared to wirting every loop into a cell. I would appreciate any help.
Carlos
Use R1C1 notation and skip the array and loop:
Range("C" & FirstRow & ":C" & LastRowPos).FormulaR1C1 = "=" & SteigungPos.Address(0,0,xlR1C1) & "*RC2 +" & NullwertPosTren.Address(0,0,xlR1C1) & ""
Range("D" & FirstRow & ":D" & LastRowPos).FormulaR1C1 = "=ABS(RC1 - RC3)"

Resources