VBA Code is picking up a column not called out - regex

Sub UpdateDMDCLCSIM()
Dim SIM_DM_DCLC As Worksheet
Dim TextFileUpdated As Date
Set SIM_DM_DCLC = ThisWorkbook.Sheets(Sheet52.Name)
TextFileUpdated = DateValue(FileDateTime("\\networkshare\dept\DCGSI\Extracts\SIM_DM_DCLC.csv"))
Application.DisplayAlerts = False
Application.StatusBar = "Importing latest DM DCLC SIM Data..."
With SIM_DM_DCLC.QueryTables.Add(Connection:= _
"TEXT;\\networkshare\dept\DCGSI\Extracts\SIM_DM_DCLC.csv" _
, Destination:=SIM_DM_DCLC.Range("$A$1"))
.Name = "SIM_DM_DCLC"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 936
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
'Change to MySQL date format.
SIM_DM_DCLC.Range("I:K", "P:T").Replace Chr(84), " "
SIM_DM_DCLC.Range("I:K", "P:T").Replace Chr(90), ""
SIM_DM_DCLC.Range("I:K", "P:T").NumberFormat = "yyyy-mm-dd hh:mm:ss"
Okay so this opens a csv that is downloaded to a network share and fixes some dates. The dates in the original file are formatted YYYY-MM-DDTHH:MM:SSZ and this is supposed to strip the T and Z from those dates in the appropriate columns. The issue I am having is that for some strange reason it is processing column L in the file and I can't figure out why.
So I looked up some code for regex replace in VBA and tried to refactor the code to use the following code to try and fix the issue:
Sub UpdateDMDCLCSIM()
On Error GoTo ErrorHandler
Dim SIM_DM_DCLC As Worksheet
Dim TextFileUpdated As Date
Set SIM_DM_DCLC = ThisWorkbook.Sheets(Sheet52.Name)
TextFileUpdated = DateValue(FileDateTime("\\networksharem\dept\DCGSI\Extracts\SIM_DM_DCLC.csv"))
Application.DisplayAlerts = False
Application.StatusBar = "Importing latest DM DCLC SIM Data..."
With SIM_DM_DCLC.QueryTables.Add(Connection:= _
"TEXT;\\networkshare\dept\DCGSI\Extracts\SIM_DM_DCLC.csv" _
, Destination:=SIM_DM_DCLC.Range("$A$1"))
.Name = "SIM_DM_DCLC"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 936
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
'Change to MySQL date format.
Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = "/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z)$/"
For Each cell In SIM_DM_DCLC.UsedRange
If cell.Value <> "" Then cell.Value = regex.Replace(cell.Value, "/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/")
Next cell
Pretty sure that the 5017 - Application-defined or object-defined error I am getting on the regex.Replace means I have something wrong with the regex piece. Just not sure what it is.

Well you have to check to an actual match and not just a blank; here is the updated and appropriate section of code.
'Change to MySQL date format.
Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = "^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z)$"
For Each cell In SIM_DM_DCLC.UsedRange
If cell.Value = regex.Pattern Then cell.Value = regex.Replace(cell.Value, "^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$")
Next cell

Related

Regex + Word Macro for Replacing Styles

I've got a bunch of documents with disparate styles that I've been adding to a long Macro that finds and replaces these styles with the correct ones. Right now, I'm just adding to a list as I find a wrong style. For example, there can be Heading 1, heading 1, H1, or h1. I want to write a find and replace function for each of those for the moment. What would be cooler is if I could write a catch all macro for these sorts of things using Regex: (h|H).{6}\s1 (not the best Regex writer, so bear with that). Ideally that would catch anything the variations of heading 1 (though it would not catch the h1, H1 cases, though I could add that easily enough.
I know that VBA supports Regex. I've added the reference to it. I also know how this would work for replacing specific text. I'm not replacing text though. ONLY formatting. I haven't played around with it too much. I just want to know if I can use the Regex when working specifically with a style. Here's what the functions look like right now:
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading 1")
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("SSC TOC 2")
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
I simply recorded that. Now, would I be able to put Regex in place of that string, like so:
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles(someRegex function (h|H).{6}\s1)
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("SSC TOC 2")
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Basically just using a function someRegex function (h|H).{6}\s1 in place of the string literal. Is there any way to do this? Would appreciate any guidance or help!
You could use something along the lines the following macro to delete all unused user-defined Styles (except Linked Styles) in a document, and to clean up the various H1, etc. Styles you mentioned:
Sub CleanUpStyles()
Application.ScreenUpdating = False
Dim Doc As Document, bDel As Boolean, bHid As Boolean
Dim Rng As Range, StlNm As String, i As Long
bHid = ActiveWindow.View.ShowHiddenText
ActiveWindow.View.ShowHiddenText = True
Set Doc = ActiveDocument
With Doc
For i = .Styles.Count To 1 Step -1
With .Styles(i)
If .BuiltIn = False And .Linked = False Then
bDel = True: StlNm = .NameLocal
For Each Rng In Doc.StoryRanges
With Rng
With .Find
.ClearFormatting
.Format = True
.Style = StlNm
.Execute
End With
If .Find.Found = True Then
If StlNm Like "[Hh]*#" Then
If StlNm <> "Heading " & Right(StlNm, 1) Then
.Style = "Heading " & Right(StlNm, 1)
bDel = True
End If
Else
bDel = False
End If
Exit For
End If
End With
Next
If bDel = True Then .Delete
End If
End With
Next
End With
ActiveWindow.View.ShowHiddenText = bHid
Application.ScreenUpdating = True
End Sub

VBA Using regex for replacing stuff

I never did VBA before, and I am not skilled in programming in any way xD
I am trying do remove some things from a list that contains html descriptions, but it is not working.
Any advice what horrible things I did wrong here?
Function entferne_sonstige_zeichen(description)
entferne_sonstige_zeichen = description
Dim oRegExp As RegExp
Set oRegExp = New RegExp
With oRegExp
.IgnoreCase = False
.Global = True
.MultiLine = True
.Pattern = "[^A-Za-z\d,/-]"
End With
Dim ReplacePattern As String
ReplacePattern = ""
description = oRegExp.Replace(ReplacePattern)
End Function
Maybe...
Function entferne_sonstige_zeichen(str As String) As String
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.IgnoreCase = False
.Global = True
.MultiLine = True
.Pattern = "[^A-Za-z\d,/-]"
.ignorecase = True
entferne_sonstige_zeichen = .Replace(str, vbNullString)
End With
End Function

VBScript Regexp Does Not Give Result As Expected

Dim regEx
Set regEx = New RegExp
With regEx
.Pattern = "\[QUOTE=(.*?)\](.*?)\[\/QUOTE\]"
.IgnoreCase = True
.Global = True
.MultiLine = True
End With
string1="[QUOTE=P2]A[/QUOTE]B[QUOTE=P3][QUOTE=P1]C[/QUOTE]D[/QUOTE]E"
response.write regEx.Replace(string1, "")
I want BE as a result but I get BD[/QUOTE]E
Where is the problem?
Just do some conversions step by step to get the necessary structure, then retrieve result:
string1 = "[QUOTE=P2]A[/QUOTE]B[QUOTE=P3][QUOTE=P1]C[/QUOTE]D[/QUOTE]E"
With New RegExp
.IgnoreCase = True
.Global = True
.MultiLine = True
.Pattern = "\[QUOTE=(.*?)\]"
string1 = .Replace(string1, "[")
.Pattern = "\[\/QUOTE\]"
string1 = .Replace(string1, "]")
.Pattern = "\[[^[]]*?\]"
Do While .Test(string1)
string1 = .Replace(string1, "")
Loop
End With
response.write string1

Create vTiger Sales Order fails due to MANDATORY_FIELDS_MISSING "message":"Mandatory Fields Missing

I'm trying to create a SalesOrder via WebServices but it always fails due to missing mandatory fields.
I'm sending the following fields.
The error message does not specify the missing fields
I'm using vTiger 6.0.0
How can I figure it out
salesOrder.subject = fullDescription
salesOrder.sostatus = "delivered"
salesOrder.account_id ='11x28'
salesOrder.bill_street = shipping.address.street
salesOrder.bill_city = shipping.address.city
salesOrder.bill_state = shipping.address.state
salesOrder.bill_code = shipping.address.postalCode
salesOrder.bill_country = shipping.address.postalCode
salesOrder.ship_street = shipping.address.street
salesOrder.ship_city = shipping.address.city
salesOrder.ship_state = shipping.address.state
salesOrder.ship_code = shipping.address.postalCode
salesOrder.ship_country = shipping.address.postalCode
salesOrder.invoicestatus = "Created"
salesOrder.productid = selectedServices[0].id
salesOrder.quantity = 1
salesOrder.listprice = selectedServices[0].unit_price
//
salesOrder.comment= ""
salesOrder.tax1 = ""
salesOrder.tax2 = "10.000"
salesOrder.tax3 = "6.000"
salesOrder.pre_tax_total = "876.00"
salesOrder.currency_id = "21x1"
salesOrder.conversion_rate = "1.000"
salesOrder.tax4 = ""
salesOrder.duedate = "2014-12-12"
salesOrder.carrier = "FedEx"
salesOrder.pending = ""
salesOrder.txtAdjustment = "-21.00000000"
salesOrder.salescommission = "5.000"
salesOrder.exciseduty = "0.000"
salesOrder.hdnGrandTotal = "995.16000000"
salesOrder.hdnSubTotal = "876.00000000"
salesOrder.hdnTaxType = "group"
salesOrder.hdndiscountamount = "0"
salesOrder.hdnS_H_Percent = "21"
salesOrder.discount_percent = "25.000"
salesOrder.discount_amount = ""
salesOrder.terms_conditions = "- Unless "
salesOrder.enable_recurring = "0"
I was missing the LineItems. Please notice that it has to be CamelCase
"LineItems": [
{
"productid": "25x142",
"listprice": "299.00000000",
"quantity": "1"
}
],
you missed the mandatory field "Assigned TO"

MS Word VBA How to Search and copy selected text to beginning of the Paragraph

I want to search and copy year specified in the paragraph and copy it to beginning of the paragraph. following is the code i am working with, it does selects the year but doesn't copy it to the beginning:
Sub CopyYeartoFirst()
'
' Macro1 Macro
'
' Selection.Find.ClearFormatting
With ActiveDocument.Content
With Selection.Find
.Text = "[0-9]{4}"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute
End With
While .Find.Found = True
Selection.Copy
Selection.HomeKey Unit:=wdLine
Selection.PasteAndFormat (wdPasteDefault)
'Selection.TypeText Text:=" -- "
.Find.Execute
'Selection.Find.ClearFormatting
Wend
End With
End Sub
You will need to use something like this before pasting your search results
Dim oRng As Range
Set oRng = Selection.Paragraphs(1).Range
oRng.Collapse wdCollapseStart
oRng.Select