Loop that changes a date, copy & pastes resulting data, then continues these processes - vba

I have a workbook with 2 sheets. Sheet 1 has a column with a series of dates that are the same day in a sequence of years (for example 5/1/1986, 5/1/1987, 5/1/1988, ... 5/1/2017).
When the first date at the top of the column is changed (for example from 5/1/1986 to 5/2/1986), all of the other years below will update to that given day and a series of data will be produced in two separate columns through a calculation that is depends on the day as a variable.
The 2nd worksheet has two tables where I need the data resulting from each day between 5/1 and 8/28 for 1986 - 2017 to be pasted from the 1st worksheet after the date is changed (5/1 to 5/2 to 5/3) and so on. The tricky part is the copy and paste needs to happen each time before the date is changed in order to record all of the subsequent data.
I know that I will likely need to use a DateAdd function to loop the addition of one day to the starting date at the top of the column, and I know that I will probably need to utilize a Do Until function to have the sequence stopped when the final date of 8/28 is input. I have been tweaking around the code but I cannot get the formatting correct.
To clarify I need a loop that will do two processes, add one day to 5/1/1986 (until 8/28) and copy the data that each change of day yields and paste it into a 2nd worksheet, before the date is changed again and new data appears. Thanks.
Below is the code I wrote that successfully sets up the first step of the process, setting the start date to 5/1/1986 and copying and pasting the data into the tables in sheet2.
'StartDate is input as 5/1/1986 and plugged into first cell of data range, DK7'
Dim StartDate As String
StartDate = Range("DO50").Value
Range("DK7").Value = StartDate
'EndDate is input as 8/28/1986 and used as reference to end sequence of Copy & Pasting'
Dim EndDate As String
EndDate = Range("DO51").Value
'Corn Z-Score results for StartDate are copied and pasted into Sheet2'
Range("DS7:DS38").Copy
Worksheets(2).Range("D4:D36").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
'Soy Z-Score results for StartDate are copied and pasted into Sheet2'
Range("DT7:DT38").Copy
Worksheets(2).Range("D41:D73").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False

Related

vlookup to pick the nearest date lesser date from the reference cell

I required your support,
I have a datasheet (Sheet1) which is having product details and its dates which are been imported. Another sheet (Sheet2) which are also having product details and its dates which are been exported by the seller. Both the sheet dates might not be equal nor the total no. of rows will be the same.
I wanted to understand if a Vlookup can compare the dates of sheet 1 & sheet2 for each product keeping the imported dates of sheet 1 as a reference and pull the nearest lowest date from the sheet2 "created date"
Sheet 1 is having the data which is from imported data
Sheet 2 is the date which shows the actual export from the seller
So now I need to do a comparison for the Product bearing (cell A2) from Sheet 1, which is having the imported date as of 10/22/2019. Comparing to the next Sheet2 (Image 2 ) for the same product bearing which is having the created dates as 10/18/2019, 11/08/2019,10/16/2019.
The formula should now pick a date which is nearest lesser to the imported date which is in the sheet1. in this case, formula to pick the date 10/18/2019.
I tried to sort the data to lowest to highest and highest to lowest, but v-lookup results were not always right. I tried to insert the IF condition resulted in no luck.
It would be very helpful if you all share some insight into this case.
Thank you for reading the above case.
Use AGGREGATE:
=AGGREGATE(14,7,B2:B100/((A2:A100="Bearing")*(B2:B100<=Date(2019,10,22)),1)

I need to convert total date to month only

I set up a google forms form for my work where my employees pass information and this information is recorded in a spreadsheet. The information, when recorded, automatically inserts a date and time in the first column of form responses. However, when I enter the code = month (a1), it always returns the answer "1" or "January" and this information does not match the date entered in the column. How do I fix this?
If you are entering '=month(a1)' for every row, then you are always taking the month of the top left cell in the sheet. You would need to adjust the row number for the row you are in.
You could use something like '=month(now())' to ensure you are always getting the month of the current date.

Copy record in Excel up to specific date

I'm trying to copy a unique row (Row with PUMP for example), "x" amount of times. Eventually, the interval should be based on the column frequency (Frequentie). A record should be copied until the mentioned date, for example, when 01-01-2023 has been reached. This firstly should be completed for the first row and when that has been accomplished it needs to be done for the second row. This to create a dataset which can be used in Power BI. Can anybody help me with this problem?
In the image attached is an example given of the current data set.
Many thanks in advance!
It is always a good idea to show what have you tried so far, because the way the question is asked it looks like you are asking for the whole solution, without any input from your site. And this is a bit against the rules of SO. However, this is something to get you started:
Read the dates from the excel table. Like firstDate = Range("L2")
Locate the max and the min dates. They will be firstDate and lastDate.
Read about loops and copying of Excel range.
Try to do a loop between firstDate and lastDate and copy range valeus in the loop.
This is some loop example, writing dates between two dates on a new row:
Public Sub TestMe()
Dim firstDate As Date
Dim lastDate As Date
Dim cnt As Long
firstDate = DateSerial(2010, 12, 1)
lastDate = DateSerial(2011, 12, 1)
For cnt = 1 To lastDate - firstDate
Cells(cnt, 1) = CDate(firstDate + cnt)
Next cnt
End Sub

Automatically populate data from active excel worksheet to another

I have a table of targets vs accomplishments per quarter. I have a 1st quarter and 2nd quarter worksheet. What I want to achieve is every time i put data to the 1st quarter on 1st quarter worksheet, it automatically populate the columns/rows in the 2nd quarter worksheet. Please see attached screen shots.
If I understand correctly, you want to have the Q1 cells populated in the Q2 worksheet with the values you enter in Q1 sheet.
Can't you simply put a reference to the cell in Q1?
='Q1SheetName'!CellAddress
You may use Excel formuar to link value,e.g. in Sheet2 cell C11 =Sheet1!B11

If particular cell is NOT BLANK copy down FORMULA from above cell

Basically I have the following formulas:
Column J: =IF(ISNA(VLOOKUP(I2,CCG,1,FALSE)),"Out of Area",VLOOKUP(I2,CCG,1,FALSE))
Column K: =INDEX(ResponsibleAgency,MATCH(N3,LeftLookup,0),MATCH(J3,TopLookup,0))
Column L: =IF(ISNA(VLOOKUP(N3,PPLookup,2,FALSE)),"Missing F Code",VLOOKUP(N3,PPLookup,2,FALSE))
Column M: =IF(ISNA(VLOOKUP(N3,PPLookup,3,FALSE))," ",VLOOKUP(N3,PPLookup,3,FALSE))
Basically, I only want these formulas to activiate provided that column A is populated with a date. If there is no date in column A, I want the cells to remain blank.
Is this possible?
Technically speaking, it is not possible to tell if cell contains a date or not in strict sense.
In Excel dates are in fact numbers representing number of days since January 1, 1900. I.e. today is 42066. It's the formatting that makes those numbers look like dates to user. If you pass a cell containing a date to any formula, it receives this number.
As a workaround, you can check if the cell satisfies two conditions:
It is a number.
It falls into some date range that makes sense.
For example, if your column contains goods delivery dates you usually don't expect them be previous century or 50 years into the future.
So you can wrap your formulae into something like this:
=IF(ISNUMBER(A1),IF(AND(A1>=DATE(2014,1,1),A1<=DATE(2015,12,13)),[LOGIC_IF_CORRECT_DATE_HERE],""),"")
Being said, it's still a workaround.