Consulting

Results 1 to 17 of 17

Thread: Change days each time

  1. #1

    Question Change days each time

    I am trying to enter days of the week each time i press button new day this code has error
    user defined type not defined can you help?
    [vba]
    Private Sub CommandButton1_Click()

    Dim i As Long
    Dim Count As Long

    Count = 1
    For i = 2 To 65536

    If Cells(i, 1) <> "" Then
    Count = Count + 1
    End If
    Next i

    Dim day As day
    day = Cells(Count, 1).Value
    day = day + 1


    'Days of the week Monday to Friday
    Sheets("Sheet5").Cells(1, 1)(Rows.Count, "D").End(xlUp).Offset(1).Value = day

    End Sub
    [/vba]
    Last edited by Bob Phillips; 07-11-2010 at 02:02 AM. Reason: Added VBA tags

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Headworth
    Always use Option Explicit. That will show a basic error.
    While a good start, the code is a bit inefficient. Can you post a sample workbook so I can see your data types, layout and desired result?
    Use Manage Attachments in the Go Advanced reply section.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    There is no such datatype as Day. To count days, which is just a number, use a type Long.

    But I am totally confused what you are doing, what is your requirement?

    YOu can count all of the non-empty cells with a simple

    [vba]
    Count = Application.CountA(Columns(1))
    [/vba]

    no need for a loop.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    I have changed the code it how works but i still need some help what i am trying to do is when i press the button each time i need it to change the day like Mon, Tue, Wed would it be a If statement to say the cell befor this has Tue then this cell wed?
    [vba] Private Sub CommandButton1_Click()
    Dim i As Long
    Dim Count As Long

    Count = 1
    For i = 2 To 65536
    If Cells(i, 1) "" Then
    Count = Count + 1
    End If
    Next i

    Dim day As String
    Dim mon As Long
    day = Cells(Count, 1).Value
    day = mon + 1

    'Days of the week Monday to Friday
    Sheets("Sheet5").Cells(1, 1)(Rows.Count, "D").End(xlUp).Offset(1).Value = "Mon"
    End Sub [/vba]
    Last edited by Bob Phillips; 07-11-2010 at 04:36 AM. Reason: Added VBA tags

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If you won't explain what you are trying to do, it is difficult to help.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    If column D row 1 has the word Mon then if i press CommandButton it should put Tue in column D row 2

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Just once, or should the next one, row 3, be Wed? And where do you stop?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8

    Change days each time

    Yes next one wed and stop at Fri

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub CommandButton1_Click()
    Dim aryDays As Variant
    Dim DayIdx As Long

    aryDays = Array("Mon", "Tue", "Wed", "Thu", "Fri")

    With Sheets("Sheet5")

    If .Range("D1").Value2 <> "" Then

    On Error Resume Next
    DayIdx = Application.Match(.Range("D1").Value2, aryDays, 0)
    On Error GoTo 0
    If DayIdx > 0 Then

    .Range("D1").AutoFill .Range("D1").Resize(6 - DayIdx)
    End If
    End If
    End With
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10

    Angry Change days each time

    Thank you for your help if only i could put into words what help i am asking for
    your code works great.

    As I did not ask the right question this code will not help me but i will work on your code to try to make it work for me for your code changes the days. The code I am work on has to be for entry into a database weekly and the days have to be for 15 entrys for each day Monday to Friday

    Regards
    Headworth

  11. #11
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Refer to Post 2
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  12. #12

    Change days each time

    The go advanced will not let me select Attachment

  13. #13

    Change days each time

    Have sent you sample of data

  14. #14
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  15. #15
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Hi HW,

    Xpost dont'cha know.

    Slightly different approach here. All info is on sheet 'Data', layed out as Mon to Fri with shift info, etc. Macro simply copies all from 'Data' to sheet 'Shifts' on next available row.

    This way anything you change, update or delete on sheet 'Data' will be copied to sheet 'Shifts' on next button click - no need to change all that code.
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  16. #16
    Hi rbrhodes

    Thank you this works very nicely

    Regards
    Headworth

  17. #17
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    You're welcome! but do me a favour and read about Xposting,
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

Posting Permissions

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