IF Statement that if true, runs warning

Vanish29

New Member
Joined
Apr 28, 2016
Messages
22
Hello there,
I am working on a personal project.
Goal: I want to click the button that is linked to this Macro, and have the Macro check to see if a certain cell (F2) on a certain sheet (Generator) is equal to Error. If that cell's value is Error or "ERROR", then I want the Critical Error to show and stop the export of the file to .txt.
If cell value is blank or 0, then I want the macro to proceed with the export.
I seem unable to change the code I have to do what I want. Therefore current code does that step. This code currently functions 100%, it just doesn't check for the "Error".
Thanks in advance :)
I am using Windows 7 with Excel 2010

Code:
Sub export2txt()
    Dim lastColumn As Integer
    Dim lastRow As Integer
    Dim strString As String
    Dim i As Integer, j As Integer
    Dim UD As String
    Dim warning


    UD = CreateObject("WScript.Shell").specialfolders("Desktop")
    UD = UD & "\Export.txt"
    Worksheets("EXPORT").Activate
    lastColumn = ActiveSheet.UsedRange.Column - 1 + ActiveSheet.UsedRange.Columns.Count
    lastRow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
    
    warning = MsgBox("One or more of the selected Systems are incompatible with the size of the ship you have chosen", vbOKOnly + vbCritical, "Warning")
    If warning = vbOK Then Exit Sub


    Open UD For Output As #1


    For i = 1 To lastRow
        Cells(i, 1).Select
        strString = ""
        For j = 1 To lastColumn
            If j <> lastColumn Then
                strString = strString & Cells(i, j).Value & "," ' Use semicolon instead of pipe.
            Else
                strString = strString & Cells(i, j).Value
            End If
        Next j
        Print #1, strString
    Next i


    Close #1
    
    Worksheets("Generator").Activate
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Add this statement prior to the export:

Code:
Dim errCheck As Range
Dim errVal As String
Set errCheck = Worksheets("Generator").Range("F2")


errVal = LCase(errCheck.value)


If errVal = "error" Then
    
   Dim response As Integer
   response = MsgBox("Aborted Export", vbCritical)
    
   End


End If
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,030
Messages
6,122,762
Members
449,095
Latest member
m_smith_solihull

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