Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with macro to increment dates in column 1

Status
Not open for further replies.

dwest100

Technical User
Aug 9, 2003
59
US
I have a column of dates (column d). New dates are added and deleted regularly.

I need to be able to increment all the dates in the column by a specific number of days, forward or backward, using a macro.

The macro needs to pop open a dialog window to prompt me for the number of days I wish to increment the dates in the column, and also whether to index forward or backward depending on which radio button is selected (forward or backward). Default would be forward.

How can I do this?

Any help would be greatly appreciated!
 
To increment a cells value:

Cells(myRow, myCol).Value = Cells(myRow, myCol).Value + 1



________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

For tsunami relief donations

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
I would use a combobox / Textbox to store the number and then convert it to +/- dependant on check on option button
Set a frame up around the option buttons to take a 1 or a -1 value dependant on the option chosen

Code:
myIncr = ComboboxName.value * FrameName.value

LastRow = cells(65536,4).end(xlup).row
with Range("D" & LastRow +1)
    .value = myIncr
    .copy
end with
with Range("D2:D" & LastRow)
   .PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd
end with
range("D" & LastRow).clearcontents

Rgds, Geoff

"Three things are certain: Death, taxes and lost data. Guess which has occurred"

Please read FAQ222-2244 before you ask a question
 
Thanks guys!
xlbo, I understand what you are saying but please elaborate on how to set the frame to assume the value of the checked option box.
Thanks!
 
Apologies - thinking in MSAccess mode

In excel, you would have to check the value of one of the option buttons

Code:
If OptIncr = TRUE then
 myIncr = ComboboxName.value
Else
 'OptDecr must be checked
 myIncr = ComboboxName.value * -1
End If

Rgds, Geoff

"Three things are certain: Death, taxes and lost data. Guess which has occurred"

Please read FAQ222-2244 before you ask a question
 
Here is what I have come up with. It works...sort of. I have to click cancel twice to unload the form. Also, I have to run the macro twice for the form to unload after processing the dates.

Any ideas to fix it?
Code:
Private Sub CommandButton1_Click()
    Dim DateCell As Range
    If Me.OptionButton1.Value = False And Me.OptionButton2.Value = False Then
        MsgBox "Please select forward or backward."
        Exit Sub
    ElseIf Not IsNumeric(Me.TextBox1.Value) Then
        MsgBox "Please enter the number of days to adjust the schedule."
        Exit Sub
    End If
    
    Me.TextBox1.Value = Int(Me.TextBox1.Value)
    
    If Me.OptionButton1.Value = True Then
        For Each DateCell In Sheets(1).[d4:d400]
            DateCell = DateCell + Me.TextBox1.Value
        Next DateCell
        
    ElseIf Me.OptionButton2.Value = True Then
        For Each DateCell In Sheets(1).[d4:d400]
            DateCell = DateCell - Me.TextBox1.Value
        Next DateCell
    End If
    Unload Me
End Sub

Private Sub CommandButton2_Click()
    Unload Me
End Sub
 
I would take Textbox1.value into a variable and HIDE the form rather than unloading
Code:
Private Sub CommandButton1_Click()
    Dim DateCell As Range
    If Me.OptionButton1.Value = False And Me.OptionButton2.Value = False Then
        MsgBox "Please select forward or backward."
        Exit Sub
    ElseIf Not IsNumeric(Me.TextBox1.Value) Then
        MsgBox "Please enter the number of days to adjust the schedule."
        Exit Sub
    End If
    
    myIncr = Int(Me.TextBox1.Value)
    
    If Me.OptionButton1.Value = True Then
        For Each DateCell In Sheets(1).[d4:d400]
            DateCell = DateCell + myIncr
        Next DateCell
        
    ElseIf Me.OptionButton2.Value = True Then
        For Each DateCell In Sheets(1).[d4:d400]
            DateCell = DateCell - myIncr
        Next DateCell
    End If
    Me.Hide
End Sub


However, I would advise against looping unless necessary - the pastespecial - Add function will do nicely here

Rgds, Geoff

"Three things are certain: Death, taxes and lost data. Guess which has occurred"

Please read FAQ222-2244 before you ask a question
 
xlbo,
Many thanks! Pardon my ignorance as I'm new at this. How would you incorporate the pastespecial add code into my code?
Don
 
As I did in my 1st post ;-)
Code:
If optionbutton1 = true then
  myIncr = Int(Me.TextBox1.Value)
else
  myIncr = Int(Me.TextBox1.Value) *-1

LastRow = cells(65536,4).end(xlup).row

with Range("D" & LastRow +1)
    .value = myIncr
    .copy
end with
with Range("D2:D" & LastRow)
   .PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd
end with
range("D" & LastRow).clearcontents

Rgds, Geoff

"Three things are certain: Death, taxes and lost data. Guess which has occurred"

Please read FAQ222-2244 before you ask a question
 
I used your suggested code above but I still have to run the macro twice before the form will hide. (or unload)

What would be causing that?

Same with the cancel button. If I call the macro, the form opens as expected, but I have to click cancel twice to close the form.
 
Is the button ON the form ??

Rgds, Geoff

"Three things are certain: Death, taxes and lost data. Guess which has occurred"

Please read FAQ222-2244 before you ask a question
 
Yes. Both buttons are.
This is the first form I've built. But in design mode, the buttons are clearly on the form.
Is that all that's required or am I missing the boat on something form related??

Thanks!
 
If the button is on the form and you have Me.Hide then it should work. The only issue might be in using the ME keyword - it may be applying itself to the button being pressed rather than the form - try explicity stating the form name rather than using ME - see if it makes a difference

Rgds, Geoff

"Three things are certain: Death, taxes and lost data. Guess which has occurred"

Please read FAQ222-2244 before you ask a question
 
OK will do.

Thanks for all your help. Much appreciated!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top