Hey I am thinking about a solution for my problem for hours now. I've searched the web, too and was able to find some approaches but none of them does work for my problem.
I have one cell with data that appears like this:
(Monday#100%[00:00-24:00]);(Tuesday#100%[00:00-24:00]);(Wednesday#100%[00:00-24:00]);(Thursday#100%[00:00-24:00]);(Friday#100%[00:00-24:00]);(Saturday#100%[00:00-24:00]);(Sunday#100%[00:00-24:00])
The problem is, that this string is not consistent. It could also look like this:
(Monday#125%[00:00-04:00]);(Monday#90%[04:00-08:00]);(Monday#90%[08:00-12:00]);(Monday#115%[12:00-16:00]);(Monday#120%[16:00-20:00]);(Monday#115%[20:00-24:00]);(Tuesday#125%[00:00-04:00]);(Tuesday#90%[04:00-08:00]);(Tuesday#90%[08:00-12:00]);(Tuesday#115%[12:00-16:00]);(Tuesday#120%[16:00-20:00]);(Tuesday#115%[20:00-24:00]);(Wednesday#125%[00:00-04:00]);(Wednesday#90%[04:00-08:00]);(Wednesday#90%[08:00-12:00]);(Wednesday#115%[12:00-16:00]);(Wednesday#120%[16:00-20:00]);(Wednesday#115%[20:00-24:00]);(Thursday#125%[00:00-04:00]);(Thursday#90%[04:00-08:00]);(Thursday#90%[08:00-12:00]);(Thursday#115%[12:00-16:00]);(Thursday#120%[16:00-20:00]);(Thursday#115%[20:00-24:00]);(Friday#125%[00:00-04:00]);(Friday#90%[04:00-08:00]);(Friday#90%[08:00-12:00]);(Friday#115%[12:00-16:00]);(Friday#120%[16:00-20:00]);(Friday#115%[20:00-24:00]);(Saturday#125%[00:00-04:00]);(Saturday#90%[04:00-08:00]);(Saturday#90%[08:00-12:00]);(Saturday#115%[12:00-16:00]);(Saturday#120%[16:00-20:00]);(Saturday#115%[20:00-24:00]);(Sunday#125%[00:00-04:00]);(Sunday#90%[04:00-08:00]);(Sunday#90%[08:00-12:00]);(Sunday#115%[12:00-16:00]);(Sunday#120%[16:00-20:00]);(Sunday#115%[20:00-24:00])
The structure of the string is
You have a day of the week followed by an "#"
after the at (#) you have a number that can be between 0 and 999
after that you have a time of the day in brackets []
You can have up to 6 different time frames of one day (not more, but less)
each time frame cluster is separated by ; and within normal brackets ()
So this is my starting position. What I want is to extract the numbers between # and % and list them chronologically (Monday to Sunday, hours of the day).
I was able to extract the number between # and % for each day of the week with this formula
=MID(B3;SEARCH("Monday#";B3)+7;SEARCH("%";B3)-SEARCH("Monday#";B3)-7)
But this only works if each day of the week is mentioned only ones.
I am out of ideas of how I could get it to work when having more time frames per day. Appreciate any help.
Thanks,
Ramon
Try an set of iterative searches, where the next search starts at the position found for the previous result. ie:
in cell B6 put =SEARCH("]",$B$3,B5+1)
in cell C6 put =MID($B$3,SEARCH("(",$B$3,B5+1)+1,SEARCH("#",$B$3,B5+1)-SEARCH("(",$B$3,B5+1)-1)
in cell D6 put =MID($B$3,SEARCH("[",$B$3,B5+1)+1,SEARCH("]",$B$3,B5+1)-SEARCH("[",$B$3,B5+1)-1)
in cell E6 put =MID($B$3,SEARCH("#",$B$3,B5+1)+1,SEARCH("%",$B$3,B5+1)-SEARCH("#",$B$3,B5+1)-1)
(Note that B5 needs to be empty (or 0) for this to start correctly)
You can then fill down as far as needed to pick up each term. If they are out of order, then you can order on column D then C (the time then the day).
This might get you started.
Sub Extractor()
Dim data() As String, i As Integer, rw As Integer
rw = 3
data = Split(Range("A1"), ";")
For i = 0 To UBound(data)
Range("A" & rw) = VBA.Mid$(data(i), 2, InStr(1, data(i), "#") - 2) 'Day
Range("B" & rw) = VBA.Mid$(data(i), InStr(1, data(i), "#") + 1, InStr(1, data(i), "%") - InStr(1, data(i), "#") - 1) '% number
Range("C" & rw) = VBA.Mid$(data(i), InStr(1, data(i), "[") + 1, InStr(1, data(i), "]") - InStr(1, data(i), "[") - 1) 'Time
rw = rw + 1
Next i
End Sub
Notes:
Assumes your string is in A1
Prints your data in columns A, B, and C starting in row 3
You can use Text-to-Columns feature of excel, with ) as a delimiter. Than transpose the columns into rows to get something like this:
(Monday#125%[00:00-04:00]
;(Monday#90%[04:00-08:00]
;(Monday#90%[08:00-12:00]
;(Monday#115%[12:00-16:00]
;(Monday#120%[16:00-20:00]
...
;(Sunday#115%[20:00-24:00]
Then apply formulas.
Edit:
Using just Find and Mid you can achieve this:
The idea behind find and mid is always the same.
Ex. for finding ( is =FIND("(";[Text]).
Ex. for extracting Day is =MID([Text];[#[Pos(]]+1;[#[Pos'#]]-[#[Pos(]]-1)
Related
I have a list of numbers (that will be changed weekly) and I also have a list of ranges (that also change weekly). I need to check whether each number falls between each range.
Eg. My list of numbers on the left and my list of ranges on the right.
4 1 3
10 67 99
54 120 122
155
So what I need is to return a value if 4 falls between 1-3, then check if it falls between 67-99 and so on. Then return a value if 10 falls between 1-3 or 67-99 etc.
I have tried array and vba but I'm noob and I cant find much in the way of examples for this issue. I have had success with the following nested if;
=IF(OR(AND(G2>$L$2,G2<$M$2),AND(G2>$L$3,G2<$M$3),AND(G2>$L$4,G2<$M$4),G2,"")
=IF(OR(AND(G3>$L$2,G3<$M$2),AND(G3>$L$3,G3<$M$3),AND(G3>$L$4,G3<$M$4),G3,"")
However, once my number of ranges gets above a certain number it says i have too many characters.
Any help would be appreciated.
Regards,
Will.
So what I need is to return a value if 4 falls between 1-3, then check if it falls between 67-99 and so on. Then return a value if 10 falls between 1-3 or 67-99 etc.
If you want to match each value in col G with the list in L and M then, rearrange the G column values in a row as shown below so that you can get the entire view in one go.
Put this formula =(AND($N$1>L2,$N$1<M2)) in N2 and drag it down. Similarly put the formula =(AND($O$1>L2,$O$1<M2)) in O2 and pull it down and so on...
Thanks Siddarth. This works but the number of rows will be changing weekly and your method would be too tedious to do weekly.
Ended up working it out on my own. Pretty simple but took me forever!
Sub Subtract_Start()
Set rng1 = Range(Range("G2"), Range("G2").End(xlDown))
Set rng2 = Range(Range("L2"), Range("L2").End(xlDown))
For i = 2 To rng1.Rows.count
For j = 2 To rng2.Rows.count
If Cells(i, "G").Value > Cells(j, "L").Value Then
If Cells(i, "G").Value < Cells(j, "M").Value Then
Cells(i, "G").Copy Cells(i, "J")
End If
End If
Next j
Next i
End Sub
I have a text type column in excel with these values
2/02/1472
22/88/1234
1/8/1234
22/88/12
01/01/222
88/2222
I want to set a mask that my values do look like this
02/02/1472
22/88/1234
01/08/1234
22/88/1200
01/01/2220
00/88/2222
My mask is 00/00/0000 (if a part does not exist, fill with zero)
I use this "=text(A1,"00/00/0000")" but have error
Since you also mention vba in your tags, here is a User Defined Function:
Option Explicit
Function FormatMask(S As String) As String
Dim V
Dim I As Long
V = Split(S, "/")
V(UBound(V)) = Format(V(UBound(V)), "0000")
For I = UBound(V) - 1 To 0 Step -1
V(I) = Format(V(I), "00")
Next I
FormatMask = Right("00/00/" & Join(V, "/"), 10)
End Function
EDIT
#pnuts pointed out that your examples show that the first two groups are left-padded with 0's, but the third group is right-padded with zero's.
The following modification accomplishes that:
Option Explicit
Function FormatMask(S As String) As String
Dim V
Dim I As Long
V = Split(S, "/")
'This pads with 0's on the left
'V(UBound(V)) = Format(V(UBound(V)), "0000")
'For padding on right as you show for the last group only:
V(UBound(V)) = Left(V(UBound(V)) & "0000", 4)
For I = UBound(V) - 1 To 0 Step -1
V(I) = Format(V(I), "00")
Next I
FormatMask = Right("00/00/" & Join(V, "/"), 10)
End Function
Another example why spreadsheet software is not well suited to text processing, but Excel can manage with a (horrible) formula:
=IF(LEN(IF(LEN(A1)-LEN(SUBSTITUTE(A1,"/",""))=1,"00/"&A1,IF(LEFT(RIGHT(A1,3))="/",A1&"00",IF(LEFT(RIGHT(A1,4))="/",A1&"0",IF(MID(A1,2,1)="/","0"&A1,A1)))))=10,IF(LEN(A1)-LEN(SUBSTITUTE(A1,"/",""))=1,"00/"&A1,IF(LEFT(RIGHT(A1,3))="/",A1&"00",IF(LEFT(RIGHT(A1,4))="/",A1&"0",IF(MID(A1,2,1)="/","0"&A1,A1)))),SUBSTITUTE(IF(LEN(A1)-LEN(SUBSTITUTE(A1,"/",""))=1,"00/"&A1,IF(LEFT(RIGHT(A1,3))="/",A1&"00",IF(LEFT(RIGHT(A1,4))="/",A1&"0",IF(MID(A1,2,1)="/","0"&A1,A1)))),"/","/0",1))
Another horrible formula for you (I've broken it down so I'm using a few helper columns which you can hide so visually it looks the same). Part of the horribleness is also due to your source data not having a consistent format
In Column A I've got your original list which is stored as text (Excel won't recognise these as a date or number)
In Column B to get the first part I have the formula
=IF(LEN(A2)-LEN(SUBSTITUTE(A2,"/",""))=2, VALUE(LEFT(A2,FIND("/",A2)-1)),0)
In Column C to get the middle bit I have
=LEFT(RIGHT(A2,IF(LEN(A2)-LEN(SUBSTITUTE(A2,"/",""))=2,LEN(A2)-FIND("/",A2),LEN(A2))),FIND("/",RIGHT(A2,IF(LEN(A2)-LEN(SUBSTITUTE(A2,"/",""))=2,LEN(A2)-FIND("/",A2),LEN(A2))))-1)
and then in Column D I get the last bit using:
=RIGHT(A2,LEN(A2)-FIND("/",A2,IF(LEN(A2)-LEN(SUBSTITUTE(A2, "/", ""))=2,FIND("/",A2)+1,1)))
I then put it all together in Column E and format it using
=TEXT(B2,"00")&"/"&TEXT(C2, "00") &"/"&TEXT(D2,"0")&REPT(0,4-LEN(D2))
To get your output
You could of course combine this all into one formula, I've just broken it down for a little bit of clearness (although is still pretty bleak):
=TEXT(IF(LEN(A2)-LEN(SUBSTITUTE(A2,"/",""))=2, VALUE(LEFT(A2,FIND("/",A2)-1)),0),"00")&"/"&TEXT(LEFT(RIGHT(A2,IF(LEN(A2)-LEN(SUBSTITUTE(A2,"/",""))=2,LEN(A2)-FIND("/",A2),LEN(A2))),FIND("/",RIGHT(A2,IF(LEN(A2)-LEN(SUBSTITUTE(A2,"/",""))=2,LEN(A2)-FIND("/",A2),LEN(A2))))-1), "00") &"/"&TEXT(RIGHT(A2,LEN(A2)-FIND("/",A2,IF(LEN(A2)-LEN(SUBSTITUTE(A2, "/", ""))=2,FIND("/",A2)+1,1))),"0")&REPT(0,4-LEN(RIGHT(A2,LEN(A2)-FIND("/",A2,IF(LEN(A2)-LEN(SUBSTITUTE(A2, "/", ""))=2,FIND("/",A2)+1,1)))))
A slightly shorter version:
=IF(LEN(A2)-LEN(SUBSTITUTE(A2,"/",""))=1,"00",TEXT(LEFT(A2,FIND("/",A2)-1),"00"))&"/"&TEXT(IFERROR(MID(A2,FIND("/",A2)+1,LOOKUP(99^99,FIND("/",A2,ROW($1:$20)))-FIND("/",A2)-1),TEXT(LEFT(A2,FIND("/",A2)-1),"00")),"00")&"/"&LEFT(RIGHT(A2,LEN(A2)-LOOKUP(99^99,FIND("/",A2,ROW($1:$20))))*10000,4)
The only thing that you want to know is this:
LOOKUP(99^99,FIND("/",A2,ROW($1:$20)))
This is the function to find the last occurrence of /. I assume the maximum length of the string is 20 so you can replace that if needed.
I have 1 column with about 60 cells with values or different length. Each (or at least most) of the values have a numeric characters in the value. I want to split the columns cells into more columns which I normally would do with the 'tekst to columns' function of excel.
But this function does not have an advanced option of splitting the value at the first numeric character. splitting based on spaces, comma etc. is possible but this does not help me.
Is there any way to divide the cells into 2 columns at the first number in the cell value?
I have looked at numerous other questions but none of them (or other internet fora) have helped me to split the value at the first number of the cell value.
Thanks #quantum285 for the answer. This routine works if the string contains one number. I changed the teststring to firstpart323secondpart.
then part1 returns 'firstpart32' and part2 return secondpart.
I tried to understand what happens in this code, please correct me if I'm wrong:
First, the lenght of the string is determined.
Secondly, for each position in this string is checked if it is numeric or not. But this check is dan from right to left? So in case of firstpart323secondpart: the length is 22.
then isnumeric() checks for every position from 1 to 22 if it is numeric and stops when it finds a number?
If so, part 1 is the the tekst before the value i, where i is the first position from right to left in the string where a number is found.
and part 2 is then the string on the right from this same position.
However, I am looking for a routine which find the first position from left to right (or the last position from right to left) where a number is, ...
So I changed the routine now, simply adjusting the for i = 1 to line:
Sub test()
For j = 4 To Cells(Rows.Count, 4).End(xlUp).Row
For i = Len(Cells(j, 4)) To 1 Step -1
If IsNumeric(Mid(Cells(j, 4), i, 1)) Then
Cells(j, 5) = Left(Cells(j, 4), i - 1)
Cells(j, 6) = (Right(Cells(j, 4), Len(Cells(j, 4)) - i + 1))
End If
Next i
Next j
End Sub
this almost perfectly works (except for a few cells which have multiple number combinations in the value (like: soup 10g 20boxes). But as these are only a few, I can adjust them by hand.
Thanks!
Sub test()
testString = "firstpart3secondpart"
For i = 1 To Len(testString)
If IsNumeric(Mid(testString, i, 1)) Then
part1 = Left(testString, i - 1)
part2 = (Right(testString, Len(testString) - i))
End If
Next i
MsgBox (part1)
MsgBox (part2)
End Sub
Use something like this within your loop.
I’m still having a grave problem with some files. It’s a rather stupid problem, but I’ve been working at it for quite some time and can’t find a solution.
I need leading zeroes in the time stamps, at least on the ms level.
The timestamps that my software makes always look like this: (example)
9:55:1:19 (that is 9h, 55min, 1sec, 19 ms)
while what I need would look like
09:55:01:019
It’s no problem to make a conversion in Excel. I use
=VALUE(SUBSTITUTE(G2;":";",";3))
but I always get
09:55:01:190 (190ms!!)
Thus the milliseconds are always read like comma values, which is understandable from the software’s point of view.
I'd like a solution that either appends the correct values to the end of each row in a new column or directly changes the values in the original column (D) to the correct values. (Appending is OK because my other scripts work that way already!)
Can you help out really quickly?
https://www.dropbox.com/sh/3ch6ikddplnyjgg/vUfnVgbbzH here's an example file
With a value in A1 like:
0.413206238
the formula:
=SUBSTITUTE(TEXT(A1,"hh:mm:ss.000"),".",":")
will display:
09:55:01:019
EDIT#1:
Or if you want to convert the values in column D, in place. Select the cells and run this small macro:
Sub FFormat()
Dim r As Range, L As Long, U As Long
For Each r In Selection
ary = Split(r.Text, ":")
U = UBound(ary)
L = LBound(ary)
For i = L To U
If Len(ary(i)) = 1 Then
ary(i) = "0" & ary(i)
End If
Next i
If Len(ary(U)) = 2 Then
ary(U) = "0" & ary(U)
End If
r.Value = Join(ary, ":")
Next r
End Sub
If the original in R12 (my example) is text, you can enter this big formula: :)
=TEXT(LEFT(SUBSTITUTE(R12;":";REPT(" ";20));4);"00") & ":"&TEXT(MID(SUBSTITUTE(R12;":";REPT(" ";20));14;20);"00") & ":" & TEXT(MID(SUBSTITUTE(R12;":";REPT(" ";20));34;20);"00") & ":" & TEXT(MID(SUBSTITUTE(R12;":";REPT(" ";20));54;20);"000")
Depending on your regional settings you may need to replace field separator "; " by ","
Your data in column G has a leading space - this formula in a new column should convert to a valid time value whether you have leading spaces or not
=LEFT(TRIM(G2);FIND("^";SUBSTITUTE(TRIM(G2);":";"^";3))-1)+LOOKUP(1000;RIGHT(G2;{1;2;3})+0)/86400000
format as [h]:mm:ss.000
This will cope with single or double digit milliseconds, assumes that if there are no milliseconds you will still have third : followed by a zero. Also copes with single digit hours, minutes or seconds
All, I asked a question "Excel VBA: Sort, then Copy and Paste" and received two excellent answers. However, because I failed to provide sufficient user requirements, they won't work: I asked for a fix to the existing solution I created, instead of specifying the actual business need and seeing if anyone has a better way.
(sigh) Here goes:
My boss asked me to create a ss to log issues. He wants a compound ID that concatenates the "Assigned Date" with a number that indicates what number issue it is for that day only. A new day, the count must restart at 1. E.g.:
Assigned Issue Concatenated
Date & Count = ID
5/11/2011 & 1 = 5112011-1
5/11/2011 & 2 = 5112011-2
5/11/2011 & 3 = 5112011-3
5/12/2011 & 1 = 5122011-1
I solved this with a hidden column C that calculates =IF(D2<>D1,1,C1+1), thus calculating the Issue Count by incrementing the previous issue count if the assigned date in column D is the same as the previous date, and starting over at 1 when the date changes. Another column concatenates the assigned date and the issue count, and I have my issue ID.
Quick, easy, elegant, in, out, and done. Right? But when I delivered the ss, he pointed out that if you (that is, he) sorts any part of the spreadsheet, the issue ID goes out of sequence. Of course---each formula isn't referencing the previous date in sequence if the rows are sorted out of Assigned Date order.
My immediate thought, which prompted my previous question, was to first re-sort the Assigned Date order correctly, then copy and paste the value of the calculated Issue Count to lock it in, and thus preserve the concatenated ID.
The only other way I can see to do this (in VBA, natch) is to:
evaluate all the dates in the Assigned Date column
evaluate all the numbers in the Issue Count column
calculate the latest sequential Issue Count for an a new item assigned on a given Assigned Date
Assign that sequential Issue Count to the new item
It'd be nice to then place the cursor into the next cell that the user would ordinarily go to, which would be the one right adjacent to the just-entered Assigned Date; however, that isn't necessary
That would avoid the need to re-sort the physical ss. However, besides a hazy guess that this would involve VLOOKUP, I got nothing. I couldn't find anything through searching.
Can anyone help? Or suggest a place to go? Thanks!!!
Sounds like you just want to automate a Paste Special action. The following replaces the formulas in a1:a100 with their calculated values:
Set src = ActiveSheet.Range("a1:a100")
src.Copy
src.Select
Selection.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
I think the formula =IF(D2<>D1,1,C1+1) could be improved as this relies on dates being in order. The following will preserve the count for any order that is sorted
Assume
ColA ColB ColC
Row1 Assigned_Date Issue Count Concatenate
Row2 05/11/2011 =COUNTIF($A$1:A2,A2) =TEXT(A2,"ddmmyyyy")&"-"&B2
Row3 05/11/2011 =COUNTIF($A$1:A3,A3) =TEXT(A3,"ddmmyyyy")&"-"&B3
Row4 05/12/2011 =COUNTIF($A$1:A4,A4) =TEXT(A4,"ddmmyyyy")&"-"&B4
Row5 05/11/2011 =COUNTIF($A$1:A5,A5) =TEXT(A5,"ddmmyyyy")&"-"&B5
Essentially enter B2 and C2 formulae and drag down. You might need to swap ddmmyyyy to mmddyyyy as we use dates first rather than months :)
Also, note the locking of the first part of the range only using $ - $A$1:Ax
This works perfectly for your current question but does not work if the Issue Count is assigned in time order per date.
How about using a procedure? Just click a button to add the next entry.
I've assumed that the entries will be given today's date and that the sheet layout is:
Rows: 1 = Title / 2 = left blank / 3 = Headings of the data block
Columns: A = Date / B = Issue Count / C = Combined ID / D etc = other data
Sub AddEntry()
Dim iDayRef As Long, iNumRows As Long, n As Long
With Range("A3")
iNumRows = .CurrentRegion.Rows.Count
For n = 2 To iNumRows
If .Cells(n, 1).Value = Date Then
If .Cells(n, 2).Value > iDayRef Then iDayRef = .Cells(n, 2).Value
End If
Next
.Cells(iNumRows + 1, 1).Value = Date
.Cells(iNumRows + 1, 2).Value = iDayRef + 1
.Cells(iNumRows + 1, 3).Value = Format(Date, "mm/dd/yyyy") & " - " & iDayRef + 1
.Cells(iNumRows + 1, 4).Select
End With
End Sub
And do you really need three columns for Date, Count, and Combined ID? If you went with a
yyyy/mm/dd - xx
ID format, one column could replace all three, and you could easily sort on it.