Copy record in Excel up to specific date - vba

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

Related

How to use VBA to change a date in SQL query in excel

I need help if it is possible where I can upload SQL query in excel via Get Data>From Database>From SQL Server Database, and be able to change the date in this uploaded SQL query.
My hope is to use three cells within tab where i enter a date, and either use a vba or SQL looks cell value automatically and provide results when i refresh SQL.
Right now i have these sets of dates.
set #eomdate = '2022-10-31'
set #startdate = '2016-01-01'
set #enddate = '2022-10-31'
I want to use cells in excel that lookup date entered in cell A1, B1, and C1. My goal is to change the dates to get results based on dates entered.
set #eomdate = Sheet1A1
set #startdate = Sheet1B1
set #enddate = Sheet1C1
is there a way to use a VBA code that will change the dates based on cells mentioned above? I am researching but have been unlucky.
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Set ws = Worksheets("Query1")
`ActiveWorkbook.RefreshAll`
End Sub
Any help is greatly appreciated.
Thank you,
Kosta
This is the first time I am trying to do this but I am hoping that when I enter a date in cell A1, and i run a vba CommandButton1, the SQL that was uploaded in excel, will change the date prior running the SQL query.

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

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

How can I generate an ID that will reset every year?

I need help in VB.net I'm a newbie in programming.
I have a problem in generating an ID with the format
`yyyy` = now.year
`mm` = now.month
`0000` = auto number
I want to increment the auto number everytime I add records
so it goes like this
2014090001
and also it will reset back to 0000 when year changes.
thank you for all your replies, I took all your advice and I got a solution CONSIDER THIS THREAD AS CLOSE!!
my final solution is creating another table in database with the fields ID, counter, and YEAR
I created a condition that will test the current year, so if the year = now.year the counter will just increment by 1 and if year <> to now.year then the counter will back to 0. it will add up another records in that table so the use of ID is to delete the previous records.

First Day Of Month For Calculated Number Of Months

I'm running into an issue where I need some more guidance. I've got a form that allows users to enter a start date and end date to represent the length of a contract. I'm using the following query to calculate the monthly amount :
SELECT t_Actuals.InvoiceAmount,
(DateDiff("m",[StartDate],[EndDate])+1) AS [Contract Term],
CCur(Round(([InvoiceAmount]/[Contract Term]),0)) AS MonthlyAmount
FROM t_Actuals;
Our VP wants the MonthlyAmount projected throughout the length of the Contract Term (ie. 11 months from Start Date). Ideally, I would like to allocate the MonthlyAmount in a MM-YYYY (01/2012, 02,2012) format throughout the length of the contract term, but, admittedly, I am not that experienced with working with dates in Access outside of the Query Grid.
Regardless, I've been reading about the DateSerial function to find the first day of the month, but how would you implement that into a loop to find the value of the first date of the month for the next 36 months and write that date to a table with the projected date as fldDate and MonthlyAmount.
Sorry, this is kind of a convuluted question. Any advice would be greatly appreciated. Thank you in advance for any direction.
"a loop to find the value of the first date of the month for the next 36 months and write that date to a table with the projected date as fldDate and MonthlyAmount"
Given the starting date of the first month and the number of months, you can use the DateAdd() function to determine the rest of the dates you need to insert. Open your table as a DAO recordset, use its .AddNew method to add a row, assign the values you want in the fields, and call .Update to store the row.
Sounded like your challenge was for the date piece, so I sketched that out and left the MonthlyAmount piece for you to complete.
Public Sub LoadMonthStartDates(ByVal pFirstDate As Date, _
ByVal pMonths As Long)
Const cstrTable = "YourTableNameHere"
Dim db As DAO.database
Dim rs As DAO.Recordset
Dim i As Long
Set db = CurrentDb
Set rs = db.OpenRecordset(cstrTable, dbOpenTable, dbAppendOnly)
For i = 1 To pMonths
rs.AddNew
rs!fldDate = DateAdd("m", i - 1, pFirstDate)
'rs!MonthlyAmount = <your calculated amount>
rs.Update
Next
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub

How to display a next day date?

Using VB.NET
Using DatagridView, In a Datagrid View values are displaying like this
ID Date
001 23/02/2009
001 24/02/2009
001 25/02/2009
I want to display a date in a textbox after 25/02/2009
I Used a sql query for getting a next date
Select CONVERT(CHAR(10), DATEADD(dd, 1, MAX(SDate)), 103) AS SDate from tb_Sched_Add
the above query is displaying a next date 26/02/2009 in the textbox, but it is taking a some second to display. There is any way in program itself getting a last value of the row (date) in datagridview and display the next date.
Need vb.net code help
If you have a date in your VB.NET code you can use the DateTime.AddDays method:
Dim latestDate As DateTime = SomeMethodThatGetsLastDate()
Dim nextDate As DateTime = latestDate.AddDays(1)
Perhaps you have populated a list with the existing items, so that you can obtain the latest date from that data?
THis option is nice, tho then if you are to iterate with a button click, you are likely to land into issues of not going more than 2 days in go. So a counter has to be in place to increment the value in AddDays(1) so replace "1" with counter++