vba excel - Issues with multiple time formats [closed] - vba

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Files here
Hello,
I am trying to write code to format a report. The main issue with the report is that some cells store multiple pieces of data in a cell when each row should only have one piece of data in each cell. My solution thus far has been to use the text to columns function for the affected cells, paste all other data transposed, and finally copy and re-transpose the new data over the original report area. This has worked so far, but I am running into an issue with cells that store time. Cells with single times (such as 13:00) are in the custom format hh:mm, which vba converts to a a decimal number. Cells with multiple times (such as 11:009:008:0010:30) are viewed as a string.
The code I am currently using to split the times works for multiple time cells because it searches the string for ":", but vba does not detect the ":" in the custom format cell 13:00 since internally it sees that cells value as something like 0.56412.
I am kind of at a loss as to what to do here. I can't change the format that the report arrive to me in. My thought was that maybe I could find a way to turn the custom format "13:00" into a string "13:00" instead of 0.56412
I have attached the code file and the truncated dummy report I am testing to code on. I would like to thank everyone who responds for their help in advance!

to change a single time to a string, you can use format and identify the value to excel as a string using '
example:
'immediate window
[a1]="'"&format([a1],"hh:nn")
'or, using a range
range("A1")="'"&format(range("A1"),"hh:nn")
you can use isnumeric to check if it's a single time. True means it's a time, False means it's a string of times

Related

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

Automatic excel cell fill up from another sheet [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Here's the situation:
I have two excel sheets.
The first sheet contains a table of product codes and product descriptions (Two columns A and B).
I have a second sheet where someone is supposed to enter the product code and automatically have the next cell fill up with the product description and the cell next to it with the time.
I was wondering if that's do-able without VBA? If so, can someone give me start.
Best,
You could have the first cell that you want to auto fill have an IF statement, where if the cell has no value, nothing happens, and anything other than that gets a calculation.
Using A2:B100 as a Range if you have a header Row. Adjust to your own needs, of course.
IF FUNCTION
'IF (condition, result if true, result if false)'
VLOOKUP FUNCTION
'VLOOKUP(lookup_value,table_array,col_index_num,range_lookup)'
=If($A1 = "", "", VLOOKUP(A1,Sheet1!$A$2:$B$100,2))
The next cell over would have something similar:
=IF($A1 = "", "", NOW())
This will get you the time. You will also have to have the cell format set to Time.
There is a problem with that. The screenshot below illustrates it.
It will just keep refreshing with the current time over and over. I would use a bit of VBA to solve that by setting the value property instead of a formula.
Sheets("Sheet2").Cells(row,col).Value = Now()
You could copy and paste the value into another cell. Just the value. Not the formula.
Or you could check out this article: about generating Time Stamps.
edit: included VBA solution

Convert text cells to DATE, but leave empty and DATE cells intact [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
sorry for my ignorance I have no idea about spreadsheets.
What I have is a column that contains dates but they are there in different format
some are text like '19.12.2006 and others are cells that are formated as a date like 2009-10-10
I want them all to be date cells, so I found a formula =DATEVALUE
What I done is I put =IF(A2="";"";DATEVALUE(A2))
For the dates in text format it done well, for empty cells it's ok as well, but when the cell is a date cell I have Err: 502
Can anyone please help me?
As you've discovered, DATEVALUE expects a string that looks like a date, not an actual date. However, there is nothing stopping you from taking your dates and converting them to strings with TEXT. The right format mask can distinguish between dates and string dates for you.
        
The formula in N1 is,
=IF(ISBLANK(M1); ""; DATEVALUE(TEXT(M1; "mmm dd, yyyy;#")))
This formats anything that is an actual date into a string date and leaves any string dates alone. There seemed to be a dd/mm/yy vs mm/dd/yy issue between your sample data and my regoinal system so I used that complex date format but just about any date format will do. The # is stuck on the end to catch strings. The result is processed by DATEVALUE. I used ISBLANK for the second test but there is nothing wrong with IF(M1=""; ....
I'm not a user of OpenOffice Calc, but in Excel and Google Sheets there is a formula called =IFERROR() which can be used in this scenario - your formula in N7 would be:
=IFERROR(IF(M7="","",DATEVALUE(M7)),M7)
Otherwise, you could use IF() with ISERROR() as follows:
=IF(ISERROR(DATEVALUE(M7)),M7,IF(M7="","",DATEVALUE(M7)))
Hope that helps!
Mike

search and highlight datas in vba [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm a beginner in VBA, I've been searching in the internet that could possibly help me get any ideas about my problem.But I can't find any result that is closely related to what I've been searching for, so I came here. I can't wrote any macro about this because I don't even know where to start.I have an excel as my database containing an ID number and a last name of a person. I want to create a macro that has an input box.If the value/text is found anywhere in the workbook the cell is highlighted in yellow. Any suggestions will be appreciated.
Thanks in advance.
If you can do without the input box and just enter your search text into a cell instead, I have a solution that involves no macros.
Just use conditional formatting instead. If the first cell of your data was say B2, highlight that cell and select conditional formatting from the ribbon menu, then New Rule followed by Use formula. In the format values enter the following:
=NOT(ISERROR(FIND($D$1,B2,1)))
Where B2 is you highlighted data cell and $D$1 is the cell where you are entering your search text. You can change the search cell location but must have the $$s.
Next hit the format button and change the fill color to yellow, then click OK.
All you then need to do is copy the format from the first data cell to the rest of the column.

Removing invisible question mark from text - #​E using vba

I have to read the text from the cells of a column in excel and search for it in another sheet.
say for example, the text in sheet1 column A is "Evoked Potential Amplitude N2 - P2." This has to be searched in sheet2 column C. This fails because a question mark appears before the "E" which is not present in the value in the sheet2.
Both are representation of same character in different application. Maybe someone might recognize it.
In the excel sheet I don't see any junk characters, but while handling it in the vb code I see a question mark before the word - Evoke.
This data was extracted from a share point application and this character (?) is not visible to the plain eye. Search and replace functions are not working in this case.
Unicode 8203 is a zero-width space. I'm not sure where it's coming from. It is probably a flaw in the way the data is imported into Excel which you haven't noticed before, but it might be worth fixing.
In the meantime, you can simply use the Mid() function in Excel VBA to remove the unwanted character. For example instead of
x = cells(1,1).value
use
x = Mid(cells(1,1).value,2)
which deletes the first character.