Autopopulate via date - excel

I have a workbook that I'm trying to figure out how to do what I want. Macros are not an issue and Im certain I will have to use vba for my needs but dont know enough to put a dent in what i want to do.
The Basic premise is as follows. I have a two sheet workbook, Sheet 1 contains the current date and two fields one for #of Calls and the other for # of completes. Sheet 2 has a series of dates (for the month) and the same 2 fields form sheet 1 for all the dates. What I want to happen is, depending on the date on sheet 1 then sheet 2 will autofill the proper fields for that date. Leaving all other fields alone. I cannot get it to where it will leave the rest of the spreedsheet alone (not blank them out or put 0) and only populate the correct date.
I have uploaded a sample spreed sheet here. If anyone could provide me with any direction I would be greatly appreciative. If anything is unclear then please ask for clarification. Thanks in advanced.
STP

You may run this macro:
Option Explicit
Sub r()
Dim c as Range
For Each c In Worksheets("Sheet2").Range("A2:A999")
If c.Value = Worksheets("Sheet1").Range("A2").Value Then
c.Offset(0, 1).Value = Worksheets("Sheet1").Range("b2").Value
c.Offset(0, 2).Value = Worksheets("Sheet1").Range("c2").Value
End If
Next c
End Sub

Related

If I add a row in Worksheet 1; First column should be transferred to Worksheet 2; rest of the row should be empty in Worksheet 2

Hopefully I can explain the issue well. If not, PLEASE ask, I will try to explain it as good as I can.
I have 2 different worksheets.
Scenario 1:
Sheet 1 has 2 columns and several rows.
Sheet 2 has the same data in the first column as Sheet 1 but the data of the other columns are different.
Change to Scenario 2: If I add a row in Sheet 1 the first cell of this row (“new”) should also be automatically added in Sheet 2. And in the second/third/… row of the second worksheet should be no data, as somebody has to fill it in manually.
Just the first column has to be the same in both worksheets. BUT the row has to be added in the second worksheet automatically also but without data.
I really hope somebody can help me! Thanks so much in advance!
I tried it with macros and different formulas. But unfortunately I never got the right solution.
You can do this, using this macro (there might be some errors in it):
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
Sheets(2).Range("A1").End(xlDown).Offset(1, 0).Value = Target.Value
End If
End Sub
For your information, this macro needs to "belong" to your first sheet:

Excel VBA - dynamic data lookup between sheets in VBA

I have been busy working on a number of dynamic excel workbooks learning on the go through forums and content like this site, but as I am coming to the final parts that will round off my workflow I am stumped by what I assumed would be a fairly simple VBA code.
Not wanting to muddy the water with the code I currently have, I will instead explain clearly the problem I am trying to solve, and the ways I have tried to approach this. Note that I am very much a novice, and would appreciate being pointed in the direction of some code that I am able to copy and amend to suit.
Problem
I have two sheets;
"Purchase_Orders" - is a master list of total value purchase orders with information associated with the total value.
"Purchase_Ledger" - is a full list of payment transactions that include a mixture of payments which are project and non-project related.
As a project will have multiple purchase order numbers, and purchase orders have multiple payments, I am looking to lock the sheet down so that the user selects the purchase order number they are making a payment against, and the columns for project data are automatically pulled across from "Purchase_Orders" that are associated with the purchase order.
The data is structured as follows:
"Purchase_Orders"
(A,7-4000) Purchase Order number
(B,7-4000) Project Number
(C,7-4000) Project Name
"Purchase_Ledger"
(A,7-10000) Payment Reference
(B,7-10000) Purchase Order number - purchase order is selected from a drop-down list of numbers validated from the list on "Purchase_Orders"
(C,7-10000) Project Number - to be copied across
(D,7-10000) Project Name - to be copied across
My Attempts
I have already spent so much time working on this from different angles. As there are likely to be some purchases that are required that do not necessarily require a purchase order number, I want to keep the project number column open and free from formulae.
Currently, I have an index and match formulae in VBA which only works when run manually and copies the whole columns across. Ideally, I am looking for only the relevant cells to update when a purchase order is selected. I am working on code to trigger the macro when the drop-down is selected, but this is all very clumsy and not what I am seeking to achieve.
I have also tried using vlookup, but again this would only work with a trigger and not dynamic.
Hopefully, this is a sizeable challenge for some to flex their grey matter. If you need more info let me know.
Thanks!
Jon
Current code:
Sub Copy_Purchase_Orders()
Dim k As Integer
For k = 7 To 10000
Cells(k, 6).Value = WorksheetFunction.Index(Range("B7:B4000"), WorksheetFunction.Match(Cells(k, 5).Value, Range("A7:A4000"), 0))
Cells(k, 7).Value = WorksheetFunction.Index(Range("C7:C4000"), WorksheetFunction.Match(Cells(k, 5).Value, Range("A7:A4000"), 0))
Next k
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Range("E7:E1000"), Range(Target.Address)) Is Nothing Then
Call Copy_Purchase_Orders
End If
End Sub
So your current code is called whenever a selection happens in column E and then cycling through all rows from 7 to 10000 and populating it with the formula?
You could change the method to accept the cell / target / range / address and then only cycle through each of its rows (instead of 7 to 10000)
Something like this (untested because I didn't have data sample and short on time):
Sub Copy_Purchase_Orders(target As Range)
Cells(target.Row, 6).Value = WorksheetFunction.Index(Range("B7:B4000"), WorksheetFunction.Match(Cells(target.Row, 5).Value, Range("A7:A4000"), 0))
Cells(target.Row, 7).Value = WorksheetFunction.Index(Range("C7:C4000"), WorksheetFunction.Match(Cells(target.Row, 5).Value, Range("A7:A4000"), 0))
End Sub
Private Sub Worksheet_Change(ByVal target As Range)
If Not Application.Intersect(Range("E7:E1000"), Range(target.Address)) Is Nothing Then
Call Copy_Purchase_Orders(target)
End If
End Sub
edit
To reference a particular worksheet when using Range objects, you should "qualify" them with the worksheet you're targeting (it's always safer to do this, you shouldn't really use Range or Cell objects alone:
So if want to move sheet 2 data to sheet 1 data, Instead of:
Range("A1").Value = Range("A1").Value '(VBA doesn't know which Range you mean)
You write:
Worksheets("Sheet 1").Range("A1").Value = Worksheets("Sheet 2").Range("A1").Value
So, you just need to qualify the ranges in your code, I think like so:
Cells(target.Row, 6).Value = WorksheetFunction.Index(Worksheets("Purchase_Orders").Range("B7:B4000"), WorksheetFunction.Match(Cells(target.Row, 5).Value, Worksheets("Purchase_Orders").Range("A7:A4000"), 0))
I think for the purposes of your code at the minute, you can leave it there. If you decided to build more on these existing routines, you should really qualify your Cell objects too, however it would end up very wordy:
Worksheets("Purchase_Ledger").Cells(target.Row, 6).Value = WorksheetFunction.Index(Worksheets("Purchase_Orders").Range("B7:B4000"), WorksheetFunction.Match(Worksheets("Purchase_Ledger").Cells(target.Row, 5).Value, Worksheets("Purchase_Orders").Range("A7:A4000"), 0))
To cut down on mess, you can declare worksheets as variables and set them as particular sheets:
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Worksheets("Purhcase_Orders")
Set ws2 = Worksheets("Purchase_Ledger")
Then use them in place of writing their full object name:
ws2.Cells(target.Row, 6).Value = WorksheetFunction.Index(ws1.Range("B7:B4000"), WorksheetFunction.Match(ws2.Cells(target.Row, 5).Value, ws1.Range("A7:A4000"), 0))
There are even more shortcuts and declarations you can use to make code more succinct, safer, reusable and scalable, if you choose to really dive into VBA coding. Ranges can be variables, using sheet codenames, using With blocks, in-line evaluate - all things that could be employed to your code so far.

Getting Excel to Copy Data From One Cell to Another Depending on Date

Apologies in advance as this is my first time posting something on this site and am not the best at explain issues.
I have a spread sheet, this has production data such as meters daily, meters monthly etc. These values are updated by adding TAGS from a PLC using Rockwell VantagePoint Excel add-in (if your unfamiliar with this it shouldn't matter this part is not what I am struggling with)
I need I way to copy data from one cell to another cell on the same sheet at month end. Basically the Meters monthly field needs to copied into another cell at the end of the month to record meters run for that month. The monthly meters run resets back to 0 at the end of the month.
Basically I need to copy the value in J7 into the corresponding month in W column at the end of that month. If it could ignore the year that would be advantageous as I don't need it to keep the old values and would mean I just need one column.
I have some experience at MS-Excel, also VBA but mainly in MS-Access never in MS-Excel. If answers could be explained as simply and hands on as possible it would be appreciated.
After Googling the issue I came across this formula and changed the ranges to fit my sheet but Excel doesn't like it saying it contains an error
=QUERY( A1:B6; "select B where A =date """&TEXT(TODAY();"yyyy-mm-dd")&""" "; 0
Sorry again if I haven't explained myself properly.
If your workbook isn't guaranteed to be open at the end of each month I would update the value every time it gets opened, like(Should be placed in ThisWorkbook):
'Runs when you open the workbook
Private Sub Workbook_Open()
'Loops through U3 to the last used cell in that column
For Each c In Range(Cells(3, 21), Cells(Rows.Count, 21).End(xlUp))
'Applies the J7 value to the current month and exits the sub
If Month(c) = Month(Now) Then c.Offset(, 2).Value = [J7]: Exit Sub
Next c
End Sub
Also, not that it matters but, I would apply the following formula in U3:U14 to always get the correct dates:
=EOMONTH(DATE(YEAR(TODAY()),ROW()-2,15),0)
Okay, I'm still not super sure what the question is and I know more Access VBA than Excel VBA, but here's something that might help to find a solution.
You can make a check date function that returns a Boolean value:
Public Function EoMonthCheck() As Boolean
Dim eo_month As Date, today As Date
eo_month = Format(WorksheetFunction.EoMonth(Now(), 0), "yyyy-MM-dd")
today = Format(Now(), "yyyy-MM-dd")
If today = eo_month Then
EoMonthCheck = True
Else
EoMonthCheck = False
End If
End Function
And the,, to add a value to the "W" column, we might use something like this:
Public Function AppendValue(Optional target_cell As String = "J7")
''' This could be a subroutine, too, I guess, since we're not returning anything.
Dim i As Integer
''' Activate whatever sheet you want to work with
Worksheets("Sheet1").Activate
If EoMonthCheck() = True Then
''' Look up the bottom of the 'W' column and find the first non-empty cell
''' Add 1 to that cell to get you to the next cell (the first empty one).
i = Cells(Rows.Count, "W").End(xlUp).Row + 1
''' Set the value of that empty cell in the 'W' column to the value of 'J7'
''' which will happen after we evaluate whether it is the end of the month.
Cells(i, "W").Value = Range(target_cell).Value
End If
Then, you could maybe trigger that each time the workbook opens.

Add total to column where row header meets criteria

Apologies beforehand for this, I'm very new to VBA and I'm sure this is simple coding that I'm struggling with.
Based on a user defined number of months (just entered into the ssheet) I have a row of column headings giving the month number and the heading "Total" after the last month.
i.e. User says 12 months, cell A2 = "Jan-15" and M2 = "Total".
Underneath the headings the user then adds some sales data.
What I need to do is use VBA to add a total to the end of each of the rows of data where the header = "total".
I was thinking an If Then makes sense but I'm struggling with how to get the macro to put the formula in the correct cell and then have the formula look at the variable range. This is the best I've come up with so far (please don't laugh!)
Sub Add_total()
Dim criteria As String
criteria = Range("A:A").Value
If criteria = "Total" Then
Range("12:12").Select
ActiveCell.Formula = "=sum($c12:c12)"
End If
End Sub
What I'd like is, for example, where M2 = "Total", then C12 = sum(c1:L12)
I realise the easiest way would be to just get the user to add the total themselves or have a total column far away to the left of the sheet but this is going across the business and needs to restrict manual input/guarantee accuracy of the calcs.
Any help much appreciated.
Why not simply put in cell M3 the function =sum(A3:L3) and copy downwards?
If you need it in VBA anyway should this work:
Sub testsum()
i = 3 'start in cell3
While Not IsEmpty(Cells(i, 1))
Cells(i, 13) = WorksheetFunction.Sum(Range((Cells(i, 1)), (Cells(i, 12))))
i = i + 1
Wend
End Sub
Thanks very much for that - not quite what I needed but you pointed me in the direction. Went for the following in the end:
Sub Total() Set Find_total = Range("6:6").Find("Total",_
LookIn:=xlValues,searchorder:=xlByColumns)
Set Revenue_total = Find_total.Offset(rowoffset:=3, columnoffset:=-1)
Set Revenue_cell = Find_total.Offset(rowoffset:=3, columnoffset:=0)
Revenue_cell = WorksheetFunction.Sum(Range((Cells(9, 2)), Revenue_total))
End Sub
Thanks again

Formula to pull text from one sheet in excel to another to summarize (not numbers)

I have a spreadsheet that has a tab for each research location. There is a section on the sheet that has several columns. Three of the columns are as follows: 1 lists action items (text) 1 lists who is responsible (text) and 1 lists the due date (date field). The rows in this same "table" represent categories. In many cases there is an action item only in one or two categories or maybe none at all for some.
I would like to query each tab that represents a research site and pull any action items, the responsible party and date onto another tab so that we can see all action items in one place for all the sites vs. going tab by tab to review.
I thought some sort of IF or VLOOKUP function might work, or some sort of pivot table but because it is text and not numbers I am having a hard time crafting the appropriate formula. I was also told I could do some sort of reference look up (like putting a word like ACTION at the start of any text I want to find later) but this seems more complicated than it needs to be.
Any help would be deeply appreciated.
I don't think VLOOKUP can solve your problem. You definitely need VBA so something like this will get you going. Make a new sheet called as Summary and put this code in the sheet:
Sub SummarizeSheets()
Dim ws As Worksheet
Application.ScreenUpdating = False
Sheets("Summary").Activate
Range("A2").Select
For Each ws In Worksheets
If ws.Name <> "Summary" Then
If ws.Range("A2") <> "" Then 'A2 is blank means no action items found so go to next worksheet
ws.Range("A2:C100000").Copy 'Adjust your range here
ActiveSheet.Paste Range("A65536").End(xlUp).Offset(2, 0)' Paste the copied range
End If
End If
Next ws
Application.CutCopyMode = False
End Sub
You have to adjust the code to suit your needs. Assumption here is your action items starts with cell A2 and responsible person and due date are in cell B2 and C2 respectively.

Resources