Hi (T_T),
you cannot compare the values of cells in a range in one go (except using Evaluate) but should loop through the cells instead. And I doubt the next error would occur as rows 4 and 5 are text.
Later on in the code you refer to a with statement which is not provided as well as no information about what ranges "peak shaving" and "battery" refer to and what pkSh is.
Maybe you should give this code a try:
Private Sub CommandButton1_Click()
Dim bl As Long
Dim mx As Long
Dim pw As Range
Dim LastColumn As Long
Dim str As Long
Dim pkSh As Long
Dim rCell As Range
Dim oCTRL As Object
LastColumn = Sheets("Tabelle1").Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
str = 0
mx = TextBox_maxP * TextBox_PeakS / 100
bl = TextBox_maxP * TextBox_baseL / 100
Set pw = Sheets("Tabelle1").UsedRange.Find("Power", , , 1, 1)
Set pw = Range(pw, pw.End(xlDown))
Set pw = pw.Offset(2).Resize(pw.Rows.Count - 2, pw.Columns.Count)
For Each rCell In Range(pw, pw.End(xlDown))
With rCell
If .Value >= mx Then
.Value = -bl
str = str + 1
ElseIf .Value < bl Then
.Value = bl
End If
End With
Next rCell
'/// No named ranges for peak Shaving nor battery
'/// could be replaced by Column letters
'/// no With set for the ranges so either omitt the dot or refer to a range
'/// what is pkSH, which range do you rely on as you want to fill in a value?
' .Range("peak shaving" & LastColumn + 1) = pkSh
' .Range("battery" & LastColumn + 2) = .Value
Sheets("Tabelle1").Range("K1").Value = str
For Each oCTRL In Me.Controls
If TypeName(oCTRL) = "Textbox" Then
oCTRL.Value = vbNullString
End If
Next oCTRL
Set pw = Nothing
End Sub
Please be informed that the last For...Next loop clears the contents of the userform if you want further entries to be made (or simply use Unload.Me for just one entry).
Ciao,
Holger
Bookmarks