VBA Loop for Column select from input box

Earlyfreak

New Member
Joined
Jan 31, 2017
Messages
16
I have a input box to select the column with the dates thenwhere I want to put the time span into words.
I am trying to use a loop to enter formula down the column but keep getting error's


Code:
Sub DateSpan2Words()
Dim DateNumCol As Integer
Dim TimeSpanCol As Integer
Dim isnumber As Long
Dim MyCol As Long
Application.ScreenUpdating = False
On Error Resume Next
MyCol = ActiveCell.Column
DateNumCol = Application.InputBox(Prompt:="If not this column, then Enter Column Number where Date is In", Default:=MyCol, Type:=1)
TimeSpanCol = Application.InputBox(Prompt:="Enter Column Number where Time Span to be entered in", Default:=MyCol, Type:=1)
FinalRow = Cells(Rows.Count, LocNumcol).End(xlUp).row
On Error Resume Next
For i = 1 To FinalRow
).VaCells(i, TimeSpanCollue = .FormulaR1C1 = _
        "=IFERROR(IF(DATEDIF(RC[-1],TODAY(),""y"")=0,"""",DATEDIF(RC[-1],TODAY(),""y"")&"" years "")&IF(DATEDIF(RC[-1],TODAY(),""ym"")=0,"""",DATEDIF(RC[-1],TODAY(),""ym"")&"" months "")&IF(DATEDIF(RC[-1],TODAY(),""md"")=0,"""",DATEDIF(RC[-1],TODAY(),""md"")&"" days""),"""")"
  Next
Application.ScreenUpdating = True
End Sub
[CODE]

errors on the loop "Cells(i, TimeSpanCol).Value = .FormulaR1C1 = _"
any direction would be greatly appreciated
 
What is the error message & number, & what line is highlighted?
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
it highlights this line the formula
Code:
Cells(i, TimeSpanCol).FormulaR1C1 = _
"=IFERROR(IF(DATEDIF(RC" & DateNumCol & ",TODAY(),""y"")=0,"",DATEDIF(RC" & DateNumCol & ",TODAY(),""y"")&"" years "")&IF(DATEDIF(RC" & DateNumCol & ",TODAY(),""ym"")=0,"""",DATEDIF(RC" & DateNumCol & ",TODAY(),""ym"")&"" months "")&IF(DATEDIF(RC" & DateNumCol & ",TODAY(),""md"")=0,"""",DATEDIF(RC" & DateNumCol & ",TODAY(),""md"")&"" days""),"""")"
Next

line-0
 
Upvote 0
We got it Fluff with your direction I checked and added two more "" after Year between next Datedif

final working code

Code:
Sub WorkingSolutionDateSpan2Words()
    
Dim DateNumCol As Integer
Dim TimeSpanCol As Integer
'Dim isnumber As Long
Dim MyCol As Long
Application.ScreenUpdating = False
On Error GoTo DateSpan2Words_Error
MyCol = ActiveCell.Column
DateNumCol = Application.InputBox(Prompt:="If not this column, then Enter Column Number where Date is In", Default:=MyCol, Type:=1)
TimeSpanCol = Application.InputBox(Prompt:="Enter Column Number where Time Span to be entered in", Default:=MyCol, Type:=1)
FinalRow = Cells(Rows.Count, DateNumCol).End(xlUp).row
For i = 1 To FinalRow
Cells(i, TimeSpanCol).FormulaR1C1 = _
"=IFERROR(IF(DATEDIF(RC" & DateNumCol & ",TODAY(),""y"")=0,"""",DATEDIF(RC" & DateNumCol & ",TODAY(),""y"")&"" years "")&IF(DATEDIF(RC" & DateNumCol & ",TODAY(),""ym"")=0,"""",DATEDIF(RC" & DateNumCol & ",TODAY(),""ym"")&"" months "")&IF(DATEDIF(RC" & DateNumCol & ",TODAY(),""md"")=0,"""",DATEDIF(RC" & DateNumCol & ",TODAY(),""md"")&"" days""),"""")"
Next
  
   On Error GoTo 0
    Exit Sub
Application.ScreenUpdating = True
DateSpan2Words_Error:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure DateSpan2Words, line " & Erl & "."
End Sub



Thanks for guiding me through, much appreciated. Thanks for the education.
 
Last edited:
Upvote 0
If your dates are in col A, try
Code:
"=IFERROR(IF(DATEDIF(RC1,TODAY(),""y"")=0,"""",DATEDIF(RC1,TODAY(),""y"")&"" years "")&IF(DATEDIF(RC1,TODAY(),""ym"")=0,"""",DATEDIF(RC1,TODAY(),""ym"")&"" months "")&IF(DATEDIF(RC1,TODAY(),""md"")=0,"""",DATEDIF(RC1,TODAY(),""md"")&"" days""),"""")"
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,023
Latest member
alabaz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top