How to bulk find and replace from a list in Google Sheets? - google-sheets-formula
I would like to bulk find and replace from a list with data over 60K rows in Google Sheets. Below are the data.
Existing Data
Sheet Name: Keyword list
Column A: wrong words that should be replaced
Column B: correct words
Sheet Name: Item name
Column A: original_item name
Column B: correct_item name (what I wanna get)
What I want to achieve
Step1. Find if each item name contains wrong word that should be
replaced (wrong word can be more than 1)
Step2. Replace those wrong words with column B from Sheet-Keyword
list
due to your extra large dataset try:
=INDEX(SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE(A2:INDEX(A:A, MAX((A:A<>"")*ROW(A:A))),'Keyword list'!A2,'Keyword list'!B2) ,'Keyword list'!A3,'Keyword list'!B3) ,'Keyword list'!A4,'Keyword list'!B4) ,'Keyword list'!A5,'Keyword list'!B5) ,'Keyword list'!A6,'Keyword list'!B6) ,'Keyword list'!A7,'Keyword list'!B7) ,'Keyword list'!A8,'Keyword list'!B8) ,'Keyword list'!A9,'Keyword list'!B9) ,'Keyword list'!A10,'Keyword list'!B10) ,'Keyword list'!A11,'Keyword list'!B11) ,'Keyword list'!A12,'Keyword list'!B12) ,'Keyword list'!A13,'Keyword list'!B13) ,'Keyword list'!A14,'Keyword list'!B14) ,'Keyword list'!A15,'Keyword list'!B15) ,'Keyword list'!A16,'Keyword list'!B16) ,'Keyword list'!A17,'Keyword list'!B17) ,'Keyword list'!A18,'Keyword list'!B18) ,'Keyword list'!A19,'Keyword list'!B19) ,'Keyword list'!A20,'Keyword list'!B20) ,'Keyword list'!A21,'Keyword list'!B21) ,'Keyword list'!A22,'Keyword list'!B22) ,'Keyword list'!A23,'Keyword list'!B23) ,'Keyword list'!A24,'Keyword list'!B24) ,'Keyword list'!A25,'Keyword list'!B25) ,'Keyword list'!A26,'Keyword list'!B26) ,'Keyword list'!A27,'Keyword list'!B27) ,'Keyword list'!A28,'Keyword list'!B28) ,'Keyword list'!A29,'Keyword list'!B29) ,'Keyword list'!A30,'Keyword list'!B30) ,'Keyword list'!A31,'Keyword list'!B31) ,'Keyword list'!A32,'Keyword list'!B32) ,'Keyword list'!A33,'Keyword list'!B33) ,'Keyword list'!A34,'Keyword list'!B34) ,'Keyword list'!A35,'Keyword list'!B35) ,'Keyword list'!A36,'Keyword list'!B36) ,'Keyword list'!A37,'Keyword list'!B37) ,'Keyword list'!A38,'Keyword list'!B38) ,'Keyword list'!A39,'Keyword list'!B39) ,'Keyword list'!A40,'Keyword list'!B40) ,'Keyword list'!A41,'Keyword list'!B41) ,'Keyword list'!A42,'Keyword list'!B42) ,'Keyword list'!A43,'Keyword list'!B43) ,'Keyword list'!A44,'Keyword list'!B44) ,'Keyword list'!A45,'Keyword list'!B45) ,'Keyword list'!A46,'Keyword list'!B46) ,'Keyword list'!A47,'Keyword list'!B47) ,'Keyword list'!A48,'Keyword list'!B48) ,'Keyword list'!A49,'Keyword list'!B49) ,'Keyword list'!A50,'Keyword list'!B50) ,'Keyword list'!A51,'Keyword list'!B51) ,'Keyword list'!A52,'Keyword list'!B52) ,'Keyword list'!A53,'Keyword list'!B53) ,'Keyword list'!A54,'Keyword list'!B54) ,'Keyword list'!A55,'Keyword list'!B55) ,'Keyword list'!A56,'Keyword list'!B56) ,'Keyword list'!A57,'Keyword list'!B57) ,'Keyword list'!A58,'Keyword list'!B58) ,'Keyword list'!A59,'Keyword list'!B59) ,'Keyword list'!A60,'Keyword list'!B60) ,'Keyword list'!A61,'Keyword list'!B61) ,'Keyword list'!A62,'Keyword list'!B62) ,'Keyword list'!A63,'Keyword list'!B63) ,'Keyword list'!A64,'Keyword list'!B64) ,'Keyword list'!A65,'Keyword list'!B65) ,'Keyword list'!A66,'Keyword list'!B66) ,'Keyword list'!A67,'Keyword list'!B67) ,'Keyword list'!A68,'Keyword list'!B68) ,'Keyword list'!A69,'Keyword list'!B69) ,'Keyword list'!A70,'Keyword list'!B70) ,'Keyword list'!A71,'Keyword list'!B71) ,'Keyword list'!A72,'Keyword list'!B72) ,'Keyword list'!A73,'Keyword list'!B73) ,'Keyword list'!A74,'Keyword list'!B74) ,'Keyword list'!A75,'Keyword list'!B75) ,'Keyword list'!A76,'Keyword list'!B76) ,'Keyword list'!A77,'Keyword list'!B77) ,'Keyword list'!A78,'Keyword list'!B78) ,'Keyword list'!A79,'Keyword list'!B79) ,'Keyword list'!A80,'Keyword list'!B80) ,'Keyword list'!A81,'Keyword list'!B81) ,'Keyword list'!A82,'Keyword list'!B82) ,'Keyword list'!A83,'Keyword list'!B83) ,'Keyword list'!A84,'Keyword list'!B84) ,'Keyword list'!A85,'Keyword list'!B85) ,'Keyword list'!A86,'Keyword list'!B86) ,'Keyword list'!A87,'Keyword list'!B87) ,'Keyword list'!A88,'Keyword list'!B88) ,'Keyword list'!A89,'Keyword list'!B89) ,'Keyword list'!A90,'Keyword list'!B90) ,'Keyword list'!A91,'Keyword list'!B91) ,'Keyword list'!A92,'Keyword list'!B92) ,'Keyword list'!A93,'Keyword list'!B93) ,'Keyword list'!A94,'Keyword list'!B94) ,'Keyword list'!A95,'Keyword list'!B95) ,'Keyword list'!A96,'Keyword list'!B96) ,'Keyword list'!A97,'Keyword list'!B97) ,'Keyword list'!A98,'Keyword list'!B98) ,'Keyword list'!A99,'Keyword list'!B99) ,'Keyword list'!A100,'Keyword list'!B100) ,'Keyword list'!A101,'Keyword list'!B101) ,'Keyword list'!A102,'Keyword list'!B102) ,'Keyword list'!A103,'Keyword list'!B103) ,'Keyword list'!A104,'Keyword list'!B104) ,'Keyword list'!A105,'Keyword list'!B105) ,'Keyword list'!A106,'Keyword list'!B106) ,'Keyword list'!A107,'Keyword list'!B107) ,'Keyword list'!A108,'Keyword list'!B108) ,'Keyword list'!A109,'Keyword list'!B109) ,'Keyword list'!A110,'Keyword list'!B110) ,'Keyword list'!A111,'Keyword list'!B111) ,'Keyword list'!A112,'Keyword list'!B112) ,'Keyword list'!A113,'Keyword list'!B113) ,'Keyword list'!A114,'Keyword list'!B114) ,'Keyword list'!A115,'Keyword list'!B115) ,'Keyword list'!A116,'Keyword list'!B116) ,'Keyword list'!A117,'Keyword list'!B117) ,'Keyword list'!A118,'Keyword list'!B118) ,'Keyword list'!A119,'Keyword list'!B119) ,'Keyword list'!A120,'Keyword list'!B120) ,'Keyword list'!A121,'Keyword list'!B121) ,'Keyword list'!A122,'Keyword list'!B122) ,'Keyword list'!A123,'Keyword list'!B123) ,'Keyword list'!A124,'Keyword list'!B124) ,'Keyword list'!A125,'Keyword list'!B125) ,'Keyword list'!A126,'Keyword list'!B126) ,'Keyword list'!A127,'Keyword list'!B127) ,'Keyword list'!A128,'Keyword list'!B128) ,'Keyword list'!A129,'Keyword list'!B129) ,'Keyword list'!A130,'Keyword list'!B130) ,'Keyword list'!A131,'Keyword list'!B131) ,'Keyword list'!A132,'Keyword list'!B132) ,'Keyword list'!A133,'Keyword list'!B133) ,'Keyword list'!A134,'Keyword list'!B134) ,'Keyword list'!A135,'Keyword list'!B135) ,'Keyword list'!A136,'Keyword list'!B136) ,'Keyword list'!A137,'Keyword list'!B137) ,'Keyword list'!A138,'Keyword list'!B138) ,'Keyword list'!A139,'Keyword list'!B139) ,'Keyword list'!A140,'Keyword list'!B140) ,'Keyword list'!A141,'Keyword list'!B141) ,'Keyword list'!A142,'Keyword list'!B142) ,'Keyword list'!A143,'Keyword list'!B143) ,'Keyword list'!A144,'Keyword list'!B144) ,'Keyword list'!A145,'Keyword list'!B145) ,'Keyword list'!A146,'Keyword list'!B146) ,'Keyword list'!A147,'Keyword list'!B147) ,'Keyword list'!A148,'Keyword list'!B148) ,'Keyword list'!A149,'Keyword list'!B149) ,'Keyword list'!A150,'Keyword list'!B150) ,'Keyword list'!A151,'Keyword list'!B151) ,'Keyword list'!A152,'Keyword list'!B152) ,'Keyword list'!A153,'Keyword list'!B153) ,'Keyword list'!A154,'Keyword list'!B154) ,'Keyword list'!A155,'Keyword list'!B155) ,'Keyword list'!A156,'Keyword list'!B156) ,'Keyword list'!A157,'Keyword list'!B157) ,'Keyword list'!A158,'Keyword list'!B158) ,'Keyword list'!A159,'Keyword list'!B159) ,'Keyword list'!A160,'Keyword list'!B160) ,'Keyword list'!A161,'Keyword list'!B161) ,'Keyword list'!A162,'Keyword list'!B162) ,'Keyword list'!A163,'Keyword list'!B163) ,'Keyword list'!A164,'Keyword list'!B164) ,'Keyword list'!A165,'Keyword list'!B165) ,'Keyword list'!A166,'Keyword list'!B166) ,'Keyword list'!A167,'Keyword list'!B167) ,'Keyword list'!A168,'Keyword list'!B168) ,'Keyword list'!A169,'Keyword list'!B169) ,'Keyword list'!A170,'Keyword list'!B170) ,'Keyword list'!A171,'Keyword list'!B171) ,'Keyword list'!A172,'Keyword list'!B172) ,'Keyword list'!A173,'Keyword list'!B173) ,'Keyword list'!A174,'Keyword list'!B174) ,'Keyword list'!A175,'Keyword list'!B175) ,'Keyword list'!A176,'Keyword list'!B176))
and give it a few seconds to complete...
this formula was generated by "formula generator"
in your Keyword list sheet add column C then paste this in C2:
={""; "=INDEX("&QUERY(FILTER(IFERROR('Keyword list'!A2:A/0, "SUBSTITUTE("), 'Keyword list'!A2:A<>""),,9^9)&"A2:INDEX(A:A, MAX((A:A<>"""")*ROW(A:A)))"&
QUERY(FILTER(IFERROR('Keyword list'!A2:A/0, ",'Keyword list'!A"&ROW('Keyword list'!A2:A)&",'Keyword list'!B"&ROW('Keyword list'!A2:A)&")"), 'Keyword list'!A2:A<>""),,9^9)&")"}
next copy C3 and paste into your Item name sheet into B2 cell
Related
power query - split data from one column into more rows
I am not allowed to paste an image here, so I have copied the power query table. I need to split this column into rows. For an example: 1st row: Q3(71.0 µm) = 63.64 % 2nd row: 1-Q3(315.0 µm) = 0.87 % the column is pasted below. Column1 "Q3(71.0 µm) = 63.64 % 1-Q3(315.0 µm) = 0.87 % p3(71.0 µm,105.0 µm) = 13.36 % p3(105.0 µm,149.0 µm) = 11.74 %p3(149.0 µm,210.0 µm) = 7.14 % p3(210.0 µm,250.0 µm) = 1.88 % p3(250.0 µm,315.0 µm) = 1.37 %" "Q3 (b/l=0.5) = 8.80 %Mean value SPHT3 = 0.7743 Mean value Symm3 = 0.9041 Mean value b/l3 = 0.6985"
Z notation to prolog
I have a Z specification and I have to translate it into Prolog code. I try but it doesn't work so someone can help me? linkedListInit(LinkedList) :- LinkedList = {[linked,L],[size, S]} & list(L) & S is 0. insert(LinkedList,Node_i,LinkedList_):- insertNode(LinkedList,Node_i,LinkedList_). insertNode(LinkedList,Node_i,LinkedList_):- LinkedList = {[linked,L],[size,S]} & concat(L,Node_i,L_) & S_ is S + 1 & LinkedList_ = {[linked,L_],[size,S_]}. extract(LinkedList,Node_i,LinkedList_):- extractHeadNodeOk(LinkedList,Node_i,Msg_o,LinkedList_) or extractHeadNodeError(LinkedList,Node_i,Msg_o,LinkedList_). extractHeadNodeOk(LinkedList,Node_i,Msg_o,LinkedList_):- LinkedList = {[linked,L],[size,S]} & L neq {} & tail(L,L_) & head(L,Node_i) & S_ is S-1 & Msg_o = ok & LinkedList_ = {[linked,L_],[size,S_]}. extractHeadNodeError(LinkedList,Node_i,Msg_o,LinkedList_):- LinkedList = {[linked,L] / _} & slits(L) & L = {} & (nran(L,D) & Node_i in D) & Msg_o = error & LinkedList_ = LinkedList. checkNode(LinkedList,Node_i,LinkedList_):- checkNodeOk(LinkedList,Node_i,Msg_o,LinkedList_) or checkNodeError(LinkedList,Node_i,Msg_o,LinkedList_). checkNodeOk(LinkedList,Node_i,Msg_o,LinkedList_):- LinkedList = {[linked,L],[size,S]} & (ran(L,D) & Node_i in D) & Msg = ok & LinkedList_ = LinkedList. checkNodeError(LinkedList,Node_i,Msg_o,LinkedList_):- LinkedList = {[linked,L] / _} & (nran(L,D) & Node_i in D) & Msg = error & LinkedList_ = LinkedList. removePartialNode(LinkedList,Node_i,LinkedList_):- removePartialNodeOk(LinkedList,Node_i,Msg_o,LinkedList_) or removePartialNodeError(LinkedList,Node_i,Msg_o,LinkedList_). removePartialNodeOk(LinkedList,Node_i,Msg_o,LinkedList_):- LinkedList = {[linked,L],[size,S]} & (ran(L,D) & Node_i in D) & dares(dom(filter(L,Node_i,R),A),L,L_) & Msg_o = ok & LinkedList_ = {[linked,L_],[size,S_]}. removePartialNodeError(LinkedList,Node_i,Msg_o,LinkedList_):- LinkedList = {[linked,L] / _} & (nran(L,D) & Node_i in D) & Msg_o = error & LinkedList_ = LinkedList. removePartialData(LinkedList,Node_i,Data_i,LinkedList_):- removePartialDataOk(LinkedList,Node_i,Data_i,Msg_o,LinkedList_) or removePartialDataError(LinkedList,Node_i,Data_i,Msg_o,LinkedList_). removePartialDataOk(LinkedList,Node_i,Data_i,Msg_o,LinkedList_):- LinkedList = {[linked,L],[size,S]} & (ran(L,D) & Node_i in D) & (ran(Data_i,F) & Data_i in F) & dares(dom(filter(L,rres(Data_i,Node_i,S),A),L,L_) & Msg_o = ok & LinkedList_ = {[linked,L_],[size,S_]}. removePartialDataError(LinkedList,Node_i,Data_i,Msg_o,LinkedList_):- LinkedList = {[linked,L] / _} & (ran(L,D) & Node_i in D) & (nran(Data_i,F) & Data_i in F) & Msg_o = error & LinkedList_ = LinkedList.
find the greatest of 10 numbers in vbscript
I just created vbscript code to find the greatest numbers from 10 input numbers. It works only for numbers between 1 to 10. Why? <html> <head> <title>Enter 10 numbers to find the largest number</title> <meta http-equiv="x-ua-compatible" content="IE=10"> </head> <body> <script language = "vbscript" type = "text/vbscript"> Dim n1 Dim n2 Dim n3 Dim n4 Dim n5 Dim n6 Dim n7 Dim n8 Dim n9 Dim n10 Dim largest n1=inputBox("please Enter the number1 ","number1") n2=inputBox("please Enter the number2 ","number2") n3=inputBox("please Enter the number3 ","number3") n4=inputBox("please Enter the number4 ","number4") n5=inputBox("please Enter the number5 ","number5") n6=inputBox("please Enter the number6 ","number6") n7=inputBox("please Enter the number7 ","number7") n8=inputBox("please Enter the number8 ","number8") n9=inputBox("please Enter the number9 ","number9") n10=inputBox("please Enter the number10 ","number10") If (n1>n2 AND num1>vnum3 AND n1>n4 AND n1>n5 AND n1>n6 AND n1>n7 AND n1>n8 AND n1>n9 AND n1>n10 ) then document.write("this is the largest number " & n1 & ", Enjoy VBscript") ElseIf (n2>n1 AND n2>n3 AND n2>n4 AND n2>n5 AND n2>n6 AND n2>n7 AND n2>n8 AND n2>n9 AND n2>n10 ) Then document.write("this is the largest number " & n2 & ", Enjoy VBscript") ElseIf (n3>n1 AND n3>n2 AND n3>n4 AND n3>n5 AND n3>n6 AND n3>n7 AND n3>n8 AND n3>n9 AND n3>n10 ) Then document.write("this is the largest number " & n3 & ", Enjoy VBscript") ElseIf (n4>n1 AND n4>n3 AND n4>n2 AND n4>n5 AND n4>n6 AND n4>n7 AND n4>n8 AND n4>n9 AND n4>n10 ) Then document.write("this is the largest number " & n4 & ", Enjoy VBscript") ElseIf (n5>n1 AND n5>n3 AND n5>n4 AND n5>n2 AND n5>n6 AND n5>n7 AND n5>n8 AND n5>n9 AND n5>n10 ) Then document.write("this is the largest number " & n5 & ", Enjoy VBscript") ElseIf (n6>n1 AND n6>n3 AND n6>n4 AND n6>n5 AND n6>n2 AND n6>n7 AND n6>n8 AND n6>n9 AND n6>n10 ) Then document.write("this is the largest number " & n6 & ", Enjoy VBscript") ElseIf (n7>n1 AND n7>n3 AND n7>n4 AND n7>n5 AND n7>n6 AND n7>n2 AND n7>n8 AND n7>n9 AND n7>n10 ) Then document.write("this is the largest number " & n7 & ", Enjoy VBscript") ElseIf (n8>n1 AND n8>n3 AND n8>n4 AND n8>n5 AND n8>n6 AND n8>n7 AND n8>n2 AND n8>n9 AND n8>n10 ) Then document.write("this is the largest number " & n8 & ", Enjoy VBscript") ElseIf (n9>n1 AND n9>n3 AND n9>n4 AND n9>n5 AND n9>n6 AND n9>n7 AND n9>n8 AND n9>n2 AND n9>n10 ) Then document.write("this is the largest number " & n9 & ", Enjoy VBscript") ElseIf (n10>n1 AND n10>n3 AND n10>n4 AND n10>n5 AND n10>n6 AND n10>n7 AND n10>n8 AND n10>n9 AND n10>n2 ) Then document.write("this is the largest number " & n10 & ", Enjoy VBscript") Else document.write("this is the largest number is " & number10 & ", Enjoy VBscript") End If </script> </body> </html>
This puts things into loops and uses an object that sorts. 'Getting the 10 numbers Dim Thing(10) For x = 0 to 9 Thing(x) = InputBox("Something") Next Set rs = CreateObject("ADODB.Recordset") With rs 'Setting up a one field database containing a single precision field called Numbers .Fields.Append "Number", 4 .Open 'Adding the data For x = 0 to 9 .AddNew .Fields("Number").value = Thing(x) .UpDate Next 'Sorting descending on number field .Sort = "Number DESC" 'Now write it MsgBox .Fields("Number").Value 'And the second one .MoveNext MsgBox .Fields("Number").Value 'And all the rest For x = 0 to 7 .MoveNext MyStr = MyStr & .Fields("Number").Value & vbcrlf Next MsgBox MyStr End With
date difference between two dates and the output should be in date format using vb script
I am new to VB Scripting. I have two dates, date_1 and date_2. I need to subtract date_1 from date_2 and the output the difference in date format. example: date_1 = 01-09-2014 date_2 = 08-10-2016 output would ideally be: = date_2 - date_1 = 08-10-2016 - 01-09-2014 = 07-01-0002 Finally i need the output like 02 years, 01 months and 07 days. Please help me out. Many thanks in advance.
Take a look at the below code: Dim date_1, date_2, l, r(2) date_1 = "01-09-2014" date_2 = "08-10-2016" l = SetLocale(2057) ' UK Delta 0, Array("yyyy", "m", "d"), Array("years", "months", "days"), r, CDate(date_1), CDate(date_2), False SetLocale l MsgBox Join(r, ", ") ' 2 years, 1 months, 7 days Sub Delta(i, t, n, r, d1, d2, c) Dim q, d q = DateDiff(t(i), d1, d2) If UBound(t) > i Then Do d = DateAdd(t(i), q, d1) Delta i + 1, t, n, r, d, d2, c If c Then Exit Do q = q - 1 Loop End If c = q >= 0 r(i) = q & " " & n(i) End Sub You even can set the date and time and compute difference up to second: Dim date_1, date_2, l, r(5) date_1 = "01-09-2014 10:55:30" date_2 = "08-10-2016 15:45:10" l = SetLocale(2057) ' UK Delta 0, Array("yyyy", "m", "d", "h", "n", "s"), Array("years", "months", "days", "hours", "minutes", "seconds"), r, CDate(date_1), CDate(date_2), False SetLocale l MsgBox Join(r, ", ") ' 2 years, 1 months, 7 days, 4 hours, 49 minutes, 40 seconds Sub Delta(i, t, n, r, d1, d2, c) Dim q, d q = DateDiff(t(i), d1, d2) If UBound(t) > i Then Do d = DateAdd(t(i), q, d1) Delta i + 1, t, n, r, d, d2, c If c Then Exit Do q = q - 1 Loop End If c = q >= 0 r(i) = q & " " & n(i) End Sub
Using some string functions , you can easily achieve your goal . Hope this helps . date_1 = InputBox("Enter first Date in format DD-MM-YYYY","Time Difference") date_2 = InputBox("Enter second Date in format DD-MM-YYYY","Time Difference") day_differ = Abs(CInt(Left(date_2 , 2)) - CInt(Left(date_1 , 2))) month_differ = Abs(CInt(Mid(date_2 , 4 , 2)) - CInt(Mid(date_1 , 4 , 2))) year_differ = Abs(CInt(Right(date_2 , 4)) - CInt(Right(date_1 , 4))) wscript.echo "Date Difference is " & year_differ & " years, " & month_differ & " months and " & day_differ & " days."
Date difference in Day-Hour-Minutes format
Can we get the two date values difference using VBScript in Day-Hour-Minutes format?
here is a HH::MM:SS example changing it to DD::HH::MM should be easy ASP FAQ - Date time routines manipulation <% Function TimeSpan(dt1, dt2) If (isDate(dt1) And IsDate(dt2)) = false Then TimeSpan = "00:00:00" Exit Function End If seconds = Abs(DateDiff("S", dt1, dt2)) minutes = seconds \ 60 hours = minutes \ 60 minutes = minutes mod 60 seconds = seconds mod 60 if len(hours) = 1 then hours = "0" & hours TimeSpan = hours & ":" & _ RIGHT("00" & minutes, 2) & ":" & _ RIGHT("00" & seconds, 2) End Function d1 = "2002-03-27 9:20:25 AM" d2 = "2002-03-27 9:20:45 AM" Response.Write TimeSpan(d1, d2) %> something like:( im not sure, I don't know vbscript) <% Function TimeSpan(dt1, dt2) If (isDate(dt1) And IsDate(dt2)) = false Then TimeSpan = "00:00:00" Exit Function End If seconds = Abs(DateDiff("S", dt1, dt2)) minutes = seconds \ 60 hours = minutes \ 60 days = hours \ 24 minutes = minutes mod 60 seconds = seconds mod 60 hours = hours mod 24 if len(hours) = 1 then hours = "0" & hours TimeSpan = days& ":" & _ RIGHT("00" & hours , 2) & ":" & _ RIGHT("00" & minutes, 2) End Function d1 = "2002-03-27 9:20:25 AM" d2 = "2002-03-27 9:20:45 AM" Response.Write TimeSpan(d1, d2) %>