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
Related
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 "
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
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)"
So here is my problem or my lack of knowledge...I'm exporting a few columns of a worksheet into a csv-file.
The columns will be written in one cell in one line like that ->
Bauer M,"90","colleagues","active"
So in my Excel it looks like this ->
Bauer Martin 90 (and each word in one cell)
So my question is...how do I get only the first later of the name Martin in my CSV-file...my code is down below...I just started coding like 3 days ago so I'm sorry if it's not perfect...
Private Sub btn_erstellen_Click()
Dim My_filenumber As Integer
Dim logSTR As String
Dim i, j As Integer
i = tb_dw1.Value
j = tb_dw2.Value
My_filenumber = FreeFile
Open "C:\test\Sample.csv" For Append As #My_filenumber
Do While Not Cells(i, "B").Value = ""
If i = j + 1 Then
Exit Do
Else
If Cells(i, "F").Text = "inaktiv" Or Cells(i, "A").Text = "INTERNER" Then
i = i + 1
Else
If Cells(i, "A").Value = "-" Then
logSTR = Cells(i, "B").Value & ","
Else
logSTR = Cells(i, "A").Value & " "
logSTR = logSTR & Cells(i, "B").Value & ","
End If
logSTR = logSTR & """"
logSTR = logSTR & Cells(i, "C").Value
logSTR = logSTR & """" & "," & """" & "colleagues" & """" & "," & """" & "active" & """"
i = i + 1
Print #My_filenumber, logSTR
End If
End If
Loop
Close #My_filenumber
End Sub
Replace
Cells(i, "B").Value & ","
with
Mid(Cells(i, "B").Value,1,1) & ","
The first part refers to a value on a certain cell. By using Mid it will allow you to extract part of the contents of it.
Its usage is Mid(STRING,WHERE_IT_STARTS,HOW_MANY_CHARACTERS). In this case, it will get the value from Cells(i, "B").Value, and from the first digit, it will extract one digit (The 1,1 on the code I suggested)
Use the Left(string, Length) function. This would look something like left(Range("A1").value, 1) for your case.
I am trying to use VBA to concatenate everything between two specified rows. What's the best way to go about this?
Basically I want to leave the lines where the third cell is "U" intact, and make the sixth cell of that row the concatenation of the rows below, until we run into another row that contains a "U" in the third cell. Then the process would repeat. The number of rows between the cells containing "U" is varied.
Pic is below
Ok, this should work (haven't tested it though):
Sub My_Amazing_Skills()
Dim l As Long, i As Long
l = 1
i = 1
Do Until i > Range("A1048576").End(xlUp).Row
If Range("C" & l).Value = "U" Then
i = i + 1
Do Until Range("C" & i).Value = "U"
Range("F" & l).Value = Range("F" & l).Value & " " & Range("C" & i).Value
i = i + 1
Loop
Range("F" & l).Value = Trim(Range("F" & l).Value)
End If
l = i
Loop
MsgBox "Bow down to the great Jeremy!", vbInformation, "Your concatenating is done"
End Sub
I presume you know know where to copy this to?