Is there any way to make the time stop counting up in excel? - excel

In my excel sheet I have the following code:
=IF(ISERROR(MATCH(D2,'Sheet 2'!A:A,0)),"",NOW())
This basically checks to see if the value in D2 matches any values in column A:A in sheet 2, and then populates the cell with a date and time with NOW().
My problem is the date and time is counting up because I am using the NOW() function whereas what I need is the date to, in a way, take a snapshot of the date or freeze the date. This table I am creating is acting like a log so I need the date to stay as it is when it is put into the cell.
Any help with this is much appreciated.

You could have this automatically run, if you paste it in the code behind your sheet (Where the range theCells is the column where the timestamps are going):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("theCells")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
If Range(Target.Address).Value <> "" Then
Range(Target.Address).Copy
Range(Target.Address).PasteSpecial xlPasteVaues
End If
End Sub

Ok so I solved it, In my VBA I have the following code which pretty much creates a log file when my button is clicked, it takes various information in different cells and populates it in the next defined available row:
Sub copylog()
Dim LastRow As Long, ws As Worksheet
Dim wt As Worksheet
Set ws = Sheets("Create Log")
Set wt = Sheets("PDF Creation")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1
ws.Range("A" & LastRow).Value = wt.Range("N11").Value
ws.Range("B" & LastRow).Value = wt.Range("N12").Value
ws.Range("C" & LastRow).Value = wt.Range("N13").Value
ws.Range("D" & LastRow).Value = wt.Range("N14").Value
ws.Range("E" & LastRow).Value = wt.Range("N15").Value
ws.Range("F" & LastRow).Value = wt.Range("N16").Value
ws.Range("G" & LastRow).Value = wt.Range("AT19").Value
ws.Range("H" & LastRow).Value = wt.Range("AT21").Value
ws.Range("I" & LastRow).Value = wt.Range("AT23").Value
ws.Range("J" & LastRow).Value = wt.Range("AT25").Value
ws.Range("K" & LastRow).Value = wt.Range("AT27").Value
ws.Range("L" & LastRow).Value = wt.Range("A2").Value
ws.Range("M" & LastRow).Value = Environ("Username")
End Sub
The code above will populate a table in the next available row, for example, the first line:
ws.Range("A" & LastRow).Value = wt.Range("N11").Value
This will take the value that populates N11 in the PDF create sheet and populate the next available row in column A in the create log sheet (using copy and paste).
The line that has fixed my time problem is the line:
ws.Range("L" & LastRow).Value = wt.Range("A2").Value
In cell A2, I have the NOW() function and the button copies and pastes it into the next available space in column L (copies and pastes as TEXT).

Related

Loop through range in one sheet and have custom formula for each cell. Put data into a new sheet [excel][vba][bloomberg]

I'm not sure how descriptive my title is but below I will try to explain what I am trying to do.
I have a list of company Bloomberg tickers => maybe more than 100
This list of tickers is saved in say "Sheet1" "A:A"
For each ticker I have a Bloomberg (BDS) formula that returns a certain number of shareholders
The number of shareholders needs to be dynamic
The ticker of each company needs to be copied alongside the info gathered from BBG (as this is not provided by their formula)
This whole data should be in a new sheet let's say "Sheet2"
Below is the code I am using. It actually does what I need, however I am not able to make the output of my FOR loop to be in a new sheet. I also believe that the writing is not the most efficient so any help there would be great.
My current excel spreadsheet:
How it looks now
Sub Macro1()
'
' Macro1 Macro
'
Dim ticker As Range
Dim cell As Range
Dim start_row As Integer
Dim row As Integer
Dim top_investors As Integer
top_investors = 5
start_row = 2
Range("K2:U999999").ClearContents
Range("L" & start_row).Select
For Each ticker In Range("A2:A" & Cells(Rows.Count, 2).End(xlUp).row)
If Not IsEmpty(ticker) Then
ActiveCell.Formula = "=BDS(" & Chr(34) & ticker.Value & Chr(34) & ",""TOP_20_HOLDERS_PUBLIC_FILINGS"",""Endrow""," & Chr(34) & top_investors & Chr(34) & ",""Endcol"",""9"")"
For Each cell In Range("K" & start_row & ":" & "K" & start_row + top_investors)
row = start_row
Range("K" & row).Select
cell.Value = ticker.Value
row = row + 1
Next cell
start_row = start_row + top_investors
Range("L" & start_row).Select
End If
Next ticker
End Sub
Well, I guess you need to do something like:
Sub Macro1()
'
' Macro1 Macro
'
Dim ticker As Range
Dim cell As Range
Dim start_row As Integer
Dim row As Integer
Dim top_investors As Integer
Dim mySheet as Worksheet
top_investors = 5
start_row = 2
Sheets.Add.Name = "myNewSheet"
mySheet = ThisWorkbook.Sheets("myNewSheet")
Range("K2:U999999").ClearContents
Range("L" & start_row).Select
For Each ticker In Range("A2:A" & Cells(Rows.Count, 2).End(xlUp).row)
If Not IsEmpty(ticker) Then
mySheet.Range("define your Range").Formula = "=BDS(" & Chr(34) & ticker.Value & Chr(34) & ",""TOP_20_HOLDERS_PUBLIC_FILINGS"",""Endrow""," & Chr(34) & top_investors & Chr(34) & ",""Endcol"",""9"")"
For Each cell In (mySheet?.)Range("K" & start_row & ":" & "K" & start_row + top_investors)
row = start_row
mySheet.Range("K" & row).Select
cell.Value = ticker.Value
row = row + 1
Next cell
start_row = start_row + top_investors
Range("L" & start_row).Select
End If
Next ticker
End Sub
That won't work yet, but I guess you got the idea of working with "mySheet" and know better than me were to add it in your code. Maybe you need to google a little bit about the correct syntax.
If you have a recent Excel version (that has the LET spreadsheet function) then VBA is not strictly necessary for what you want to achieve.
For example, if you tickers are in the range A4:A7 on 'Sheet 1', you can collect all the data in 'Sheet 2':
The formula in Sheet2!A4: =Sheet1!A4
The formula in Sheet2!B4: =LET(data,BDS(A4,$B$2,"Array=TRUE"),s,SEQUENCE(1,$B$1),INDEX(data,s,1))
Then fill the formulae down as needed. The dynamic number of investors is in cell Sheet2!B1, but this can be hard-coded.
The main 'trick' is to use the "Array=TRUE" option in the BDS() call. This returns the data in a dynamic array, which can be indexed into.

How to use variables in a VBA code using the sumif function

I download a data set that always has a different number of rows. I store two columns as variables, the imports and the months. Then I need to run a Sumif formula that sums the value of imports by the months. I am writing a Sumif formula that uses the two variables and references the cell to its left.
The cells however vary in location based on the changing size of the data set. So I write a code to select the last active cell on a column and select the cell 3 rows down.
When writing the formula with the variables and the cell its giving me an error. Please help sorry for any typos fist time doing this.
I select all the active cells in range D and store them as months, I do the same for the imports. Then using range I find the last active cell on column M, and use select the cell 3 rows down, where I wish to write my formula.
Please see my codes to see what am I doing wrong, I am a novice coder.
Sub Importaciones()
'
' Importaciones Macro
'
Dim LastRow As Long
LastRow = Range("L" & Rows.Count).End(xlUp).Row
Dim Months As Long
Months = Range("D2", Range("D2").End(xlDown)).Select
Dim Imports As Long
Imports = Range("M2", Range("M2").End(xlDown)).Select
Dim LastRowM As Long
LastRowM = Range("M" & Rows.Count).End(xlUp).Row
Range("M" & LastRowM + 3).Formula = "=sumif(" & Months & ", " &
Range("L" & LastRow + 3) & ", " & Imports & ")"
End Sub
For the formula to work and the sum of the month that I choose comes up
As per all the comments:
Sub Importaciones()
With Worksheets("Sheet1") 'Change to your sheet
Dim LastRow As Long
LastRow = .Range("L" & .Rows.Count).End(xlUp).Row
Dim Months As Range
Set Months = .Range("D2", .Range("D2").End(xlDown))
Dim Imports As Range
Set Imports = .Range("M2", .Range("M2").End(xlDown))
Dim LastRowM As Long
LastRowM = .Range("M" & .Rows.Count).End(xlUp).Row
.Range("M" & LastRowM + 3).Formula = "=sumif(" & Months.Address(0, 0) & ", " & .Range("L" & LastRow + 3).Address(0, 0) & ", " & Imports.Address(0, 0) & ")"
End With
End Sub

Hide rows in Excel based off multiple column value

How can I hide rows based off multiple column values? Example: If the "Projects", "Team Member", "Priority", & "Status" fields are all blank, then the row will hide itself.
I saw your other post, and I don't really think this is the way you should go about building your dashboard. You are essentially creating a copy of your other sheet. It seems like an Advance Filter would be better suited here.
If you are set on your current method, this will determine lowest used cell in your columns, and hide rows above that cell based on your criteria. I would add a command button named something like "Refresh My Dash" and link it to this macro.
Option Explicit
Sub HideRow()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim LRowC, LRowD, LRowF, LRowH, LRow As Long
LRowC = ws.Range("C" & ws.Rows.Count).End(xlUp).Row
LRowD = ws.Range("D" & ws.Rows.Count).End(xlUp).Row
LRowF = ws.Range("F" & ws.Rows.Count).End(xlUp).Row
LRowH = ws.Range("H" & ws.Rows.Count).End(xlUp).Row
LRow = Application.WorksheetFunction.Max(LRowC, LRowD, LRowF, LRowH)
Dim i As Long
Application.ScreenUpdating = False
ws.Rows.Hidden = False
For i = LRow To 2 Step -1
If ws.Range("C" & i).Text = "" And ws.Range("D" & i).Text = "" And ws.Range("F" & i).Text = "" And ws.Range("H" & i).Text = "" Then
ws.Rows(i).EntireRow.Hidden = True
End If
Next i
Application.ScreenUpdating = True
End Sub

Copy entire row by checking condition and paste into different ranges of cells aside

I am using below code to copy 4 columns of data from range A:D till the end of that row by checking a condition on B column whether its primary or backup. If primary category code will paste the data into columns K:N, if backup category paste the entire row to P:S columns.
But when executing I am getting Object Defined Error. Can anybody help with whats wrong in this code ? Thanks
Public Sub CopyData()
Dim rngSinglecell As Range
Dim rngQuantityCells As Range
Dim intCount As Integer
Range("K2:S1400").Clear
Set rngQuantityCells = Range("B120", Range("B120").End(xlDown))
For Each rngSinglecell In rngQuantityCells
If rngSinglecell.Value = "Primary" Then
Range("K" & Rows.Count).End(xlUp).Offset(1).Resize(rngSinglecell.Value, 14).Value = _
Range(Range("A" & rngSinglecell.Row), Range("D" & rngSinglecell.Row)).Value
ElseIf rngSinglecell.Value = "Backup" Then
Range("P" & Rows.Count).End(xlUp).Offset(1).Resize(rngSinglecell.Value, 19).Value = _
Range(Range("A" & rngSinglecell.Row), Range("D" & rngSinglecell.Row)).Value
End If
Next
End Sub
Try with Copy :
Public Sub CopyData()
Dim rngSinglecell As Range
Dim rngQuantityCells As Range
Dim intCount As Integer
Range("K2:S1400").Clear
Set rngQuantityCells = Range("B120", Range("B120").End(xlDown))
For Each rngSinglecell In rngQuantityCells
If rngSinglecell.Value = "Primary" Then
Range(Range("A" & rngSinglecell.Row), Range("D" & rngSinglecell.Row)).Copy Range("K" & Rows.Count).End(xlUp).Offset(1)
ElseIf rngSinglecell.Value = "Backup" Then
Range(Range("A" & rngSinglecell.Row), Range("D" & rngSinglecell.Row)).Copy Range("P" & Rows.Count).End(xlUp).Offset(1)
End If
Next
End Sub
No need to resize.
Well, among other things, you're using rngSingleCell.value in two completely different ways, and one of them isn't working.
rngSingleCell is the cell that contains Primary/Backup, correct? But you're trying to use that value in a resize method, which expects a numeric value. This is definitely going to throw you off.
Of course, without seeing the actual layout of your data it's hard to be sure, but it's not clear why you're using a method a method like Range().end(xlup).offset(1) to define your copying range; it's going to keep on copying the same data that way.

Excel consolidate same values on column1 while keeping data

I have a excel table which looks like this one.
As you can see, this products 2005 and 3004 are listed more than once because this same products have different price value based on sale scala. Like, if you buy 50 each of that item, the price gets lower.
What I want is -assuming there is a way to achieve- is this :
ProductID Column is Unique and entrys with same product ID gathers on 1 row. With their Sale Scala and Price(Per) values added(if there is) within next columns. Is there a solution to alter the table like this with coding ? There is too much line to manually edit.
Please Suggest. Thanks.
There are probably better ways to do this but I've created a simple loop that looks for duplicate values in column A then processes the data accordingly. I'm working bottom up for simplicity. Note: You'll have to manually create your headers when finished unless you want to add more logic.
EDIT Simpler code
Sub TransposeData()
Dim WS As Worksheet
Dim LastCell As Range
Dim LastCellRowNumber As Long
Set WS = Worksheets("Sheet1")
With WS
Set LastCell = .Cells(.Rows.Count, "A").End(xlUp)
LastCellRowNumber = LastCell.Row
End With
'Loop through column A bottom up
For i = LastCellRowNumber To 2 Step -1
' Check if value in current cell matches value in cell above it
If (Range("A" & i).Value = Range("A" & i).Offset(-1, 0).Value) Then
'Shift current values over
Range("C" & i & ":D" & i).Insert Shift:=xlToRight
'Copy new values down
Range("C" & i).Value = Range("C" & i - 1).Value
Range("D" & i).Value = Range("D" & i - 1).Value
'Delete row
Rows(i - 1).Delete Shift:=xlUp
End If
Next i
End Sub
Results:

Resources