+ Reply to Thread
Results 1 to 22 of 22

Whats wrong with this for searching a range in a worksheet?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Crayford, kent
    MS-Off Ver
    Excel 2013
    Posts
    394

    Whats wrong with this for searching a range in a worksheet?

    Hi

    Im not the best on coding, and have done this before, but cant seem to get it to work!! Usually i just use simple codes that allow lists to generate based on other values, but now i need to search for a specific row in a sheet to add a value to that row in a specific column.

    My code is...

    lastrow = Worksheets("Data2").Range("D1").End(xlDown).Row
    Should it be
    worksheet("Data2")
    or

    Range.("D1")
    Could someone let me know whats wrong or missing please, ive tried a few different codes but keep getting the run-time error '9' message

    Thank you Dan

  2. #2
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Whats wrong with this for searching a range in a worksheet?

    If you have error 9 then your sheet name is not "Data2". Perhaps "Data 2" with space?

  3. #3
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Crayford, kent
    MS-Off Ver
    Excel 2013
    Posts
    394

    Re: Whats wrong with this for searching a range in a worksheet?

    Nope its definatley Data2

    Capital D no space

  4. #4
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Crayford, kent
    MS-Off Ver
    Excel 2013
    Posts
    394

    Re: Whats wrong with this for searching a range in a worksheet?

    lastrow = Worksheets("Data2").Range("D1").End(xlDown).Row
    For I = 1 To lastrow
    If UserForm1.ComboBox4.Text = Range("D" & I).Value Then
    ask = I
    Exit For
    End If
    Next I
    Range("P" & ask).Value = Range("P" & ask).Value + UserForm1.TextBox65.Value
    this is the full code

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Whats wrong with this for searching a range in a worksheet?

    Are there any other workbooks open when you get the error?
    If posting code please use code tags, see here.

  6. #6
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Crayford, kent
    MS-Off Ver
    Excel 2013
    Posts
    394

    Re: Whats wrong with this for searching a range in a worksheet?

    Yeah one, i didnt know that could make a difference, il try it now....

  7. #7
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Whats wrong with this for searching a range in a worksheet?

    May be you need:
    lastrow = ActiveWorkbook.Worksheets("Data2").Range("D1").End(xlDown).Row
    or ThisWorkbook if the code is in the same workbook.

  8. #8
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Crayford, kent
    MS-Off Ver
    Excel 2013
    Posts
    394

    Re: Whats wrong with this for searching a range in a worksheet?

    Ok i will change it to that, thank you, it does work when only that workbook is open and no more runtime error 9, but..... now on this line

    Range("P" & ask).Value = Range("P" & ask).Value + UserForm1.TextBox65.Value
    i have error 1004 and range global, should the "P" be changed to "16" usually when im transferring data from a userform i will have to change cell D6 to (6, 4) so would p need changing to 16

  9. #9
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Whats wrong with this for searching a range in a worksheet?

    You must check if ask is number greater than 0:
    If ask > 0 then Range("P" & ask).Value = Range("P" & ask).Value + UserForm1.TextBox65.Value
    When you call Range property you must use the column letter. When you call Cells you may use column letter or number equally.

  10. #10
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Crayford, kent
    MS-Off Ver
    Excel 2013
    Posts
    394

    Re: Whats wrong with this for searching a range in a worksheet?

    Ok well the errors are gone now, but the value of TextBox65 is not being added into the Data2 sheet, is there an easier method to search the row for the value in Combobox4 and add the value from Textbox65 into the corresponding cell in column P?

    Say D10 = 01/12/2013 and my combobox4 value = 01/12/2013 then in my textbox65 is $4000 i want $4000 in P10?.....

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Whats wrong with this for searching a range in a worksheet?

    No you shouldn't make that change.

    The problem is with ask, check it's value when you get the error, my bet is it's 0.

    The reason it might be 0 is because a match wasn't found in the loop, and the reason for that could be because the code isn't looking at the right sheet because you have no worksheet/workbook reference for Range("D"&I) in the loop.
    Last edited by Norie; 12-13-2013 at 07:06 AM.

  12. #12
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Crayford, kent
    MS-Off Ver
    Excel 2013
    Posts
    394

    Re: Whats wrong with this for searching a range in a worksheet?

    Ah ok..... il give that a try...

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Whats wrong with this for searching a range in a worksheet?

    How are you populating the combobox?

  14. #14
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Crayford, kent
    MS-Off Ver
    Excel 2013
    Posts
    394

    Re: Whats wrong with this for searching a range in a worksheet?

    Yes, its looking at a named range in that data2 tab

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Whats wrong with this for searching a range in a worksheet?

    What's the name of the named range?

  16. #16
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Crayford, kent
    MS-Off Ver
    Excel 2013
    Posts
    394

    Re: Whats wrong with this for searching a range in a worksheet?

    Weekcommencing

  17. #17
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Whats wrong with this for searching a range in a worksheet?

    How is it defined?

  18. #18
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Crayford, kent
    MS-Off Ver
    Excel 2013
    Posts
    394

    Re: Whats wrong with this for searching a range in a worksheet?

    its just the range
    Formula: copy to clipboard
    =Data2!$D$2:$D$58
    then named "WEEKCOMMENCING" in the name box

  19. #19
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Whats wrong with this for searching a range in a worksheet?

    Try this.
     
    idx = Combobox4.ListIndex
    
    If idx = -1 Then Exit Sub
    
    ask = idx + 2
    
    With ThisWorkbook.Worksheets("Data2")
          .Range("P" & ask).Value = .Range("P" & ask).Value + Val(UserForm1.TextBox65.Value)
    End With

  20. #20
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Crayford, kent
    MS-Off Ver
    Excel 2013
    Posts
    394

    Re: Whats wrong with this for searching a range in a worksheet?

    Bang on!!! I had to do one little thing because i wasnt removing the old value to change to the new rate


    idx = Combobox4.ListIndex
    
    If idx = -1 Then Exit Sub
    
    ask = idx + 2
    
    With ThisWorkbook.Worksheets("Data2")
          .Range("P" & ask).Value = Val(UserForm1.TextBox65.Value)
    End With
    Thanks yet again Norie

  21. #21
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Crayford, kent
    MS-Off Ver
    Excel 2013
    Posts
    394

    Re: Whats wrong with this for searching a range in a worksheet?

    Basically i copied this code

    idx = Combobox4.ListIndex
    
    If idx = -1 Then Exit Sub
    
    ask = idx + 2
    
    With ThisWorkbook.Worksheets("Data2")
          .Range("P" & ask).Value = Val(UserForm1.TextBox65.Value)
    End With
    and changed it to

    idx1 = Combobox5.ListIndex
    
    If idx1 = -1 Then Exit Sub
    
    ask = idx1 + 2
    
    With ThisWorkbook.Worksheets("Data2")
          .Range("P" & ask).Value = Val(UserForm1.TextBox66.Value)
    End With
    As it wouldnt let me duplicate idx, but doesnt seem to work...

  22. #22
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Crayford, kent
    MS-Off Ver
    Excel 2013
    Posts
    394

    Re: Whats wrong with this for searching a range in a worksheet?

    Hi Norie

    Im wondering if you would be able to give me a point in the right direction? furthering on from the above, if i have two ranges in one column, and i only want what is in the text box to go to those specific cells in column P relating to the range, is it possible to do this?

    Say Range 1 = A1 to A20 and Range 2 = A21 to A40, Textbox1 relates to Range1 and Textbox 2 relates to Range2, If i enter something in Textbox2 i cant suss how to get it into P21 to P40 depending on the combox value.

    Is this possible?

    Dan

+ 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. Whats wrong with my formula?
    By DKerr in forum Excel General
    Replies: 2
    Last Post: 09-05-2006, 07:02 AM
  2. whats wrong with if??
    By duncan79 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-31-2006, 05:20 PM
  3. [SOLVED] Whats wrong with this?
    By Metrazal in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-24-2006, 12:00 PM
  4. Whats wrong with this?
    By LucasBuck in forum Excel General
    Replies: 3
    Last Post: 01-12-2006, 04:15 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