I have an Excel Spreadsheet that I want to add 1 year to the date in a cell.
Cell B3 currently has a value of 1/1/2014
Would like to create a Macro that will change this date to 1/1/2015
Seems easy enough but my search for this have not given desired results. Any help would be appreciated.
Select your cell and run:
Sub dateFixer()
Dim d As Date
d = ActiveCell.Value
ActiveCell.Value = DateSerial(Year(d) + 1, Month(d), Day(d))
End Sub
Related
I have a workbook that is used to schedule the next upcoming task on a job. each row has 28 cells, each cell represents a day of the week within the 4 weeks lookahead. I made a formula to check the date of the cell with the start and end date of the task and fill the cell accordingly.
Here is the formula:
=IFERROR(IF(AND(ISNUMBER(SEARCH("Delivery",$D16)),VALUE(F$10)=VALUE('Calculation
New'!$AO53)),"D",IF(AND(ISNUMBER(SEARCH('Calculation
New'!$BH$13,$AJ16)),VALUE(F$10)>=VALUE('Calculation
New'!$AO53),VALUE(F$10)<=VALUE('Calculation
New'!$AP53)),"N",IF(AND(ISNUMBER(SEARCH('Calculation
New'!$BH$12,$AJ16)),VALUE(F$10)>=VALUE('Calculation
New'!$AO53),VALUE(F$10)<=VALUE('Calculation
New'!$AP53)),"E",IF(AND(VALUE('Calculation
New'!$AO53)=VALUE('Calculation New'!$AP53),F$10='Calculation
New'!$AO53,NOT(ISNUMBER(SEARCH('Calculation
New'!$BH$9,$D16)))),"SF",IF(AND(ISNUMBER(SEARCH('Calculation
New'!$BH$9,$D16)),VALUE(F$10)>=VALUE('Calculation
New'!$AO53),VALUE(F$10)<=VALUE('Calculation
New'!$AP53)),"I",IF(AND(VALUE(F$10)>VALUE('Calculation
New'!$AO53),VALUE(F$10)<VALUE('Calculation
New'!$AP53)),"X",IF(VALUE(F$10)=VALUE('Calculation
New'!$AO53),"S",IF(VALUE(F$10)=VALUE('Calculation
New'!$AP53),"F","")))))))),"")
a few things to that formula:
D16:D85 on the sheet "SIS" is the Task description where to look for certain words
BH9 on sheet "Calculation New" contains a word to compare to. The range of words is BH3:BH13
F10:AF10 on the sheet "SIS" contains the date for the cells below of the day of the week
AO53:AO122 on sheet "Calculation New" contains the start date of a task
AP53:AP122 on sheet "Calculation New" contains the End date of a task
currently, I got 70 Rows times 28 cells and each cell has this formula in it. Now I want to rather use a VBA code do the same thing, but I am having a hard time to get started. I am not very experienced with VBA. I researched in regards to nesting For each loop but so far I am not succeeding.
I would appreciate any help I can get.
Thank you in advance
Dan
here is the code I have written so far not complete but I am stuck and need some advice
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim SDate As Range
Dim EDate As Range
Dim WDate As Range
Set SDate = Worksheets("Calculation New").Range("SDate")
Set EDate = Worksheets("Calculation New").Range("EDate")
Set WDate = Worksheets("Calculation New").Range("WDate")
For SDate = 1 To Worksheets("Calculation New").Range("SDate").End(xlDown) 'lenght of range varies
'For WDate = 1 To 28 ' length is always same
'If cell = WDate Then 'i want to compare each cell of WDate with the start date
'cell = "X"
'Next
Next
End Sub
To get you started with VBA, you might want to start here. There's a lot of questionable sites offering VBA code but this is directly from Microsoft and covers the basics. Happy coding!
https://learn.microsoft.com/en-us/office/vba/library-reference/concepts/getting-started-with-vba-in-office
For loops can be tricky - generally you can start with an array saying r = ActiveSheet.UsedRange and loop through it
Sub nestedLoop()
r = ActiveSheet.UsedRange
For i = LBound(r) To UBound(r)
For j = LBound(r, 2) To UBound(r, 2)
'evaluate r(i,j) do something
'Debug.print r(i,j)
Next j
Next i
End Sub
I'm very new to Excel VBA and I'm trying to getting figure out certain things. I want to populate cells in a range K26:K386 with a value that comes from cell N47 (call it "Income)". The cell to be populated (among those rows in K26:386) is determined by a number in cell N46 (call it "Month"). "Income" can be any figure. "Month" can be any figure from 1 to 360.
I want to plug in two inputs into cells N47 and N46 - income and the respective month - such that the respective cell in range K26:K386 is populated with the "Income" value for the respective month from 1 to 360. Can somebody please advise?
I only know how to populate a single cell like this:
Sub Ievietot()
Dim Sum As Integer
Sum = Range("N47").Value
Range("K30").Value = Sum
End Sub
you could use
Range("K26:K386").Cells(Range("N46").Value) = Range("N47").Value
I think that this can be done as follows:
Sub CopyData()
Dim iMonthNum as Integer
Dim income as Integer
iMonthNum = Range("N46").Value
income = Range("N47").Value
Range("K" & (25 + iMonthNum)).Value = income
End Sub
i have that problem, when a guy complete cell 1 from column B i want to put date now in Column A, cell 1. If i save excel I want to save date, if i open tomorrow to save date from the last day but when complete Column B, row 2, put date from the current date without change date from Column A, row 1. Like in picture:
Ex: if i complete in Column B, row 6, put current date in Column A, row 6, without change other dates.
Thanks
Only way I know of that you can do this is with some VBA code.
Select the sheet that you want to automate and press ALT + F11 to go to the VBA editor, then you could use some code like this
Private Sub Worksheet_Change(ByVal Target As Range)
If (Target.Column = 2) Then ' Check if in 2nd Column ie. Column B
If Target.Offset(, -1).Value = "" Then ' Check if there is already a value in Column A
Target.Offset(, -1).Value = Date ' No value, lets stick in the current system date
End If
End If
End Sub
Problem with this code is it will run for every cell change and on a large workbook might slow things down a bit.
I have to write a vba code that convert all dates in a year into days in a week eg. Monday, Tuesday, Wednesday....Sunday and the value of all days in a week eg.Monday must be in a string format. (See image inserted)
I tried using the .NumberFormat function but it does not work since the format is still under the date format although the values displayed are correct (please look at the formula bar of F10). Below is the code I have written:
Sub convertdate()
Range("A7:A371").Copy
'copy all the dates throughout the year'
Range("F7:F371").PasteSpecial xlPasteFormulasAndNumberFormats
'paste it into F column'
Worksheets("Sheet1").Range("F7:F371").NumberFormat = "dddd"
'convert the dates into days'
Sum1 = Application.SumIf(Range("F7:F371"), "Monday", Range("B7:B371"))
'example of calculating the total rainfall of all Mondays throughout the year'
Worksheets("Sheet1").Range("G22").FormulaArray = Sum1
End Sub
The formula bar from cell F7:F371 should display the string value "Monday","Tuesday" etc depending on the dates rather than the date itself.The reason of converting it into a string is so that it could be use in a SUMIF function later on to calculate the total rainfall of all Mondays,Tuesday etc.
Appreciate if anyone could help. Thank you.
A formula can do this.
In cell F7 enter =TEXT(A7,"dddd") and drag down.
This will format the date to show the full day name as a string.
https://support.office.com/en-us/article/TEXT-function-20d5ac4d-7b94-49fd-bb38-93d29371225c
Try something like this. It will loop through all the dates in Column A (starting in row 7) and put the associated Weekday name in Column F:
Sub test()
Dim i As Long
Dim lRow As Long
With ActiveSheet
lRow = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 7 To lRow
.Cells(i, 6).Value = WeekdayName(Weekday(.Cells(i, 1).Value, 1), False, 1)
Next i
End With
End Sub
I am creating an excel template and want to auto populate the dates in my table.
Above their is a line where the dates included are written in EX. 10/01/14 - 10/31/14.
I want the dates in my table to be filled in when i enter those dates.
Do I need to make it two cells, one start date and one end date?
What formula do i need to use?
Select the cell containing the pair of dates separated by a dash and run this small macro:
Sub DateMaker()
Dim v As String, d1 As Date, d2 As Date, i As Long
v = ActiveCell.Text
ary = Split(v, "-")
d1 = DateValue(Trim(ary(0)))
d2 = DateValue(Trim(ary(1)))
Set r = ActiveCell.Offset(1, 0)
For i = 1 To Rows.Count
r.Value = d1
d1 = d1 + 1
If d1 > d2 Then Exit Sub
Set r = r.Offset(1, 0)
Next i
End Sub
If you can manage without using the Table then Try This:
select entire entry range (blank cells where date would reside later) and insert this formula :
=LEFT($L$3;8)+(ROW()-n)
here, n= row no of first entry and L3 cell contain the date range.
After pasting the above formula press CTRL+SHIFT+ENTER to make it array formula.
Or, you might follow the macro (code) way suggested in previous answer.
2 cells are the easiest - you can then CONCATENATE them to give the display how you want, and hide the Start and End columns
In the first (start) cell, put your starting date.
For the ending date, use =EOMONTH([#Start],0)
For 2nd month use either =DATE(YEAR(A2),MONTH(A2)+1,1) or =B2+1 as the start date, and keep =EOMONTH([#Start],0) as the end date. The table will then autofill with the start and end date for each month as you grow the table.