After the second round of my code the while wend loop stops because the field seems empty, even if I look after it in the watch window. But the Excel table is full with stuff from Cells 1:1 to 12:92...
x = 1
y = 2
While Cells(x & 1) <> ""
If Cells(x & 1) = cmb_spalte Then
While Cells(x & y) <> ""
If Cells(x & y) = txt_suchwert Then
meineDatei.WriteLine (Range("C" & y) & vbTab & vbTab & vbTab & Range("G" & y) &
vbTab & vbTab & Range("K" & y))
y = y + 1
End If
Wend
End If
x = x + 1
Wend
Related
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 …
I am having trouble figuring out why my IF formula in VBA is not returning any of the criteria values. All I can get is the True or False value. This is what I currently have:
Cells(y, 11).Formula = "=IF((A" & y & ")+(B" & y & "))-((A" & y - 1 & ")+(B" & y - 1 & "))" > "($J$7*20),(((C" & y & ")-(C" & y - 1 & ")) * $J$7),(((A" & y & ")+(B" & y & "))-((A" & y - 1 & ")+(B" & y - 1 & ")))"
Im sure its just a type-o or I am missing some quotations somewhere but if anyone can take a look at this and find what I am missing, that would be great.
This is my whole code for better context.
Sub make_new_entry()
Dim Layer_Increment, i As Integer
Set Layer_Increment = Cells(7, 2)
y = 10 'Set this to the first row number that gets a time entered.
i = 0
Do While i = 0
If Cells(y, 1) = "" Then
add_row y
Cells(y, 1) = Format(Now(), "m/d/yy")
Cells(y, 2) = Format(Now(), "h:mm")
Cells(y, 3) = Cells(y - 1, 2) - ((Cells(y - 1, 2) - Layer_Increment * (y - 10)))
Cells(y, 8).Formula = "=($H$7*(C" & y & "))+((0.5*'Data Reference'!F2)+(0.4*'Data Reference'!F3)+(0.3*'Data Reference'!F4)+(0.2*'Data Reference'!F5)+(0.1*'Data Reference'!F6)+(0.05*'Data Reference'!F7)+(0.01*'Data Reference'!F8)+(0.001*'Data Reference'!F9))+ $H$10+$N$7"
Cells(y, 9).Formula = "=$I$10-(0.05*(C" & y & "-$C$10))"
Cells(y, 10).Formula = "=(((A" & y & ")+(B" & y & "))-((A" & y - 1 & ")+(B" & y - 1 & ")))/((C" & y & ")-(C" & y - 1 & "))"
Cells(y, 11).Formula = "=IF(A" & y & " + B" & y & ")-(A" & y - 1 & " + B" & y - 1 & ") > $J$7*20, (C" & y & " - C" & y - 1 & ") * $J$7, (A" & y & " + B" & y & ")-(A" & y - 1 & " + B" & y - 1 & ")"
Cells(7, 6).Formula = "=((130-(H" & y & "))/$H$7)-('Build Information'!$F$6-(C" & y & "))"
Cells(7, 4).Formula = "=Max(C:C)"
Cells(7, 10).Formula = "=AverageIf(J11:J" & y & ",""<00:05:00"")"
Cells(8, 11).Formula = "=Sum($K$10:(K" & y & ")"
i = 1
Else
y = y + 1
End If
Loop
End Sub
I suggest that you:
fully qualify your ranges
check your parenthesis and the order the calculations are made in Excel
use variable names that are easy to understand (for example, instead of y use counter) .
Regarding your formula, here is the way I'd fix it
Cells(y, 11).Formula = "=IF((A" & y & "+B" & y & ")-(A" & y - 1 & "+B" & y - 1 & ") > $J$7*20,((C" & y & "-C" & y - 1 & ") * $J$7),((A" & y & "+B" & y & ")-(A" & y - 1 & "+B" & y - 1 & ")))"
PS To fully qualify a range use this syntax
Thisworkbook.Worksheets("Sheet1").Cells(y,11).Formula =
Im trying to use the AverageIf function in my code that uses a cell as criteria. This cell is also in the same macro. When I do this I get "False" returned instead of a value. This is what I have. {Sub make_new_entry()
Dim Layer_Increment, i As Integer
Set Layer_Increment = Cells(7, 2)
y = 10 'Set this to the first row number that gets a time entered.
i = 0
Do While i = 0
If Cells(y, 1) = "" Then
add_row y
Cells(y, 1) = Format(Now(), "m/d/yy")
Cells(y, 2) = Format(Now(), "h:mm")
Cells(y, 3) = Cells(y - 1, 2) - ((Cells(y - 1, 2) - Layer_Increment * (y - 10)))
Cells(y, 8).Formula = "=($H$7*(C" & y & "))+((0.5*'Data Reference'!F2)+(0.4*'Data Reference'!F3)+(0.3*'Data Reference'!F4)+(0.2*'Data Reference'!F5)+(0.1*'Data Reference'!F6)+(0.05*'Data Reference'!F7)+(0.01*'Data Reference'!F8)+(0.001*'Data Reference'!F9))+ $H$10+$N$7"
Cells(y, 9).Formula = "=$I$10-(0.05*(C" & y & "-$C$10))"
Cells(y, 10).Formula = "=((((A" & y & ") + (B" & y & "))-((A" & y - 1 & ")+(B" & y - 1 & ")))*1440)/((C" & y & ") - (C" & y - 1 & "))"
Cells(y, 11).Formula = "=((B" & y & ") + (A" & y & ")) - ($B$10+$A$10)"
Cells(7, 6).Formula = "=((130-(H" & y & "))/$H$7)-('Build Information'!$F$6-(C" & y & "))"
Cells(7, 4).Formula = "=Max(C:C)"
Cells(1, 15).Formula = "=(J" & y - 1 & ")+1.5"
Cells(7, 10).Formula = "=AverageIf(J11:(J" & y & ")," < " &O1)"
i = 1
Else
y = y + 1
End If
Loop
End Sub}
Can anyone help me figure out what Im doing wrong.
You need to double up the quotes around the < to actually include it in the string (it is currently outside the quotes and thus a comparison operator). Then, your parentheses are off:
Cells(7, 10).Formula = "=AverageIf(J11:J" & y & ",""<""&O1)"
I have cells that are supposed to be 0 I believe. 7.45058059692383E-12.
How do I make these 0 in my code?
Sheets("MainData").Range("C" & i) = Sheets("CM_MainData").Range("C" & i)
I thought converting NumberFormat to 0 may help but it did not work.
Sheets("MainData").Range("C" & i) = Sheets("CM_MainData").Range("C" & i).NumberFormat = "0.00"
Instead the result is FALSE
Please Help!
Full code:
Sub CopyData()
Worksheets("MainData").Rows("2:" & Rows.Count).ClearContents
'Copy data from the CM Commentary File to Template
Application.ScreenUpdating = False
Set MainDataCM = Workbooks.Open(Sheets("Input").Range("B3") & Sheets("Input").Range("B6"))
MainDataCM.Sheets("Main Data").Copy After:=ThisWorkbook.Sheets(1)
MainDataCM.Close savechanges:=False
Application.ScreenUpdating = True
Sheets("Main Data").Name = "CM_MainData"
Worksheets("CM_MainData").Visible = False
'Read the CM_MainData tab and copy the required columns in the MainData tab
Dim k As Long
k = Sheets("CM_MainData").Range("A1", Sheets("CM_MainData").Range("A1").End(xlDown)).Rows.Count
Debug.Print (k)
i = 2
While i <= k
Sheets("MainData").Range("A" & i) = Sheets("CM_MainData").Range("A" & i)
Sheets("MainData").Range("B" & i) = Sheets("CM_MainData").Range("B" & i)
Sheets("MainData").Range("C" & i) = Sheets("CM_MainData").Range("C" & i).NumberFormat = "0.00"
Sheets("MainData").Range("D" & i) = Sheets("CM_MainData").Range("D" & i)
Sheets("MainData").Range("E" & i) = Sheets("CM_MainData").Range("C" & i) * 1000
Sheets("MainData").Range("F" & i) = Sheets("CM_MainData").Range("H" & i)
'Sheets("MainData").Range("E" & i).NumberFormat = "0.00"
If Sheets("MainData").Range("F" & i) = "" Then
Sheets("MainData").Range("F" & i) = "RBC INVESTOR SERV O/H & MISC"
End If
i = i + 1
Wend
Worksheets("Macro").Activate
Worksheets("Macro").Select
MsgBox "Step 1 Completed"
End Sub
Value transfer:
Sheets("MainData").Range("C" & i).Value = Sheets("CM_MainData").Range("C" & i).Value
Number format (a separate step, and does not change the underlying value):
Sheets("MainData").Range("C" & i).NumberFormat = "0.00"
Or use WorksheetFunction.Round (does change the underlying value).
Sheets("MainData").Range("C" & i).Value = WorksheetFunction.Round(Sheets("CM_MainData").Range("C" & i).Value, 2)
I am doing an average of data (VBA Excel) as below:
If n < 8 Then
Sheet2.Cells(i, 20).Value = "=SUM(E" & i & ":S" & i & ")/" & n
Else
Sheet2.Cells(i, 20).Value = "=AVERAGE(LARGE(E" & i & ":S" & i & ", {1,2,3,4,5,6,7,8}))"
End If
n = 0
This code is working, because I want the best 8 values of 15 values.
My question is how to do the same with x values (x will be introduced via a userform, 0<x<16).
Of course, I could use a Select Case with 15 lines depending of x, but it does not seem to me a good coding.
Any ideas?
If AVERAGEIF is available:
Sheet2.Cells(i, 20).Value = "=AVERAGEIF(E" & i & ":S" & i & ","">=""&LARGE(E" & i & ":S" & i & "," & x & "))"
For i = 2 and x = 8, the formula is:=AVERAGEIF(E2:S2,">="&LARGE(E2:S2,8))
If not:
Sheet2.Cells(i, 20).Value = "=SUMIF(E" & i & ":S" & i & ","">=""&LARGE(E" & i & ":S" & i & "," & x & "))/" & x
For i = 2 and x = 8, the formula is:=SUMIF(E2:S2,">="&LARGE(E2:S2,8))/8