VBA to Copy Range on Year to Date basis - excel

I have a worksheet with three columns. Column C:C has all calendar dates, N:N contains Data 1 and R:R Data 2.
The macro I am trying to write should check dates in Column C to find dates that are (EDIT) less than today and copy data of Column R:R to Column N:N.
Does this make sense? I have a feeling it's a super basic thing, but oh well, I spent one hour Googling without success.
Example
Thanks!

In Excel a date is just the number of days which starts counting from January 1st 1900. So January 1, 1900 is number 1. With this in mind, it is very easy to calculate with dates. Hours and minutes are fractions.
This sub should do the job:
Sub CopyData()
For i = 1 To 100
If Cells(i, 3) < DateTime.Date Then
Cells(i, 14) = Cells(i, 18)
End If
Next
End Sub
Replace the number 100 with the amount of lines you have or with some logic to count the number of lines.
Succes.

Related

Excel VBA - Do While Loop - Date in a cell

I want to increase the settlement date which is (assigned to cell N13) until the network days (assigned to cell P11) reaches 3.
Network days counts the number of weekdays between two dates excluding holidays. However, I added a range of holidays to it.
Sub Settlement_Date()
Do While Cells(16, 12) < 3
Cells(14,11)
Loop
to increase Cells(14,11) within the loop, change that line to
Cells(14,11) = Cells(14,11) + 1
Managed to do it myself but got snippets in some of the post I found here
Sub Settlement_Date()
Do While ActiveCell.Offset(0, 2).Value < 3
ActiveCell.Value = ActiveCell.Value + 1
Loop
End Sub
Moving Right One Cell
Add one day to date in cells using VBA

Excel macro to find weekend from day number

I have an Excel macro which calculates no. of hours spent on a task for each day in a month.
The first row contains day numbers from 1 to 31 and data is filled using the macro accordingly for each day number for a month. The sheet name contains the month name.
Now, the problem is the macro leaves blank for the days for which data is not available. That day can be a Saturday, Sunday, or the person was absent that day.
I want the macro to identify weekends and highlight those with gray color so that only absent days appear as blank.
Using cell formulas and conditional formatting (VBA below)
You could do this quite easily with the built-in WEEKDAY cell formula, together with the DATE formula.
By setting up the year and month value in the sheet, you can create an extra column which gives the day of the week as a number between 1 and 7:
=WEEKDAY(DATE($A$2, $B$2,C2))
Then you can set up conditional formatting to colour days 7 and 1 (Saturday and Sunday respectively) grey
Using VBA
You can apply the same logic as above, but from a sub. This doesn't require the month and year values to be stored in the sheet, and takes the month from the sheet name. It then directly colours the day number grey if on the weekend.
Sub weekdayhighlight()
Dim days As Range
Set days = ActiveSheet.Range("C2:C8") ' Range where date numbers are stored, 1,2,...,31
Dim month As String
month = ActiveSheet.Name ' Active sheet name is "May"
Dim day As Range
Dim daynum As Long
' Loop over days to test for weekday
For Each day In days
' Get day number, from date in format "1 May, 2017"
daynum = Weekday(DateValue(day.Value & " " & month & ", 2017"), firstdayofweek:=vbSunday)
If daynum = 7 Or daynum = 1 Then
day.Interior.Color = RGB(200, 200, 200) ' Grey for saturdays or sundays
End If
Next day
End Sub
Note: This macro will throw an error if you try and compute the weekday of a non-existant date, for instance the 31st of Feb. You say each sheet has day numbers from 1 to 31, you should limit that to actual existing dates or add error handling to the code.
Try combining your sheet name and column headers into date (what about a year?) in a loop and then use DatePart() function to determine whether it is weekend day or not. It would be easier if column headers were real dates (may be formatted to show day only).

Converting all dates in a year into multiple strings using Excel VBA

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

Excel VBA code to return weekday name from a date then autofilters for userdefined weekdays and copies autofiltered data to new sheet

I spent last night looking for an answer to my question but i didn't find anything specific to my ask.
I have a huge sheet that contains lots of data. In Column A, I have the Date and time. I would like to have a VBA code that does few actions.
1) Insert a column B next to column A that gives the weekday name. For example if the date in column A is January 10, 2016 8:30 then column B will have "Sunday"
2) Then the VBA code to filter this sheet based on Column B. For instance given a user input of a particular day (example Friday), the autofilter will filter for 4 days ahead ( Friday, Saturday, Sunday, Monday ).
3) The result of the autofilter then is copied to new blank sheet.
4) In the new sheet I want the vba to delete any row data for the same user defined day ( example Friday ) that has time of less than user defined time (example 8:00 am) in Column A. Finally shifting cells up to remove any blank rows.
Hope I am not overwhelming you with this problem, but I hope I get feedback from one of the brilliant minds in here
Here it the code attempted for part 1
Sub sbInsertingColumns()
Worksheets("Imported").Activate
'Inserting a Column at Column B
If Range("B1").Value = "Weekday" Then
Range("B1").Value = "Weekday1"
Else
Range("B1").EntireColumn.Insert
Range("B1").Value = "Weekday"
Worksheets("Imported").Range("B2").Formula = "=choose(weekday(A2),""Sunday"",""Monday"",""Tuesday"",""Wednesday"",""Thursday"",""Friday"",""Saturday"")"
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("B2").Copy Range("B2:B" & LastRow)
End If
End Sub
This is only to get you started.
If A2 contains a genuine Excel date/time, then in B1 enter:
=TEXT(A2,"dddd")
If you want a VBA macro to accomplish your project, start by turning on the macro recorder and performing the task once manually.
You will then have a macro that you can begin to optimize and generalize.

Insert a new column after every 7 columns with a formula

I have a spreadsheet with 4000 records that keeps on updating new data. The column headers are Employee Name, Project, Activity, 06/29/2013, 06/30/2013... etc. These are individual dates starting from October of last year to this week and each of these columns contains numeric values. I'm trying to modify this week-wise. So if Sunday is considered as the 7th day of a week, then I want to subtract the values from this week's Sunday with the last week's Sunday. And I'd want it to continue doing that for the rest of the columns since the dates will keep on updating so it's going to be taxing to do it manually.
FYI I'm using Excel 2013.
I know how to perform basic operations but this one is beyond my understanding so any help would be much appreciated!
Sub insert_column_after_interval_7()
For colx = 7 To 200 Step 8
Columns(colx).Insert Shift:=xlToRight
Next
End Sub
I have specified the range as 200 but the data might exceed later on. So how do I modify it without giving any specific range?
you could try something along the lines of this:
Sub insert_column_after_interval_7()
dim iLastCol as integer
iLastCol = Cells(1, Columns.Count).End(xlToLeft).Column ' same as CTRL+RIGHT ARROW
For colx = 7 To iLastCol Step 8
Columns(colx).Insert Shift:=xlToRight
Next
End Sub
of course, as you are likely adding more than 8 columns, you might be better off with a DO WHILE loop

Resources