Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Automating a user form

0

Hello. 

I have been printing a form and filling in the variable data by hand. I think I could have been printing the five cells of variabl data automatically. The attached file should be easy to follow, the date sequence is causing me the biggest problem as it is a six day week, Monday to Saturday. I copied the next start sequence to L column thinking I only had to update five cells. I have created buttons for the print and save operations  I am trying to avoid printing a blank row for Sundays , if that is possible.

Thank you for looking at my question.

Answer
Discuss

Discussion

Hello MrEMann,  
The file you have uploaded is missing elements you say are part of it. Firstly, the title of your post is misleading - "Automating a user form" - but your file does not contain a user form. Secondly, when I go to "Print Preview" there are no blank rows showing. You say you have buttons for "Save" and "Print" but they are nowhere to be found. In the VBA Project you have added 7 modules but none have any code in them. There is no code in any part of the project. You mention there are 5 cells of variable data but you haven't specified which ones they are. There are 8 blank columns (C - J) so I am guessing the 5 you mention are not these. You also have many, many columns hidden. Do these hidden columns have formulas which impact what is visible? Unless you have uploaded the wrong file, there is no issue with "printing a blank row for Sundays" because there are no blank rows (at least not for me, Excel 2016); so I have no answer to provide.

Cheers   :-)
WillieD24 (rep: 557) Feb 17, '24 at 6:10 pm
Hi. This started as a fun project between friend who buy lottery tickets using the same 'lucky' numbers. We debated whether picking random numbers might be more successful. Sorry, I thought user form would be seen as different to userform. We pay a small amount for each guess and if anyone 'wins' they will get whatever is in the pot. The variables are at A1, the B column and K2 to K4. C to I  are the user guesses and J is my win/lose mark. I wasn't aware of any hidden cells as I don't intentionally hide any cells.
However, I have decided to abandon this project and carry on just printing the bare bones and filling in the variables by hand.
I am sorry for wasting your time, but one good thing has emerged from this. I have finally found someone who agrees with me, the smiley face needs the nose! Thank you :-)
MrEMann (rep: 20) Feb 18, '24 at 3:50 am
Hi MrEMann,

I had wondered if you were using "user form" correctly or not because there have been many times when someone will use "user form" when they really mean "userform". The only visible columns are A thru L, O, and AI; all others are hidden.
So, if I understand correctly, the 3 of you pcik 7 numbers but don't actually play them in the lottery. You just keep track to see who is the better guesser (a different guesser each draw; column "B" being the draw date).
It will 3-4 days before I can take a closer look at you file. If you would like, I could clean it up and possibly make something workable for you. I would just need a bit more info - the signifcance of "1" in "A1", What is the purpose of the values in "K" and why do they increment by 1. Just let me know if you would like me to have a go at it.

Cheers   :-)
WillieD24 (rep: 557) Feb 18, '24 at 10:03 am
Thank you for your continued interest in my question. A1 is just the count of print runs to make sure the next run begins where I expect it to. The rest of column A are just names. The numbers in the L column are the number of draws made by the diffent lottery games. Only needed as a 'double check' that I am checking the correct draw. The date in the B column should normally be sufficient.
In time I was going to experiment using 'delete sheet rows' to see how that worked. I think I would just need to reset the start points so that 'cell+1'  would produce the right result. To update the whole page, print and save it with a few mouse clicks was my initial goal.
I wonder if the hidden cells came about when I set the print area. I was surprised when a print test did not offer more than one page.
If that does not make my goal clear I would ask you to abandon this project. I do not want to waste any more of your precious time.
Thank you once again.
MrEMann (rep: 20) Feb 18, '24 at 11:56 am
Hi MrEMann. No need to abandon your project- I've added an Answer. Please check but kindly also edit your question title to something like "Adding a sequence of dates but skipping a chosen week day"- that will be more helpful to other usrrs with a similar problem.
John_Ru (rep: 6142) Feb 19, '24 at 8:36 am
Hello again,

I had a few minutes today to look at this again. When you print the reulsts, how many draw results do you typically want to print. The last week (6 draws)? The last 4 weeks (24 draws)? Or do you want to print X number of draws and X number of empty rows?
WillieD24 (rep: 557) Feb 19, '24 at 5:14 pm
@Willie - I just tried to solve the user's stated "date sequence" problem but looks like you're planning a fuller solution - good work! 
John_Ru (rep: 6142) Feb 19, '24 at 5:42 pm
@John

I have no plans to change MrEMann's method of date progression. The first 6 draw dates (rows 2 - 7) are manually entered; them beginning with row 8 the date is incremented by the formula: "=B2 + 7", "=B3 + 7", "+B4 + 7", etc. and works nicely to skip Sunday. I just need added details about what is printed and when, so I can finalize my proposed solution.

Cheers  :-)
WillieD24 (rep: 557) Feb 19, '24 at 7:10 pm
@Willie - didn't notice any date formula (which would depend on what day is in B2) but assumed the original question word "Automating..." implied the need for a VBA solution. I'll do nothing further with my Answer, unless MrEMann requests anything).
John_Ru (rep: 6142) Feb 20, '24 at 6:30 am
Add to Discussion

Answers

0
Selected Answer

Hi again MrEMann

The attached file shows one way to add dates but skipping a given day of the week.

I've created a variable SkipDay and set it to 7 (which means Sundays, whereas 2 would omit Tuesdays say). Press the key combination Ctrl+Shift+J and it will add dates, provided there's a "seed" date in cell B2. I've added comments below so you can see what's happening (and added a custom format in column B so it's easy to see if the chosen day of the week is indeed skipped):

Sub AddDates()
    '
    ' Keyboard Shortcut: Ctrl+Shift+J
    '
    Dim SkipDay As Integer, n As Long

    ' set the week day to skip (where Monday=1, to Sunday=7)
    SkipDay = 7

    With Sheet1
        ' check B2 is a date
        If Not IsDate(.Cells(2, 2).Value) Then
            .Cells(2, 2).Select
            MsgBox "Please put a valid date in cell B2"
            Exit Sub
        End If
        'clear any values
        .Range(.Cells(3, 2), .Cells(200, 2)).ClearContents
        ' find last used cell in column 1
        LstRw = .Cells(.Rows.Count, 1).End(xlUp).Row
        ' loop in column 2 from 3 to last row
        For n = 3 To LstRw
            ' add a day to date in previous row
            .Cells(n, 2).Value = Cells(n - 1, 2).Value + 1
            ' if the resulting date is a day to skip, add one to that date
            If Weekday(.Cells(n, 2).Value, vbMonday) = SkipDay Then
                .Cells(n, 2).Value = Cells(n, 2).Value + 1
            End If
        Next n
    End With

    MsgBox "Added dates (less " & WeekdayName(SkipDay, 0, vbMonday) & "s) to cells B3 to B" & LstRw

End Sub

Hope this fixes your problem. If so, please be sure to mark this Answer as Selected.

Discuss

Discussion

This answer has solved my question perfectly. Thank you to all who have looked into this and for your very kind and expert input.
MrEMann (rep: 20) Feb 20, '24 at 9:51 pm
Glad that helped. Thanks for selecting my Answer, MrEMann. Please modify your question subject as I suggested (I can't) to help others. 
John_Ru (rep: 6142) Feb 21, '24 at 12:51 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login