Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hi
My problem is that I need to select a month either 1 or 2 months ago, depending on the variable v_addTimeShift, which has a value of either 1 or 2. I currently use different macros for this and run them using button actions. Unfortunately, I am unable to run different macros in the one activity depending on the variable ( something like: If(var=1, macro1, macro2). I have tried to use the variable inside a macro as well, in this case one macro is enough, but unfortunately that doesn't work either. The solution would also be if I could write a third macro that would run either the first or the second macro depending on the value of the variable, I tried that too, but unfortunately it didn't work.
Riho
Sub SelectLastMonth
With ActiveDocument.Fields("Year_Month")
.Select ActiveDocument.Evaluate("Date(Date#(AddMonths(Today(),-1), 'DD.MM.YYYY'), 'YYYY-MM')")
End With
End Sub
Sub SelectLastMonth_L
With ActiveDocument.Fields("Year_Month")
.Select ActiveDocument.Evaluate("Date(Date#(AddMonths(Today(),-2), 'DD.MM.YYYY'), 'YYYY-MM')")
End With
End Sub
I would think you could just reference the variable in your macro like:
Select ActiveDocument.Evaluate("Date(Date#(AddMonths(Today(),-$(myvar)), 'DD.MM.YYYY'),
BTW,
Thank you very much for your help, the advice worked. However, I also have another macro that selects the last 12 months. I also did this round following the same advice and it works too. However, this macro seems very clumsy and it should be possible to write it using the for next loop. Unfortunately, I can't get it to work. I am also attaching both versions of the macro.
Riho
sub Select12months
With ActiveDocument.Fields("Year_Month")
.Select ActiveDocument.Evaluate("Date(Date#(AddMonths(Today(),-1-$(v_addTimeShift)), 'DD.MM.YYYY'), 'YYYY-MM')")
.ToggleSelect ActiveDocument.Evaluate("Date(Date#(AddMonths(Today(),-2-$(v_addTimeShift)), 'DD.MM.YYYY'), 'YYYY-MM')")
.ToggleSelect ActiveDocument.Evaluate("Date(Date#(AddMonths(Today(),-3-$(v_addTimeShift)), 'DD.MM.YYYY'), 'YYYY-MM')")
.ToggleSelect ActiveDocument.Evaluate("Date(Date#(AddMonths(Today(),-4-$(v_addTimeShift)), 'DD.MM.YYYY'), 'YYYY-MM')")
.ToggleSelect ActiveDocument.Evaluate("Date(Date#(AddMonths(Today(),-5-$(v_addTimeShift)), 'DD.MM.YYYY'), 'YYYY-MM')")
.ToggleSelect ActiveDocument.Evaluate("Date(Date#(AddMonths(Today(),-6-$(v_addTimeShift)), 'DD.MM.YYYY'), 'YYYY-MM')")
.ToggleSelect ActiveDocument.Evaluate("Date(Date#(AddMonths(Today(),-7-$(v_addTimeShift)), 'DD.MM.YYYY'), 'YYYY-MM')")
.ToggleSelect ActiveDocument.Evaluate("Date(Date#(AddMonths(Today(),-8-$(v_addTimeShift)), 'DD.MM.YYYY'), 'YYYY-MM')")
.ToggleSelect ActiveDocument.Evaluate("Date(Date#(AddMonths(Today(),-9-$(v_addTimeShift)), 'DD.MM.YYYY'), 'YYYY-MM')")
.ToggleSelect ActiveDocument.Evaluate("Date(Date#(AddMonths(Today(),-10-$(v_addTimeShift)), 'DD.MM.YYYY'), 'YYYY-MM')")
. ToggleSelect ActiveDocument.Evaluate("Date(Date#(AddMonths(Today(),-11-$(v_addTimeShift)), 'DD.MM.YYYY'), 'YYYY-MM')")
. ToggleSelect ActiveDocument.Evaluate("Date(Date#(AddMonths(Today(),-12-$(v_addTimeShift)), 'DD.MM.YYYY'), 'YYYY-MM')")
End With
end sub
sub Select12monthsTest
With ActiveDocument.Fields("Year_Month")
.Select ActiveDocument.Evaluate("Date(Date#(AddMonths(Today(),-1-$(v_addTimeShift)), 'DD.MM.YYYY'), 'YYYY-MM')")
for n=2 to 12
.ToggleSelect ActiveDocument.Evaluate("Date(Date#(AddMonths(Today(),-n-$(v_addTimeShift)), 'DD.MM.YYYY'), 'YYYY-MM')")
next
End With
end sub