VBA Excel Date Generator - vba

I was wondering if there is a way using vba or excel to make an array of each day starting with Oct. 10th 2010 and ending with Sept. 30 2015. Unfortunately the dates have to be in the format YYYYMMDD, such as 20091001 and 20150930.
Thanks for the help

For a non-VBA solution KingsInnerSoul's solution is very good.
For VBA, you could do something like this:
Sub tgr()
Dim arrDates() As Variant
Dim dtStart As Date
Dim dtEnd As Date
'Assign start and end dates
dtStart = CDate("Oct 10, 2010")
dtEnd = CDate("Sep 30, 2015")
'Create the array by evaluating an Index formula
arrDates = Application.Transpose(Evaluate("INDEX(TEXT(""" & dtStart & """+ROW(1:" & dtEnd - dtStart + 1 & ")-1,""YYYYMMDD""),)"))
'Example of how to access the array values
MsgBox arrDates(1) & Chr(10) & _
arrDates(2) & Chr(10) & _
arrDates(3)
'You could iterate over the array with a loop also,
'or use any other array function, like Filter(), etc
End Sub

Create a list in Excel:
If you go to excel,
In the first top cell type 10/10/2010
Then right click on the cell - and select Format Cells,
Then go to the Custom category
In the Type input area type yyyymmdd - verify that it is the right format in the Sample field (I had it set to yyyymmdd;# )
Press OK
Once the cell is highlighted, you can see a border around it, and a + in the lower right corner of the cell.
Click on the + and drag it down as many rows as you need to - it will copy the cell's content and create a series with each cell adding a day.
Create a single row list in Word:
Once you created that single column in Excel, in the column next to it, type , (comma), and drag it down the same way in order to copy it next to the whole series.
Copy both columns in Excel.
Paste it to MS Word.
In the lower right corner an icon will appear - Paste Options.
Click on the Paste Options icon and select 'Keep Text Only' - It is the 'A' icon. Now you will have a single text column with the number, a tab character, comma, and Enter character.
Open the Search and Replace by pressing CTRL+H.
In the 'Find What' input field type ^p - it is the code for Enter.
Leave the `Replace With' field empty.
Press on `Replace All'.
Confirm the number of total replacements popup window by pressing OK.
To get rid of the Tab character, repeat steps 6-10 but search and replace ^w.
More information about special characters.
Related questions:
VBA convert range to array and format date
excel vba filling array
VBA Excel - Problems with a simple macro to auto-fill cells for a budgeting spreadsheet I'm attempting to make
Fill two columns with random dates
How to force Excel to automatically fill prior year in column instead of current year?
Fill dates array and add dummy variables
https://stackoverflow.com/questions/25277464/fill-dates-gap-in-excel

For the sake of an image, add October 10, 2010 (formatted yyyymmdd) in say A1 and HOME > Editing, - Fill:, Series...
for a column (or change Series in selection for in a row).

Related

Vba turns cell in to date on mistake

Hi how can I get around vba is converting a cell automatic to a data format when I add a value like 10 - 12 I just want the value in the cell to be 10 - 12 not a date
the code I use is "cFli is the value 10 - 12":
tsheet.Range("E2").Value = cFli
You can prepend a single-quotation mark to the value of a cell, which indicates to Excel to treat the rest of the string as text:
tsheet.Range("E2").Value = "'" & cFli
Note: The ' does not become part of the cell contents, although it will appear when editing the cell in future.
Alternatively, you can achieve the same thing by formatting the cell using the Text format:
tsheet.Range("E2").NumberFormat = "#"
tsheet.Range("E2").Value = cFli
Note: The formatting must be done before putting the value into the cell.

VBA - Copy and Paste with increment

Hi i need help on creating a VBA to copy range of cells repeatedly with one column having increments.
Current data
Expected Output
I found a vba but will only copy the rows based on column C without increments on the date
Excel VBA automation - copy row "x" number of times based on cell value
I'm not sure why you would need to do this as excel has built-pattern pattern recognition for these scenarios, after entering two succesive dates if you hover over and click the border of the cell then drag down, the desired date range will appear in the column automatically.
If you still insist on doing this programatically for whatever reason then your question already has multiply feasible solutions here: Add one day to date in cells using VBA
Specify each cell in turn then increment the value by the corresponding row number to return desired date range in column A:
Range("A2").Value = Range("A2").Value + 2 ' add 2 days
Range("A3").Value = Range("A3").Value + 3 ' add 3 days
Range("A4").Value = Range("A4").Value + 4 ' add 4 days
'-------- So on and so forth until desired range is acheived --------'
or alternatively:
Range("A2").value = DateAdd("d", 2, CDate(Range("A2")))
Range("A3").value = DateAdd("d", 3, CDate(Range("A3")))
Speaking as someone who had to learn the hard way, please take my advice and ensure you research your problem thouroughly to find a solution before posting. Refer to the guidelines here if needed.

How to increase only last three digits of a serial number string in Excel VBA?

I am trying to create a button in excel that will add a new serial number to the next available line. The serial number will be DC001 and the next DC002 and so on... The code will need to look at the last 3 digits on the serial number and increase by one, then add this number to the next available row. I cant seem to be able to do this with the DC in front of the number. Is anyone able to help please? Thank you in advance.
In the excel worksheet use this formula:
=LEFT(A1,2) & TEXT(INT(RIGHT(A1,3)) + 1, "000")
Explanation:
The result will be the concatenation (&) of your original 2-character prefix (LEFT(A1,2)) and the original number (INT(RIGHT(A1,3))) increased by 1 and converted to text (TEXT) with 3-digits ("000")
Or use its equivalent in VBA:
Left(Your_variable, 2) & Format(Int(Right(Your_variable, 3)) + 1, "000")
as the code for your button.
See Add a button and assign a macro to it in a worksheet if you have problem how to use this VBA formula.

If dropdown value is selected add formatting and values to cells to the right

Ok so I am making a sheet where a user would select a value from a dropdown and then needs to see the required parameters based on the selection.
Example:
If i select X from the drop down then three new input fields would be created and formatted as input cells with the parameter as the default value (so the yellowish orange input format with default text explaining what the parameter is -- like start date for one then the next end date)
My start:
Sub test()
If ActiveCell = "setEnrollnet" Then
ActiveCell.Offset(0, 1) = startDate
ActiveCell.Offset(1).EntireRow.Insert
ActiveCell.Offset(1, 1) = 0
ActiveCell.Offset(0, 2) = endDate
ActiveCell.Offset(1).EntireRow.Insert
ActiveCell.Offset(1, 2) = 0
End If
End Sub
This does not work well and it does not apply formatting. I need help figuring out how to do both format and input the correct text based on selection in another cell.
I suggest - for each value in the drop down list - you define the input fields in a seperate sheet, together with field labels and formats. After selection from the drodown, you scan the parameter table and copy whatever is listed for that parameter to your input sheet.
Example (range named InputParams):
ParamVal InputLabel InputField
X StartDate (empty but formatted)
X EndDate (empty but formatted)
Y Foo default_Foo
Y Bar default_Bar
Upon selecting "X" from the dropdown list you walk through InputParams and,
If InputParams(Idx, 1) = DropDownValue Then copy cells InputParams(Index,2) and InputParams(Index,3) to your input range - by default formats are copied along with content.
It is desireable to define a named range also for Input, giving it a size of maximum parameters expected, so that you can clear content and formats with one single statement prior to re-populating this range when walking through the InputParams list.

delete data in cell after specific character

I have data in cells A1:A1000. It is a list of names followed by a small note, like this:
sam" fast
nick" long
tom" quick
They all have " and a space after the names and then the note. What I am trying to do is delete the everything after the name.
I was playing around with macros to try and do this, but could not get anything to work. Any idea how I might do this?
Here is a nifty trick without macros:
Select the proper range (or even just click on A to select the entire column) and then do Ctrl+F, click Replace, in Find write exactly "* and leave the Replace with box empty. Now click Replace all and tada !
It replaces everything after (and including) the quote with nothing because it uses * as a wildcard you left the replace box empty.
Edit: As suggested here is the VBA code for this:
Columns("A:A").Replace What:="""*", Replacement:="", LookAt:=xlPart
Easy! I don't know what version of Excel you are using, but in short you want to do a Convert Text to Columns and then split the cells using a delimiter of ". This will leave you with two columns, one of the data you want and one you can just delete.
Here is the walk through in Office 2010:
Highlight column A
find the Data menu
find the Convert Text to Columns menu
Pick Delimited and hit next
In the Other box, type "
hit Finish
Done! Now you have all your names in column A and you can just delete column B.
To sum up, do a "Convert Text to Columns" and then split the cells using a delimiter of ". Super easy and fast.
few options:
Replace
Range("A1:A1000").Replace """*", vbNullString
If you require to manipulate the value further then the below are more appropriate:
With Regex:
Dim str As String, strClean As String
Dim cell As Range
For Each cell In Range("A1:A1000")
With CreateObject("vbscript.regexp")
.Pattern = "\""(.*)"
.Global = True
cell = .Replace(cell, vbNullString)
End With
Next cell
Without Regex, splitting the string:
Dim strSplit() As String
Dim cell As Range
For Each cell In Range("A1:A1000")
If (cell.Value <> vbNullString) Then
cell.Value = Split(cell.Value, """")(0)
End If
Next cell
In case you want to keep your source data, you can also do it with a simple Excel formula in the next column. Assuming that your data is in column A, the following formula will return only the name: =LEFT(A1,SEARCH("""",A1)-1)
Sub Macro1()
For Row = 1 To 1000
S = Range("A" & Row).Cells.Value
Pos = InStr(S, Chr(34))
If Pos > 0 Then Range("A" & Row).Cells.Value = Left(S, Pos - 1)
Next
End Sub
Press ctrl + f, click on replace tab, type * in the find what box and then click on replace all. No need to put anything in replace box. Here you are replacing everything after ..