+ Reply to Thread
Results 1 to 7 of 7

Invalid use of a property-error

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-05-2020
    Location
    Germany
    MS-Off Ver
    2013
    Posts
    100

    Invalid use of a property-error

    Hi,

    I have this that should take the column and check if each value are greater than mx, if so then subtract bl from them and return them in new column. Then check them again and if Value is smaller than bl then change that value to bl and return in next column.

    but it doesn't work and I keep getting "Invalid use of a property" error.

    p.s. str supposted to count how many values are greater than mx.

    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
     
    
    LastColumn = ActiveSheet.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
     
    
     str = 0
     
     mx = TextBox_maxP * TextBox_PeakS / 100
     bl = TextBox_maxP * TextBox_baseL / 100
     
    Set pw = UsedRange.Find("Power", , , 1, 1).Columns
      
     With Range(pw, pw.End(xlDown))
        If .Value >= mx Then .Value -bl
            str = str + 1
        ElseIf .Value < bl Then .Value = bl
        End If
    End With
        
     Next
     .Range("peak shaving" & LastColumn + 1) = pkSh
     .Range("battery" & LastColumn + 2) = .Value
     Range("K1").Value = str
    
    End Sub
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    06-27-2010
    Location
    sYRIA
    MS-Off Ver
    Excel 2013
    Posts
    669

    Re: Invalid use of a property-error

    Hi
    Try
    mx = TextBox_maxP.Value * TextBox_PeakS.Value / 100
     bl = TextBox_maxP.Value * TextBox_baseL.Value / 100

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Invalid use of a property-error

    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
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Forum Contributor
    Join Date
    05-05-2020
    Location
    Germany
    MS-Off Ver
    2013
    Posts
    100

    Re: Invalid use of a property-error

    Quote Originally Posted by HaHoBe View Post
    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
    Thanks
    What does this part do?
    Set pw = pw.Offset(2).Resize(pw.Rows.Count - 2, pw.Columns.Count)

  5. #5
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Invalid use of a property-error

    Hello.
    Is the 'Maximum Power' value calculated as the largest value in column C or will you write it "by hand"?

  6. #6
    Forum Contributor
    Join Date
    05-05-2020
    Location
    Germany
    MS-Off Ver
    2013
    Posts
    100

    Re: Invalid use of a property-error

    Quote Originally Posted by beyond Excel View Post
    Hello.
    Is the 'Maximum Power' value calculated as the largest value in column C or will you write it "by hand"?
    The user will give the max.

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Invalid use of a property-error

    Hi (T_T),

    Set pw = pw.Offset(2).Resize(pw.Rows.Count - 2, pw.Columns.Count)
    Set the range down 2 rows and resize the range to be 2 rows short of the original range (exclude the headers) and match the number of columns.

    BTW the first error was raised by
     With Range(pw, pw.End(xlDown))
        If .Value >= mx Then .Value -bl
            str = str + 1
        ElseIf .Value < bl Then .Value = bl
        End If
    End With
    as an = was missing. Adding this will lead to a new error as your If-clauses are different from what the compiler expects.

    Ciao,
    Holger

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Could not set the RowSource property. Invalid Property Value error when switching bet
    By sathyaganapathi in forum Access Programming / VBA / Macros
    Replies: 6
    Last Post: 08-03-2021, 06:54 AM
  2. [SOLVED] Error 381Could not set the List property. Invalid property array index
    By desonny in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-16-2020, 08:33 AM
  3. Runtime error 381: Could not set the list property.Invalid property array index
    By rohith4prithvi in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-23-2014, 10:24 PM
  4. Run-time Error '380': Could not set the RowSource property. Invalid property value
    By Rem0ram in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-26-2013, 03:57 PM
  5. [SOLVED] Excel run-time error '381': Could not set the List property. Invalid property array index
    By eemiller1997 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-20-2012, 12:48 PM
  6. Replies: 2
    Last Post: 06-11-2012, 03:03 PM
  7. Runtime Error 380 – Could not set the list property. Invalid property value
    By BernzG in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-21-2005, 05:10 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1