Copy Value Of Cell, Not Formula - vba

I am trying to grab only the current number of minutes in an hour.
I use the NOW() function and I have the format of the cell it's in set to only mm. So in the cell display, if it's 9:08, I get 08.
Whenever I try a excel formula like =VALUE(D5) or =D5, I get (I'm assuming this is what it is) an unformatted date string.
I am now looking to macros using VBA to solve my problem.
I've tried:
Worksheets("Sheet1").Cells(23, 1) = Worksheets("Sheet1").Range("E20").Value
However I get the same thing as with my excel formulas. Is there any VBA way of getting only the value that's displayed in the cell and copied to a new area. It doesn't really need to be copied over, I just need to use the value further in my macro.
I'm open to other ways of getting the current number of minutes in the current hour as well.

Do try not to confuse formatting and data.
why not use =MINUTE(D5) in a cell which will set that cell to the minute value irrespective of formatting? Use that cell as your source in your VBA.
=HOUR(D5) would operate similarly but return the hour part.

If the displays 9:08
then select that cell and:
Sub dural()
Dim s As String
s = Split(ActiveCell.Text, ":")(1)
MsgBox s
End Sub
Note this is a String

Might be calculated like so:
=60*(NOW()*24-INT(NOW()*24))

Related

Get value from cell without transform in vba macros

I have an issue with vba macros. Cell contains value 941144280284022000000. But when try to get this value in macros, variable is equal
9.41144280284022E+20. Is it possible to get "real" value?
Thanks.
Here is an example:
Sub dural()
MsgBox ActiveCell.Text
End Sub
This will insure that long strings of numerals are not converted to numbers.
EDIT#1:
This assumes that the cell actually displays the long string of numerals. If the cell displays ####, that is what the sub will pick-up. If the cell displays 9.41E+20, then that is what my sub will pick-up.
My sub will not necessarily pick-up the contents of the Formula Bar.
Try a simple :
Dim myVar as Variant
myVar = CDec(Range("A1").Value)
If you wish to store a value that looks like a number (or is a number) but has more than 15 digits of precision, then you must either format the cell as text before entering the value, or prepend the value with a single apostrophe to indicate to Excel that the value is to be treated as text and not a number.
If you don't do this, then as soon as the value is entered, there's a good chance it will be altered by Excel. The trailing zeros in the example value mean this does not happen in this specific case, but try changing that last 0 to 1 and you'll see what I mean.
You enter: 941144280284022000001
Excel converts this to: 941144280284022000000
More reading: https://en.wikipedia.org/wiki/Numeric_precision_in_Microsoft_Excel

Excel macro, dd.mm date +1

First time here, sorry if I'm doing something wrong
I just found out about excel macros and I think they will help me a lot, but I'm not really into programming and all this stuff
I did a few macros and it was really easy, but this one is not, at least not for me, I hope you can help me
I already tried a lot of stuff like NumberFormat and Format(...) but it didn't work how I wanted it to work
What I want is:
I get excel documents with a certain date in cell [F6] (formated dd.mm) like this: "01.08" , it's always 01 (first day of the month)
now I want a macro, that will take that date, and everytime I use the macro I want it to paste the next day in the cell I have currently selected (ActiveCell)
so when I use my macro for the first time in that document, I want it to paste "02.08" into the current cell I have selected
the next time I use it "03.08" etc..
I hope you understand me and can help me
Try this. Note that i will be reset to 0 when you close the workbook. If you want to store i between sessions you have to write it to a cell and then reference that cell.
Cell F6 has to be properly formatted as Date.
Dim i As Long
Sub next_day()
i = i + 1
Selection = Range("F6").Value + i
Selection.NumberFormat = "dd.mm"
End Sub

Today function equivalent in VBA in combination with countifs

I am having some problem with using a countifs formula in Excel / VBA. I have got the formula working perfect in Excel but ideally I want to use this in VBA with my form. Here is the formula in Excel which works a treat:
=COUNTIFS(Sheet1!A:A,"Place",Sheet1!K:K,"<"&TODAY())
will count the names places that are now in the past
=COUNTIFS(Sheet1!A:A,"place",Sheet1!K:K,">"&TODAY())
will count the names places that are current
I have five different Places in column A and hundreds of different dates in column K. The above formulas work well in Excel and return the correct values. I have spent hours trying to get this to work in VBA with my userform but keep getting various errors. The first part is not the problem but as soon as I get to the &today function it falls apart. From what I can see the &today function is not available in VBA and the &Date seems to be the recommendation. I have tried this but still get no where. I'm missing a trick (or several) here and I would really like to get this working in VBA rather than using the current formulas in Excel. The returned results are then displayed in textboxes on my form.
All ideas and feedback much welcome!
Second edit
================================
Thanks for the quick replies! Here is the actual code I am playing about with in VBA
'Count events by area'
Dim ListLondon As Long
ListLondon = .CountIf(Range("a1:a1998"), "London"), ("Sheet1!K1:K1998"), "<" & Date)
End With
Me.TextBox1 = ListLondon
I know the second part of the count if is all wrong regards the date - that's how I've left it for now. I am really hoping to use the current layout and a working Date / Today code at the end. Please show me what I've done wrong here!
====
oops - can see a mistake already - but the initial problem remains around the date issue. I should of used countifs as using multiple criteria.
You have to read the values of the cells to your VBA code. I recommend you to use Excel.Range object to do that. It can interpret the range like the edit line of the Excel, something like
Dim foo as Excel.Range
set foo = yourworksheet.Range("A1:B3")
Read the Date type data into VBA Date type variable by iterating through the cells.
Examine relation between the read data and the current date. Current date can be read by using the DateTime.Now function.
Increment a variable based on a decision

Posting a date column in Excel VBA

I'm stuck on what should be a very simple question, which is frustrating me greatly.
Essentially, I'm sorting through many files and manipulating them, but am stuck on this: how do I add in a column with something like the date in every cell down to the end of where the other columns have data?
Say that there are 5 other columns with length of 20 each. What code would I use to make column 6 have a certain value with a length of 20? The length varies with each sheet I work on, and this has been the problem.
In a program like MATLAB, I could just denote this by putting something like F1:end, but finding the answer for Excel is driving me crazy!
Any help would be greatly appreciated.
You could programmatically do this in vb to detect the bottom row with a value in, though it sounds as though you might be after a more straight forwards working solution.
Type a value in the top row, and with the cell selected you should see a square in the bottom right of the cell, double click it to fill to the end of the used range.
In VBA you would typically look from the bottom up to find the last non-blank cell in a column. With this, you can get the .Row to concatenate into a Range object definition. Example:
with activesheet
.range("F1:F" & .cells(rows.count, 5).end(xlup).row) = Format(Now, "dd-mmm-yyyy hh:mm")
end with
This will fill column F from the first row to the cell in column F that is next to the last non-blank value in column E.
Sub Tester()
With ActiveSheet
.Range(.Range("E1"), _
.Cells(Rows.Count, "E").End(xlUp)).Offset(0, 1).Value = Date
End With
End Sub

Excel VBA: Get the right date into a cell using DatePicker

I have a UserForm with a DatePicker control in it.
It works fine, except when copying the selected date to the spreadsheet.
This is the code:
Range("A1").Value = UserForm1.DTPicker1.Value
Which returns:
00:00:00
In cell A1, no matter what date has been selected.
You cell formatting might be set to Time instead of Date.
Try changing that to see if it works.
Also, make formatting 'General' AFTER this step, to see if anything has been pasted in "A1".
You should need an intermediate variable to get it, like below:
t = Me.DTPicker1.Value
ws.Range("A1") = t
Make sure that the command to transfer the data is located in the same form or page of a multipage form as the DTPicker itself.
For some reason it wont work when the two are separated and will display a zero in the target cell. That zero is the "time" part of the date which has been switched off. Although the "Date" part refuses to transfer over, for some reason the time does, and so that zero time is read in the cell as time zero which is mid-day.
So in summary:
Keep the DTPicker and transfer control on the same page and that should solve the problem.
The code is pretty simple. It's
Sheet1.Range("AA9") = Me.DTPicker1.Value