"Run-time error 70: Permission denied" error when I run this VBScript code...

Charlie4

New Member
Joined
Nov 22, 2017
Messages
9
Hi All

I've moved a big project onto new PC. I have the below sub to remove commas from a CSV file.
One line causes Run-time error 70: Permission denied. I haven't run this for a while so not sure if it's the move or an update that's broken it.

Sub:

VBA Code:
Public Sub FOffCommas(FileName As String)

' WHAT: Opens CSV file, removes spare commas from it, closes it.

Dim txt As String
Dim fso As Object

'1)  Opens text file/CSV in 'instance' of FileSystemObject
'       (a Microsoft thing).
Set fso = CreateObject("Scripting.FileSystemObject").opentextfile(FileName)

'2)  'Reads' all text from that file into string var 'txt'.
txt = fso.ReadAll

'3)  Looks through txt & removes commas
With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = ",+(?=" & vbNewLine & ")"
    txt = .Replace(txt, "")
End With
'4)  Closes text file/CSV instance
fso.Close

'5)  Opens CSV file (again, differently somehow) as file #1
Open FileName For Output As #1

'6)  Prints (commas removed) txt to it
    Print #1, txt
    
'7)  Closes it
Close #1

End Sub

Offending line (that causes error 70):
VBA Code:
With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = ",+(?=" & vbNewLine & ")"
    txt = .Replace(txt, "")
End With

Can anyone help?
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi. Never used regex but don't you need to

Set xx=CreateObject("VBScript.RegExp")

With xx?
 
Upvote 0
Hi Charlie4. I think daverunt meant something like this....
Code:
Dim RegEx As Object
Set RegEx = CreateObject("VBScript.RegExp")
With RegEx
    .Pattern = ",+(?=" & vbNewLine & ")"
    .Global = True
    txt = .Replace(txt, "")
Set RegEx = Nothing
End With
HTH. Dave
 
Upvote 0
Solution
Hi Charlie4. I think daverunt meant something like this....
Code:
Dim RegEx As Object
Set RegEx = CreateObject("VBScript.RegExp")
With RegEx
    .Pattern = ",+(?=" & vbNewLine & ")"
    .Global = True
    txt = .Replace(txt, "")
Set RegEx = Nothing
End With
HTH. Dave
That worked. Brilliant. Cheers, Chaps.
 
Upvote 0
I did everthing needed but still I get an err = 70
on
Set regex = CreateObject("VBScript.RegExp")
when
Dim regex As Object

I enabled the "Microsoft VBScript Regular Expression 5.5" in Tools -> References. prior to execution
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top