Copy all Rows in excel sheet only if specific cell = today's date - vba

I'm knew to vb scripting for excel and cant seem to find code that will help do what i want. maybe its how im wording my search criteria.
Sheet 1 is an input sheet which saves data into sheet 2 every day. There is a cell that holds the current date.
So sheet 2 just collates and saves everything entered.
Each day there are several rows saved into sheet 2.
I need a button on sheet two that only selects the rows that have today's date and copies the content. I just need that data copied so i can then paste this into off clipboard into another application.
Can anyone help? im using office 2016.
Thanks

Excel VBA, How to select rows based on data in a column?
I believe this is something that would be useful for yourself since you are new to VBA code.
You will most likely need to supplement your code with the parts you ned, IE current date, only coping and not pasting and so forth.
I recommend google for those parts as it is widely avalible, I.E. VBA Excel "then what you are searching for"
Dim TodaysDate As Date
TodaysDate = Now
TodaysDate = Format(TodaysDate, "dd/mm/yyyy")
^ For instance will give you the current day in dd/mm/yyyy format
I leave the rest to you, hope this helps

I would loop through the rows in the second sheet and hide the rows not matching the date. So something similar to this:
Dim today As String
Dim Rows As Integer
Dim Sheetname As String
Dim Column As Integer
Sheetname = "Sheet2" 'Name of your second sheet
Column = 1 'Column with your date
today = Now
today = Format(today, "dd/mm/yyyy")
Rows = Sheets(Sheetname).UsedRange.SpecialCells(xlCellTypeLastCell).Row
Dim i As Integer
For i = 1 To Rows Step 1
If Sheets(Sheetname).Cells(i, Column).Value <> today Then
Sheets(Sheetname).Rows(i).EntireRow.Hidden = True
End If
Next i
Then you can copy the rows of today.
To show all rows again, simply use
Dim Sheetname As String
Sheetname = "Sheet2" 'name of your second sheet
Sheets(Sheetname).Cells.EntireRow.Hidden = False

Related

Excel VBA - Copy Paste Range in column if Date matches

My macro is used to copy/paste live data to overwrite an old budget.
Essentially I want to do the following:
If the date in T6 = date in Range(G6:R6) then copy Range(T10:T30) to Range(?10:?30), where ? is the column of the cell that matched the date.
This should match what you're asking for. In the future, you should share the code that you've attempted so far.
Dim cell As Range
For Each cell In Range("G6:R6")
If cell.value = Range("T6").value Then
Range(Cells(10, cell.Column), Cells(30, cell.Column)).value = Range("T10:T30").value
End If
Next cell

Excel Macro To Advance Date

I have a ton of excel sheets that each have 3 excel workbook tabs. On the last one there will be a ton of data but one column will be a date column with a bunch of different dates underneath. The date format will be MM/DD/YYYY. I need to advance each date ahead by 4 years.
I imagine that I will need to select the correct workbook, search for the particular column, and then loop to iterate through each value underneath that column to advance it, but the day itself needs to stay the same. For example, if its 10/05/2017, it needs to be 10/05/2021. Any suggestions or help would be great. Thank you in advance.
Thank you for the help, I realize I wasn't very helpful at all with my question. I'm very new to VB script and excel macros in general. I hadn't gotten how to search for the column itself as I would like it find the column no matter what the column value is (possibly search for a cell that says "Date" through the entire sheet?, I was just trying to add the 4 years to start with and couldn't find the function I needed. This is what I had from what I derived and seems like this is very wrong ha.
Do Until IsEmpty(ActiveCell)
Set ThisCell = ActiveCell
ThisCell = DateAdd("yyyy", 4, ColumnValueHere)
' Step down 1 row from present location.
ActiveCell.Offset(1, 0).Select
Loop
You'll be wanting the DateAdd function
Try something like this:
Sub AddDates()
Dim lastRow as integer
Dim theSheetImWorkingOn as worksheet
Dim theColumnNumberForTheDates as integer
theColumnNumberForTheDates = 5 ' change this to be the column number you want
Set theSheetImWorkingOn = Sheets("Put your sheet name here")
lastRow = theSheetImWorkingOn.Cells(1000000, theColumnNumberForTheDates ).End(xlUp).row
For x = 2 to lastRow ' assuming your data starts on row 2
theSheetImWorkingOn.Cells(x, theColumnNumberForTheDates) = DateAdd("yyyy", 4, theSheetImWorkingOn.Cells(x, theColumnNumberForTheDates))
Next x
End Sub

How to select column and display its current format using VBA Macro?

Please find my requirement below for which I am unable to find any solution:
1. Iterate over workSheet from workbook
2. Find all the columns containing date values using current format/type of column (Here is a trick. Worksheet is not static, it can contain any number of columns containing date values. Columns containing date values may have any name. And such worksheets can be more than one in number)
3. Apply macro on date columns for date formatting (below macro) if "Flag" value is "y"
<code>
Sub FormatDate()
If wksSecDist.Range("Flag").value = "y" Then
LastRowColA = Range("X" & Rows.Count).End(xlUp).Row
' Here I am finding total number of rows in column X
wksSecDist.Range("X2", "X" & LastRowColA).NumberFormat = "dd/mmm/yyyy"
' Here applying specified date format to Range("X2", "X10") [if last row index for column X is 10]
End If
End Sub
</code>
I am just a beginner to VBA.
Thanks in advance.
I suspect you didn't find a solution on the internet because you looked simply for a solution and not the parts needed to build your own solution.
You mention you are a VBA beginner, please take the below answer to be of educational use and begin you in getting you where you need your tool to be. Note, if it doesn't answer your question because of information that was not included, it has still answered your question and the missing information should form part of a new question. That said, lets get this function up and running.
From what you have written I have interpreted the requirement to be: -
Look over all worksheets in a workbook ('worksheets can be more than one in number')
Check every column to see if it holds a date value
If it does, set the whole column to a specific format
What is needed to accomplish this is iteration(loops), one to loop through all worksheet, and another to loop through all columns: -
The is pseudo code of the target: -
.For each Worksheet in the Workbook
..For each Column in the Worksheet
...If the Column contains dates then format it as required
..Process next column
.Process next Worksheet
We achieve this using a variable to reference a Worksheet and using a loop (For Each) to change the reference. The same goes for the columns.
Public Sub Sample()
Dim WkSht As Excel.Worksheet
Dim LngCols As Long
Dim LngCol As Long
'This loop will process the code inside it against every worksheet in this Workbook
For Each WkSht In ThisWorkbook.Worksheets
'Go to the top right of the worksheet and then come in, this finds the last used column
LngCols = WkSht.Range(WkSht.Cells(1, WkSht.Columns.Count).Address).End(xlToLeft).Column
'This loop will process the code inside it against every column in the worksheet
For LngCol = 1 To LngCols
'If the first cell contains a date then we should format the column
If IsDate(WkSht.Cells(2, LngCol)) Then
'Set right to the bottom of the sheet
WkSht.Range(WkSht.Cells(2, LngCol), WkSht.Cells(WkSht.Rows.Count, LngCol)).NumberFormat = "dd/mmm/yyyy"
End If
Next
Next
End Sub
Hopefully that has all made sense, this does work on the premise that the header row is always row 1 and there are no gaps in the columns, but these are separate issues you can approach when you're ready to.

Populating multiple sheets with information from a table

I am working on filling out 100 instances of a form from data that I have in a master table. I am looking for a way to automate this.
The form has the fields NAME and DATE (as well as others) I am looking to create a code that will take the NAME and DATE from a row in the master table, fill it into the form that is on a separate sheet, and then repeat the process for the next row and sheet. Example:
Sheet 1: Will take NAME and DATE from Row A
Sheet 2: Will take NAME and DATE from Row B
Sheet 3: Will take NAME and DATE from Row C
...
I'm attempting to learn VB on the fly with this, but have been unsuccessful thus far.
For my quick attempt to work, your sheets will need to have similar names with an incremented number at the end (sheet1, sheet2, ...) and your master data table sheet to be named "MasterData". Also it assumes your names are in column A and dates, column B, and that you start at the first row.
Sub NameDate()
Dim i As Integer
For i = 1 To 100 'assuming 100 sheets
Sheets("Sheet" & i).Range("A1") = Sheets("MasterData").Range("A" & i) 'placing the name in row i in the Sheet's A1 cell
Sheets("Sheet" & i).Range("B1") = Sheets("MasterData").Range("B" & i) 'placing the date in row i in the Sheet's B1 cell
Next 'next i = next row for the master data sheet AND the next sheet as well
End Sub
This is extremely basic, highly improveable, and I can explain how it works in the comments if you don't understand.
Try something like this:
I would highly recommend you test this code on a seperate worksheet first with only a few "names" and "dates"
Let me know if this is what you needed or if you need it to be modified!
Create a new button with the following code:
Dim ColumnA As String
Dim ColumnB As String
Range("A2").Select
Do Until IsEmpty(ActiveCell)
ColumnA = ActiveCell.Value
ColumnB = ActiveCell.Offset(columnOffset:=1).Value
ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
Range("A1").Value = ColumnA
Range("B1").Value = ColumnB
ThisWorkbook.Sheets("Sheet1").Activate
ActiveCell.Offset(rowOffset:=1).Select
Loop
End Sub

Excel: Use values in a sheet as index to list in a different sheet and replace values in the first sheet

I have an XL file with some data to be manipulated. I think I will need to use a VB script to do this - but perhaps there is a simpler way with a formula. Just the same, could someone point out BOTH ways of achieving the following?
I have a column of numeric values (ID) in Sheet 1.
I want to use each ID as an index to lookup a list in Sheet 2.
Sheet 2 has two columns
First column is the index and Second column is the Text String
e.g.
1 Apple
2 Orange
3 Pear
What I want is to replace the column of IDs in sheet 1 with the looked up text string from Sheet 2!
Thats all...
Please help!
Not a tough situation there. Here are some solutions...
With VBA:
I know you said you're a little new with VB so I tried to explain each line as I went along. Also, the code is free-handed so forgive me if I left an error in there somewhere.
Sub replaceData()
dim i as integer, j as integer 'These are just some variables we'll use later.
dim sheetOne as worksheet, sheetTwo as worksheet, myWb as workbook
dim myData as string, myId as string
set myWB = excel.activeworkbook 'These three lines set your workbook/sheet variables.
set sheetOne = myWB.worksheets("Old Data")
set sheetTwo = myWB.worksheets("New Data")
for i = 1 to sheetTwo.usedrange.rows.count 'This loops through the rows on your second sheet.
myId = sheetTwo.cells(i,1).value 'This assigns the value for your id and the data on your second sheet.
myData = sheetTwo.cells(i,2).value
for j = 1 to sheetOne.usedrange.rows.count 'This loops through the rows on your first sheet.
if sheetOne.cells(j,1).value = myId then 'This checks each row for a matching id value.
sheetOne.cells(j,1).value = myData 'This replaces that id with the data we got from the second sheet.
end if
next j
next i
end sub
With an Excel formula:
Place the following formula in cell C1 of the first worksheet (the
sheet with the IDs you will be replacing). **Note that you will
have to replace the "InsertSheetTwoNameHere" portion with the name
of your second sheet (don't remove those single quotes though). Also
note you will need to replace the "1000" with the number of the last
used row in sheet two.
=vlookup(A1,’InsertSheetTwoNameHere’!$A$1:$B$1000,2,FALSE)
Next simply drag the handle on the cell that makes it copy itself
(whatever the heck it's called) all the way down to the end of your
range.
Next, copy those cells and then paste them over the IDs using the
Values Only setting.
Hope this helps and good luck.