+ Reply to Thread
Results 1 to 43 of 43

Code For Button To Store Data

Hybrid View

  1. #1
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Code For Button To Store Data

    Hello,

    I have a Record Workbook if you will which I'm trying to set up to store information. It's attached below.

    Each record has a number. Each record is stored in the Historical List, and the Current List shows only the information of the latest entry of each record.

    Basically what I'm trying to achieve is the following:

    The Record Workbook will open to the Entry Page. When I enter a record number, my code needs to check if this number is already stored in the Historical List. If it is, then all the information from the latest entry of that particular record number should be displayed. I can make changes if I like, and upon hitting the Submit button, I should be asked if I'm sure I want to enter an update to this current record number. If I say Yes, then all the information I just added gets put into the Historical List, the Current List gets updated from there, and my Entry Page is empty once more. If I say No, then the Entry Page would just clear.

    Is this difficult to achieve?
    Attached Files Attached Files
    There is so much good in the worst of us,
    And so much bad in the best of us,
    That it hardly behooves any of us
    To talk about the rest of us.

  2. #2
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Code For Button To Store Data

    hi there, please check attachment, enter data in B2 on Entry Page. If nothing happens, run the code "Enable_Events" first.

    The data on Historical List sheet is supposed to be sorted descending by date within unique record number as in a sample file.

    The codes used are in Entry Page sheet and Module1.
    Attached Files Attached Files

  3. #3
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Code For Button To Store Data

    Never mind...I see Watersev understands.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  4. #4
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code For Button To Store Data

    jaslake, thank you for your input - what gets entered into the cells makes absolutely no difference at this point, the only important thing right now is the actual record number. This is only a sample - I will be using the final code on my actual workbook once we get it perfected.

    watersev, thank you so much for your help in this matter, I've been trying to get this working for quite a while now. I realized that I missed a few things in my explanation, as several functions aren't quite working the way they should, but I'll try to explain as best I can:

    The Record Workbook will open to the Entry Page. When I enter a record number, my code needs to check if this number is already stored in the Historical List. If it is, then all the information from the latest entry of that particular record number should be displayed.
    This is almost working like it should - but instead of the newest record on the Historical List being displayed, I think it would be better to have the code search the Current List and display what is listed there. I don't think we're using the Current List at all at the moment, as it isn't displaying the latest information from the Historical List like it should - but I think this would make things a bit simpler.

    I am also wondering if when I enter a current record number, it wouldn't physically copy and paste, as I found that having Cells B3:B10 all selected is a bit distracting, and if I hit paste (which I accidentally did) a whole row of information from the Historical List gets copied onto the Entry Page.

    I can make changes if I like, and upon hitting the Submit button, I should be asked if I'm sure I want to enter an update to this current record number. If I say Yes, then all the information I just added gets put into the Historical List, the Current List gets updated from there, and my Entry Page is empty once more.
    Here's where I should have been a bit clearer - for each record number, an 'update' would be an entirely new addition to the Historical List under that specific number. The whole purpose of the Historical List is to show each and every time that number gets re-entered. So, instead of the Submit button changing the last entry on the Historical List, it would add a new line of information for that record. And the Current List would be updated to show the newest addition to that record number.

    Also, if I enter a new record number altogether in the Entry Page (one that isn't listed as of yet on the Historical List), I'm unable to submit it; it should be added as a new line on the Historical List, which should also be added to the Current List.

    If I say No, then the Entry Page would just clear.
    This works beautifully.

    I hope I explained that better, forgive me for not being clearer earlier. If you have any more questions, please don't hesitate to ask. And thank you so very much for your help so far!

  5. #5
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Code For Button To Store Data

    sorry for delayed reply, please check attachment
    Attached Files Attached Files

  6. #6
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code For Button To Store Data

    watersev, first of all, thanks a MILLION for all your amazing work, and don't even bother mentioning delayed responses. The wait was very well worth it! You fixed most of the bugs just like that! It's so exciting finally getting this workbook somewhere.

    The only issue I can still find after testing it out is when I try entering a new record number in Cell B2 on the Entry Page. After entering a new number, no information is displayed below it - I imagine that's because the code can't find that number on the Current List - which is great - but when I click Submit, I get the message:

    You are trying to update not existing record #X on the Historical List sheet. Please change record number and try again.
    I need to be able to add new records. Perhaps what we could do is have a message box appear when a new number is entered into Cell B2 that say something like:

    The number you entered is not yet associated with a record. Would you like to create a new one?
    ..and then upon hitting 'Yes', I could enter the rest of the information, hit 'Submit', and the new record would be added as a new line on the Historical List, which would then also be added to the Current List.

    What do you think?

  7. #7
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Code For Button To Store Data

    adding new record was not stipulated originally that's why it was marked with that message.

    Please check attachment, new record is added in the first empty row after current data on both sheets.

    Logic for now: if you confirm adding new record, the record will be added as is and update can be done later or you can reject adding new record, input more data and submit again.
    Attached Files Attached Files
    Last edited by watersev; 03-05-2012 at 05:23 PM.

  8. #8
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code For Button To Store Data

    Thank you once again for being so helpful, watersev!

    Yes, I realize I was not clear enough about adding a new line in my first post, I apologize for any inconveniences that may have caused. It's working much better now, though! I have a few more questions after testing your latest attachment:

    Are we able to have the new record line added in the correct numerical order on both the Historical and Current Lists? I'm not sure how much this would involve.. Adding a second record for the same number always goes in the correct order in the Historical List, which is swell. It's just the first new entry that would need to be placed in the correct order now. Let me know what you think though.

    While most of our records begin with a number, a few of them have letters and dashes mixed in, such as LT-003. I can enter a new record in this format just fine(apart from the numerical/alphabetical order issue I just mentioned), but when I try to update it, I get this message: Run-time error '1004': Insert method of Range class failed. If I select 'debug', this code is highlighted:
    HLsearch.EntireRow.Insert xlDown, HLsearch
    Is this able to work for us as well?

    And I have one final question - Is it possible to have a record's latest data which is displayed in the Current List linked to the matching information in the Historical List? I can set up the Current List so that no changes can be made there, but if corrections, for example, do need to be made, I'd like the user to be able to do so in the Historical List. I'm wondering whether changes there to any entry also displayed in the Current List could be linked to each other.

    Please let me know of any concerns regarding any of this. Thank you so much again for all your help!

  9. #9
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Code For Button To Store Data

    please check attachment
    Attached Files Attached Files

  10. #10
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code For Button To Store Data

    watersev, this is amazing! I am thrilled, to say the least. Thank you!

    I do notice that the values displayed on the Current List are not centered vertically in the cells. When I change it manually, they jump back down after making other entries. Do you think this could be fixed in the code? Other than that, I cannot find any more issues with this workbook, though I may have more questions about it once I start to customize it for my use.

    I truly am very excited, it's so good to have this finally working! Thank you so so much again!

  11. #11
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Code For Button To Store Data

    re vertical alignment:

    1.
    Private Sub Worksheet_Deactivate()
    ...
           With Sheets("Current List")
            
                .UsedRange.Offset(2).Clear
                
                With .Range("a3").Resize(j, 9)
                
                    .Value = result
                    
                    .Borders.LineStyle = xlContinuous
                    
                    .HorizontalAlignment = xlCenter
                    
                    .VerticalAlignment = xlVAlignCenter
         
                End With
            
            End With
    ...
    2.

    Sub Submit_button_click()
    ...
    
              For Each shname In Split("Current List,Historical List", ",")
                
                    With Sheets(shname)
                    
                        Set lastcell = .Cells(Rows.Count, 1).End(xlUp)
                    
                        With lastcell.Resize(, 9)
                
                            .Copy lastcell.Offset(1)
                
                            .Offset(1).Value = temp
                    
                        End With
                        
                     With .Range("a3", .Cells(Rows.Count, "a").End(xlUp)).Resize(, 9)
                        
                            .Sort key1:=.Range("a3"), key2:=.Range("d3"), order2:=xlDescending, header:=xlNo
                            
                            .VerticalAlignment = xlVAlignCenter
                            
                        End With
                        
                    End With
                
                Next
    
    ...

  12. #12
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code For Button To Store Data

    This is great, watersev! It works very nicely indeed.

    I've been trying to use this code on my actual worksheet, and have been having issues, however. The actual worksheets have several more data cells to the right and down. I tried to adjust the code accordingly, but I am apparently missing a few things, as you can see when you try out my attachments.

    I attached two samples; one with the code that I can't seem to get right, and the other without code to show how it's supposed to look. I should mention that the layout for the latter attachment (Test List Desired) is almost exactly how I need it, with maybe a few formatting fixes I need to do yet, but nothing that should affect the code - and you'll notice that I am using conditional formatting on the cells on the Current List. This should still work once we apply our code, and it would be preferable to have the Entry Page also display the current colour of the information belonging to whatever record number (Value 1) is entered into Cell C2.

    Would you mind taking a look at it and letting me know what I'm doing wrong, and what needs to be fixed? I'd love to learn! Thanks so very much again!

    PS Come to think of it, if we applied conditional formatting to Cell C8 on the Entry Page to be the same as the conditional formatting on the Current List, that may be all we need for that issue..
    Attached Files Attached Files
    Last edited by swordswinger710; 03-19-2012 at 09:13 AM.

  13. #13
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Code For Button To Store Data

    please check attachment, if I understood correctly the format issue for Entry Page

  14. #14
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code For Button To Store Data

    watersev, thank you so much for your reply - upon opening the attachment, I get this error:

    This workbook contains links to other data sources.
    If I click Update, it says:

    This workbook contains one or more links that cannot be updated.
    Then I have the options of Continue, or Edit Links.

    Is something possibly wrong with the Submit button?
    Last edited by swordswinger710; 03-21-2012 at 11:15 AM.

  15. #15
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Code For Button To Store Data

    something wrong either with the site uploading service or Excel converter. Anyway the code that was changed is Entry Page code:

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim CLsearch As Range
    
    If Not Intersect(Target, Range("c2")) Is Nothing Then
        
        With Application
            
            .ScreenUpdating = 0
            .EnableEvents = 0
            
            Range("c3:c25").ClearContents
            
            Set CLsearch = Sheets("Current List").UsedRange.Find(Target, , xlValues, xlWhole, , xlNext)
        
            If Not CLsearch Is Nothing Then
                
                CLsearch.Resize(, 24).Copy
               
                Range("c2").PasteSpecial Paste:=xlPasteAll, Transpose:=True
                      
            End If
            
            Range("c2").Select
                    
            .EnableEvents = 1
            .ScreenUpdating = 1
            
        End With
        
    End If
    
    End Sub

  16. #16
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code For Button To Store Data

    Hmm, that still doesn't want to work for me. When I open the workbook I attached last, from my desktop - not the one I downloaded from you - I get that silly 'contains links to other workbooks' message. I have a hunch that the Submit button is somehow linked to our working Sample Record workbook as per Post #11, for when I tried to apply the code to my actual workbook, I just copied the button from there - which was perhaps the wrong thing to do, but I don't know any other way.

    The reason I think it's the button is if I right-click it on the Actual Test List and hit Assign Macro, the macro name is
    'C:\Users\swordswinger710\Desktop\Sample Record Workbook.xls'!Submit_button_click
    What do I do?
    Last edited by swordswinger710; 03-21-2012 at 11:56 AM.

  17. #17
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Code For Button To Store Data

    try firstly to save file, open it and try again sometemies that helps, check PM

  18. #18
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code For Button To Store Data

    None of that seems to help.. and I'm not sure what PM is?

    I'm sorry watersev, I think I'd better let you do the code transfer. I've attached the Working Record Workbook, as well as the Required Record Workbook. The Required workbook has no code, and no button. I thought I'd be able to transfer the code from the Working workbook over to this one myself, but I'm obviously not that great at it yet, as it's such a mess and giving us too much trouble.

    I know you've done so much for me already, but do you think you'd be able to get our currently working code transferred to the Required workbook before we try to do anything else?
    Attached Files Attached Files
    Last edited by swordswinger710; 03-21-2012 at 01:18 PM.

  19. #19
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Code For Button To Store Data

    please check attachment, option using conditional format (I missed that point at first as in Excel 2003 I did not find any CF applied)
    Attached Files Attached Files

  20. #20
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code For Button To Store Data

    Thanks yet again, watersev - I was able to open it without any errors from Dropbox.. but when I click Submit now, I always get the error:

    The file could not be accessed. Try one of the following:
    Make sure the specified folder exists.
    Make sure the folder that contains the fie is not read-only.
    Make sure the file name does not contain any of the following characters...
    Make sure the file/path name doesn't contain more than 218 characters.
    What on earth is going on?

  21. #21
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Code For Button To Store Data

    forget the dropbox file, check file in post #19, the reason for that error is that you need to reassign macro to a button. I suppose the root of that is algorithm DB is working.

  22. #22
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code For Button To Store Data

    How do I reassign a macro to a button? I think this would solve the issue as well, but I am not familiar with buttons..
    Last edited by swordswinger710; 03-22-2012 at 08:08 AM.

  23. #23
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Code For Button To Store Data

    though they describe how to assign macro to a Forms Option button, the algorithm is the same for a Forms button as well: http://support.microsoft.com/kb/141689

  24. #24
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code For Button To Store Data

    Ah! Well, that was easy. Okay, so the conditional formatting on the Data Entry Page is working well, by the looks of things.

    The only issue I see now is the formatting of the Current List. It seems to work at first, but then after going to the Historical Sheet and back again, Row 3 is erased, as well as the thick vertical border lines, and down at the bottom of the list, two rows are cleared as well. The conditional formatting on this sheet is also erased, and all the cells have no colour whatsoever.

    Do you see what I mean? Is this fixable?

  25. #25
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Code For Button To Store Data

    hi there, this should address the issue (changed line is underlined)

    the code for "Historical list"

    Public HLchange As Boolean
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    HLchange = True
    End Sub
    
    Private Sub Worksheet_Deactivate()
    
    Dim data, result, rcount As Long, ccount As Long, i As Long, n As Long, j As Long, strchk As String
    
    If HLchange = True Then
    
        With Sheets("Historical List")
            
            data = .Range("a4", .Cells(Rows.Count, "a").End(xlUp).Offset(, 23))
            
        End With
        
        rcount = UBound(data)
        ccount = UBound(data, 2)
             
        ReDim result(1 To rcount, 1 To ccount)
            
        For i = 1 To rcount
        
            If InStr(strchk, "|" & data(i, 1) & "|") = 0 Then
            
                j = j + 1
                
                For n = 1 To ccount
                    result(j, n) = data(i, n)
                Next
            
                strchk = strchk & "|" & data(i, 1) & "|"
            
            End If
            
        Next
        
        If j > 0 Then
        
            With Sheets("Current List")
            
                .UsedRange.Offset(3).ClearContents
                
                With .Range("a4").Resize(j, 24)
                
                    .Value = result
                    
                    .Borders.LineStyle = xlContinuous
                    
                    .HorizontalAlignment = xlCenter
                    
                    .VerticalAlignment = xlVAlignCenter
                    
                End With
            
            End With
            
        End If
    
        HLchange = False
    
    End If
    
    End Sub

  26. #26
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code For Button To Store Data

    Yay! That fixed the erasing of Row 3 on the Current List, as well as the conditional formatting. Thank you!

    The only problem remaining that I can see is when I go from the Historical List to the Current List, all the thick borders I had as shown in the Test List Desired in Post #12 disappear. Is there a way to keep these?

  27. #27
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Code For Button To Store Data

    generally it's not a good practice to format the whole sheet though only a hundred rows is used. I've simplified the code for "Historical sheet":

    Public HLchange As Boolean
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    HLchange = True
    End Sub
    
    Private Sub Worksheet_Deactivate()
    
    If HLchange = True Then
        
        Application.ScreenUpdating = 0
        Application.EnableEvents = 0
        
        With Sheets("Historical List")
        
            lrow = .Cells(Rows.Count, 1).End(xlUp).Row
            
            .Range("y4:y" & lrow) = "=if(countif(A$4:A4,A4)=1,1,0)"
            
            Sheets("Current List").UsedRange.Offset(3).ClearContents
            
            With .UsedRange
            
                .AutoFilter 25, 1
                .Offset(3).Resize(, 24).Copy
                Sheets("Current List").Range("a4").PasteSpecial xlPasteValues
                .AutoFilter
                
            End With
            
            .Range("y4:y" & lrow).ClearContents
            
        End With
        
        HLchange = False
        
        Application.EnableEvents = 1
        Application.ScreenUpdating = 1
        
    End If
    
    End Sub
    Last edited by watersev; 03-28-2012 at 05:38 AM.

  28. #28
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code For Button To Store Data

    watersev, it keeps the bordering format now, which is sweet! The only thing I'd ask to be changed is to somehow not have the Current List cells selected when I come to it from the Historical List, if that's that not too difficult to fix. I think we've almost got it!

  29. #29
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Code For Button To Store Data

    try this:

    Public HLchange As Boolean
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    HLchange = True
    End Sub
    
    Private Sub Worksheet_Deactivate()
    
    If HLchange = True Then
        
        Application.ScreenUpdating = 0
        Application.EnableEvents = 0
        
        With Sheets("Historical List")
        
            lrow = .Cells(Rows.Count, 1).End(xlUp).Row
            
            .Range("y4:y" & lrow) = "=if(countif(A$4:A4,A4)=1,1,0)"
            
            Sheets("Current List").UsedRange.Offset(3).ClearContents
            
            With .UsedRange
            
                .AutoFilter 25, 1
                .Offset(3).Resize(, 24).Copy
                Sheets("Current List").Range("a4").PasteSpecial xlPasteValues
                .AutoFilter
                
            End With
            
            .Range("y4:y" & lrow).ClearContents
            
        End With
        
        HLchange = False
        
        Sheets("Current List").Range("a1").Activate
        
        Application.EnableEvents = 1
        Application.ScreenUpdating = 1
        
    End If
    
    End Sub

  30. #30
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code For Button To Store Data

    watersev, you've been extremely helpful, and I am so thankful for all your assistance. This is amazing. Everything seems to be working without a hitch now. Thank you a million times over! Bless you, man.

  31. #31
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code For Button To Store Data

    Oooh, and I think I found one more tiny bug. *Sigh*

    When I clear Cell C2 of the Entry Page after viewing a record, the previous data from cells Cell C7 and Cell C8 are shifted up one cell and are now displayed in Cell C6 and C7. Very odd. I hope this isn't too much more work to fix.

    I've attached our workbook as far as we've got it.
    Attached Files Attached Files

  32. #32
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Code For Button To Store Data

    unforunately I can not open your attachment and can not reproduce same event in my file. Can you save file with .xls extension and repost it?

  33. #33
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Code For Button To Store Data

    anyway try to update the sheet "Entry Page" code with:

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim CLsearch As Range
    
    If Not Intersect(Target, Range("c2")) Is Nothing Then
        
        With Application
            
            .ScreenUpdating = 0
            .EnableEvents = 0
            
            Range("c3:c25").ClearContents
             
            If Target = "" Then
                
                .EnableEvents = 1
                .ScreenUpdating = 1
                Exit Sub
                
            End If
             
            Set CLsearch = Sheets("Current List").UsedRange.Resize(, 1).Find(Target, , xlValues, xlWhole, , xlNext)
        
            If Not CLsearch Is Nothing Then
                
                CLsearch.Resize(, 24).Copy
               
                Range("c2").PasteSpecial Paste:=xlPasteValues, Transpose:=True
                
                Range("c3:c25").HorizontalAlignment = xlCenter
            
            End If
            
            Range("c2").Select
                    
            .EnableEvents = 1
            .ScreenUpdating = 1
            
        End With
    
    End If
    
    End Sub

  34. #34
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code For Button To Store Data

    That's odd that you can't open it - I wasn't able to save it with that extension either - but that fixed it very nicely! I can't find any more issues. Thank you a million again!

  35. #35
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code For Button To Store Data

    Hmm, I'm coming across a few more bugs:

    If I enter a new record as 1, for example, then submit and go to the Current List, it is there, but it's not in the right format for some reason. Then if I go to the Historical List, it is there again, but once more, not in the right format. When I go back to the Current List, it has disappeared...

    Any ideas why?

    Another thing - would it work to do this in Cell 25 on the Entry Page? Insert/Object/Adobe Acrobat Document/Display As Icon and have the attachment submitted to the Historical/Current lists? Or is that just crazy?
    Attached Files Attached Files
    Last edited by swordswinger710; 05-24-2012 at 11:34 AM.

  36. #36
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Post Re: Code For Button To Store Data

    Allcodes have been revised following this logic.

    If value1 is absent in current sheet entered data added to Current and Historical sheets. If value1 is found the value1 record row is updated on Current sheet and new row added to the "block" of value 1 on Historical sheet (the latest date being the first). Conditional format must be present on Current sheet.

    1. Sheet "Entry page" code:

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim CLsearch As Range
    If Not Intersect(Target, Range("c2")) Is Nothing Then
        With Application
            .ScreenUpdating = 0
            .EnableEvents = 0
            Range("c3:c25").ClearContents
            If Target = "" Then
                .EnableEvents = 1
                .ScreenUpdating = 1
                Exit Sub
            End If
            Set CLsearch = Sheets("Current List").UsedRange.Resize(, 1).Find(Target, , xlValues, xlWhole, , xlNext)
            If Not CLsearch Is Nothing Then
                CLsearch.Resize(, 24).Copy
                Range("c2").PasteSpecial Paste:=xlPasteValues, Transpose:=True
                Range("c3:c25").HorizontalAlignment = xlCenter
            End If
            Range("c2").Select
            .EnableEvents = 1
            .ScreenUpdating = 1
        End With
    End If
    End Sub
    
    Private Sub Worksheet_Activate()
    Range("C2").Activate
    End Sub
    2. Sheet "Current List"

    Private Sub Worksheet_Activate()
    Range("a1").Activate
    End Sub
    3. Module 1 codes:

    Sub Submit_button_click()
    Dim CLsearch As Range, HLsh As Worksheet, HLsearch As Range, response, shname, lastcell As Range
    With Sheets("Entry Page")
        If .Range("c2") = "" Then Exit Sub
        Set CLsearch = Sheets("Current List").UsedRange.Find(.Range("c2"), , xlValues, xlWhole, , xlNext)
        If Not CLsearch Is Nothing Then
                response = MsgBox("Are you sure you would like to update the latest data for record #" & .Range("c2") & _
                " with the new data in C3:C25 of the Entry Page?", vbYesNo + vbQuestion, "Update confirmation")
                If response = 6 Then
    
                    Set HLsh = Sheets("Historical List")
                    Set HLsearch = HLsh.UsedRange.Find(.Range("c2"), , xlValues, xlWhole, , xlNext)
                    HLsearch.EntireRow.Insert CopyOrigin:=HLsearch
                    HLsearch.Offset(-1).Resize(, 24) = Application.Transpose(.Range("c2:c25"))
                    With HLsh.Range("a4", HLsh.Cells(Rows.Count, "a").End(xlUp)).Resize(, 24)
                        .Sort key1:=.Range("a4"), key2:=.Range("e4"), order2:=xlDescending, Header:=xlNo
                        .VerticalAlignment = xlVAlignCenter
                    End With
                    
                    HLsearch.Offset(-1).Resize(, 24).Copy
                    Application.EnableEvents = 0
                    CLsearch.PasteSpecial xlPasteValuesAndNumberFormats
                    Application.EnableEvents = 1
                    
                    Clear_entry_range
                Else
                    .Range("c2").Activate
                End If
        Else
            response = MsgBox("The number you entered is not yet associated with a record. Would you like to create a new one?", _
            vbYesNo + vbQuestion, "Action confirmation")
            If response = 6 Then
                Application.EnableEvents = 0
                For Each shname In Split("Current List,Historical List", ",")
                    With Sheets(shname)
                        Set lastcell = .Cells(Rows.Count, 1).End(xlUp)
                        If lastcell.Row > 4 Then
                            With lastcell
                                .Resize(, 24).Copy .Offset(1)
                                .Resize(, 24).Offset(1).Value = Application.Transpose(Sheets("Entry Page").Range("c2:c25"))
                            End With
                        Else
                            Sheets("Entry Page").Range("c2:c25").Copy
                            lastcell.Offset(1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Transpose:=True
                        End If
                    End With
                Next
                Application.EnableEvents = 1
                Application.CutCopyMode = 0
                Clear_entry_range
                MsgBox "New record added successfully", vbInformation, "New record"
            End If
        End If
    End With
    End Sub
    
    Sub Clear_entry_range()
    Application.EnableEvents = 0
    Sheets("Entry Page").Range("c2:c25").ClearContents
    Application.EnableEvents = 1
    End Sub
    
    Sub Enable_Events()
    Application.EnableEvents = 1
    End Sub
    
    Sub CalibrationDueDate()
    Dim dt As Date, dY As Double
    dt = Range("C6").Value
    dY = Val(Range("C7").Value)
    Range("C8").Value = DateAdd("yyyy", dY, dt)
    End Sub
    4. Sheet "Historical List": delete all codes
    Last edited by watersev; 05-24-2012 at 04:54 PM.

  37. #37
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code For Button To Store Data

    Thank you for your helpfulness as usual, watersev. Here's what I still notice:

    When I enter a record by the name of 1 and hit Submit, some other sheets flash in front of me and then I'm returned to the Entry Page again. This isn't the worst thing, but it only happens with record 1, so I just wondered if there is a simple fix for that.

    Also, what would be the best way to have the records displayed in numerical order? The Current List just pops them in there in whatever order they were entered. The Historical List does it right, though. What am I doing wrong?

    And one last thing - when I make a change to a current record on the Historical List, it doesn't show up in the Current List like it used to.

    Other than those three things, it is working really nicely! Thank you so much again for not giving up on me.

  38. #38
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Code For Button To Store Data

    hi there, please check attachment
    Attached Files Attached Files

  39. #39
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code For Button To Store Data

    Hello watersev, and thanks again; this appears to be working better, I just have two more questions:

    How do I keep both lists sorted by number? I tried doing a sort, but it doesn't seem to help.

    Also, when I enter a previously entered record number in the Entry Page Cell C2, the data for it is displayed, but all the cells are selected.

    I think these are the last two issues!

  40. #40
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Code For Button To Store Data

    check attachment
    Attached Files Attached Files

  41. #41
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code For Button To Store Data

    watersev, that is amazing! I do have one more question - is all the cell formatting for the Historical List done in the code? I'd like to have the text in Cell X4 and down aligned to the top and to the left, but can't do it manually. What do I need to do if I'd like to change something like that?

  42. #42
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Code For Button To Store Data

    check attachment
    Attached Files Attached Files
    Last edited by watersev; 05-30-2012 at 02:51 AM.

  43. #43
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Code For Button To Store Data

    Thank you yet again, watersev! It took a while to get to testing it out, but it seems to be working just great!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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