VBA Help

VbaHell

Well-known Member
Joined
Jan 30, 2011
Messages
1,220
Hello All

I hope you can help on this.

I have some VBA code that opens a file C:\tmp\SOSAX.CSV
If the file is not there the code Bugs out.
Is it possible that if this error occurs a Message box opens up saying "Export SOSAX file first" instead of the error popup.

Application.DisplayAlerts = False
Application.ScreenUpdating = False
Workbooks.Open Filename:="C:\tmp\SOSAX.CSV"
Range("A1:O500").Select
Selection.Copy
Windows("Energy CVI.xls").Activate
Sheets("Data").Select
Range("B6").Select
ActiveSheet.Paste
Windows("SOSAX.CSV").Activate
ActiveWindow.Close
ThisWorkbook.Saved = True
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
One way:

Code:
    Const sFile As String = "C:\tmp\SOSAX.CSV"
    
    If Len(Dir(sFile)) Then
        Workbooks.Open Filename:=sFile
    Else
        MsgBox "No file!"
        Exit Sub
    End If
 
Upvote 0
Code:
Sub x()
    Const sFile     As String = "C:\tmp\SOSAX.CSV"
 
    If Len(Dir(sFile)) Then
        With Workbooks.Open(Filename:=sFile)
            Range("A1:O500").Copy _
                    Workbooks("Energy CVI.xls").Worksheet("Data").Range("B6")
            .Close SaveChanges:=False
            ThisWorkbook.Saved = True
        End With

    Else
        MsgBox "No file!"
    End If
End Sub
 
Upvote 0
Hello again, I know I am now being a pain but I have spent several hours trying to make this work so I am hoping you can assist me again.
Below is all the code that I am running which is activated by a Button.
I have tried to use your code and still have the popup Debug box

Sub Update()

Application.DisplayAlerts = False
Application.ScreenUpdating = False
Workbooks.Open Filename:="C:\tmp\SOSAX.CSV"
Range("A1:O500").Select
Selection.Copy
Windows("Energy CVI.xls").Activate
Range("B6").Select
Sheets("Data").Select
Range("B6").Select
ActiveSheet.Paste
Columns("H:I").Select
Selection.NumberFormat = "0.00"
Columns("F:F").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Application.CutCopyMode = False
Windows("SOSAX.CSV").Activate
ActiveWindow.Close
ThisWorkbook.Saved = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,910
Members
452,949
Latest member
beartooth91

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