VBA: Finding the row number by looking up a user defined value (date) - vba

I am trying to use VBA to look up the row value that corresponds to a user defined date on one of my work sheets so that I am able to edit all data on that row.
As a bit of context:
I have several time series data sets that all have different start and end dates with a good portion of overlap in the middle. I want to chart these using user defined date parameters, however, because of non-uniform start dates, the chart is impossible to dynamically rebase.
I was hoping to use a macro to clone the data on one sheet, overwrite the line of values that corresponds to the user defined start date, and then calculate return values based on percentage change figures (I already have in a different sheet).
If I can dynamically o/w the row that corresponds to the UD start date of the date range, I can replace it with a one and all my calculations will effectively rebase.
Any and all feedback would be great!
EDIT
Lucas,
I am having two issues; firstly, what I have inexpertly cobbled together doesn't work when I protect the sheets (not insurmountable); secondly, it doesn't work :). Here is my work:
Sub Rebase()
Dim UDStartVal
Dim UDStartLoc As Range
Dim UDRow As Integer
'
' Rebase Macro
' A macro to rebase the chart to the user defined start date.
'
'
Sheets("Cumulative Monthly Returns").Select
Cells.Select
Selection.Copy
Sheets("Chart Numbers").Select
Range("A1").Select
ActiveSheet.Paste
' Lookup to change the value of the cells corresponding to the user defined start date to 0, effectivley rebasing the portfolo.
Worksheets("Cumulative Period Returns").Activate
UDStartVal = Cells(4, 2).Value
Set UDStartLoc = Range("A:A").SpecialCells(xlCellTypeVisible).Find(UDStartVal)
Set UDRow = UDStartLoc.Row
Stop
End Sub

Here's some code that I use to find the row of an entry based on quote numbers on a sheet that gets resorted and re-filtered constantly.
Private Sub FindQuote(partNum as String)
Dim quoteRow as Range
Set quoteRow = Range("A:A").SpecialCells(xlCellTypeVisible).Find(partNum)
then when I want to do something that uses the range of that row I use quoteRow.Row
If Not quoteRow Is Nothing Then
quoteNum = Cells(quoteRow.Row, "P").Value
Cells(quoteRow.Row, "Q").Value = "Found"
Else
MsgBox "No quote was found"
End If
End Sub
Did you need help with the part where you clone your sheet?

Related

How can I change formulas to static values after the formula has calculated the value?

I have an inventory sheet setup where a user is scanning part numbers into a sheet and a "date scanned" column will display the current date. The formula itself is working fine as it is showing the "today()" function to display the date. However, once the spreadsheet is open, the values now show today's date rather than the original scan in date (because the formula recalculates the date).
I've figured out how to change the formulas to values but I'm struggling with getting the timing right.
The below code does what is intended; it takes a look at all rows with a formula and converts them to values. The problem however, is that if a row does not have a value in it yet, just a formula, it will replace the formula with a blank.
I would like to make the code more robust and only change once a value has been determined by the formula (i.e. once a user scans in a part number). If a part number has not been scanned in, I would like the formula to stay in the column until done so by the user.
Sub makeStatic()
'
'Convert date formula to a static value
'
Dim rng As Range
For Each rng In ActiveSheet.Range("$E$2:E" & ActiveSheet.UsedRange.Rows.Count)
If rng.HasFormula Then
rng.Formula = rng.Value
End If
Next rng
End Sub
I figured this would be a combination of an if statement paired with an event handler but I haven't been able to decipher the best way to do it. Any help would be greatly appreciated.
Let me suggest a solution that gives you the result you are asking for, but in another way than you were sketching.
Let's consider your worksheet having the Part number in column A and Date scanned in column B. Both column A and B are empty, until the scanner enters the part number to column A. To react on a change in a cell in the A and end up with current date in B, you can write the following VBA function:
Option Explicit 'Excel worksheet change event for range A1 to A10
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A2:A10")) Is Nothing Then
Target.Cells(1, 2).Value = Date
End If
End Sub
Of course you would change the range as needed.

VBA: filter data based on cell value and fill in to another sheet

enter image description hereI Have two Excel Sheets ("Record") & ("Register"), " Register" is the database. from this database I need to create Records of each employees based on their employee ID (cell value). i am searching for a VBA code that should give me the training Record a each employee once i have enter their ID in the cell and click "a command button". Attached the Excel screen short for reference.
Steps 1: Enter Employee ID in the "Record" sheet
Step 2: Click command button "Filter" in the Record sheet
Step 3: VBA code to run and filter data from "Register" and fill "Record".
IF i Type another Employee ID in the sheet "Record" , it should ClearContents of the previous query. and produce the data.
Please help me, i am not good in VBA .attached the Excel screen short for reference ( UPDATE on 29/07/2018-Question Solved : sharing the code below; thank you Mr.ComradeMicha for your valuable feedback)
Sub Button2_Click()
'Declare the variables
Dim RegisterSh As Worksheet
Dim RecordSh As Worksheet
Dim EmployeeRange As Range
Dim rCell As Range
Dim i As Long
'Set the variables
Set RegisterSh = ThisWorkbook.Sheets("Register")
Set RecordSh = ThisWorkbook.Sheets("Record")
'Clear old data Record Sheet
RecordSh.Range("A8:F107").ClearContents
Set EmployeeRange = RegisterSh.Range(RegisterSh.Cells(6, 4), RegisterSh.Cells(Rows.Count, 6).End(xlUp))
'I went from the cell row6/column4 (or D6) and go down until the last non empty cell
i = 7
For Each rCell In EmployeeRange 'loop through each cell in the range
If rCell = RecordSh.Cells(4, 2) Then 'check if the cell is equal to "Record"
i = i + 1 'Row number (+1 everytime I found another "Record")
RecordSh.Cells(i, 1) = i - 7 'S No.
RecordSh.Cells(i, 2) = rCell.Offset(0, 2) 'Training name
RecordSh.Cells(i, 3) = rCell.Offset(0, -2) 'End date
RecordSh.Cells(i, 4) = rCell.Offset(0, 6) 'Validity
RecordSh.Cells(i, 5) = rCell.Offset(0, 7) 'Remarks
RecordSh.Cells(i, 6) = rCell.Offset(0, 5) 'Minimal requirement
End If
Next rCell
End Sub
Your code is missing a few essential parts you may want to look into:
It seems to require the user to select a specific row before the macro is started, even though there is a command button to trigger the macro. If the layout stays the same, just use constants to store the row where certain input or lookup fields are located.
ra is used both on the input form and on the lookup sheet. That's asking for trouble... Again, use constants or at least "StartingRow=3" or something similar.
You are correcting your employee number to a format that doesn't fit the data provided in the screenshot. Hopefully just a dummy data issue, but in case you are wondering why you don't find anything ;)
You are typecasting all fields individually. If your layout is always the same, it's much easier to just use the "variant" type for all cell values and make sure you already formatted all columns correctly.
ru is never initialized? It's supposed to be "the next row", why not simply use "ra+1" then instead of ru? Also, TRNRow and RTRNRow are never initialized either.
When you "search" your records, you actually only copy the same row into your results, then "copy next row until employee number is wrong". So this only works for exactly one employee, and even then you only catch the first few trainings. Use the Find function on the employee number cell in the records sheet to find the next row with that id, then copy the row's contents and find next.
I think if you get yourself aquainted with the Find function, you will easily finish this macro on your own. Here's a good guide: https://excelmacromastery.com/excel-vba-find
Good luck!

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.

Selecting last row, range, vba

I'm fairly new to vba and have a rather simple problem. Can someone please help:
Instead of selecting the specific cell, I want my vba macro to go to the bottom of the column of interest, skip, and two cells below it do the following:
Range("W72").Select
Selection.NumberFormat = "General"
ActiveCell.FormulaR1C1 = "Null_value"
Range("X72").Select
Selection.NumberFormat = "General"
ActiveCell.FormulaR1C1 = "=R[-2]C[1]-SUM(R[-2]C[-8]:R[-2]C[-6])"
As you can see the code above refers to the specific cells W72 and X72. Currently, the last entry in these columns are in W70 and X70 but next month my dataset will get bigger so W72 and X72 aren't the right locations to do the actions above. How do I correct for this such that my vba code is automatically going to the bottom of W(n):X(n), skips one row and in W(n+2), X(n+2) performs the code above.
Also, my formula above (ActiveCell.FormulaR1C1) also is referring to specific cells, in my case Row 70 several columns to the left, but as you probably tell, this too has the same issue since the row referencing changes each month. I need to get my vba to have the formula pick up the last row of those columns, the columns are P,Q,R.
Thanks for any help you can provide.
Update: Part of my same working project, I would greatly appreciate if anyone can help with this too. Thank you:
Hi All,
I currently have an input box for a variable that changes everymonth:
r_mo = Application.InputBox(prompt:="Enter the reporting month as YYYYMM (Eg:201604). Errors in this entry will result in errors in the results.")
This prompts an input box which one has to manually enter into... However, I want to automate this process and eliminate the need for an input box. Isn't there a now function in vba that will automatically generate today's date.
From a now, or system function all I want to do is extract the year in four digits and the month in two digits.
So for example, if we're in decemeber 2016
Sub asasdas ()
"Now function"
r_mo = YYYYMM ' automatically updated from "now function"
End Sub
I appreciate any help you can give me and thank you so much all.
You can get the last populated row of a given column (W in my example) in VBA with the following code:
Dim ws As Worksheet : Set ws = ThisWorkbook.Worksheets("MySheetNameHere")
lastRow = ws.Cells(ws.Rows.Count, "W").End(xlUp).Row
Naturally, if you then add 2 to lastRow you have the cell you are looking for.
I'd do it like
Sub asdf()
Range("w1048576").End(xlUp).Offset(2, 0).Select 'gets the last row
With Selection
.NumberFormat = "General"
.FormulaR1C1 = "Null_value"
End With
ActiveCell.Offset(, 1).Select
With Selection
.NumberFormat = "General"
.FormulaR1C1 = "=R[-2]C[1]-SUM(R[-2]C[-8]:R[-2]C[-6])"
End With
End Sub
If you want a more detailed answer you're going to have to make a new question but for your second question try this.
Sub Now()
Dim myDate As String
myDate = Date
myDate = Format(myDate, "yyyymm")
Debug.Print myDate '201606 output for June 10th 2016
End Sub

sumif code returns error when using column number?

I have a quick question... not sure what I am doing wrong.
I would like to have a named range (single cell) updated with the value from a sumif based on data in another tab of excel. the formula should go through column 2 look for the date and sum any values in column 10.
even when I substitute out the columns with actual hard column letters, I am getting error 1004 method range of object worksheet failed. how can I re-code this to pick up sumif data from another tab?
here is my code
with data_ws
date = #5/13/2014#
[named_range] = worksheetfunction.sumif(.range(.columns(2)), date, _
.range(.columns(10))
end with
You are close and may have an idea from below why yours are not working:
Sub TestSumIf()
Dim oRngA As Range, oRngB As Range
With ActiveCell
.ClearContents
Set oRngA = .Columns(2).EntireColumn ' 1st EntireColumn on the right
Set oRngB = .Columns(3).EntireColumn ' 2nd EntireColumn on the right
.Value = WorksheetFunction.SumIf(oRngA, Date, oRngB)
End With
End Sub
Sample: