vba excel - if not statement exiting early

devofish

Board Regular
Joined
Dec 10, 2016
Messages
68
I've managed to get this code started, but am finding difficulty seeing what's going on here. The message box never occurs when the inputbox input is "". The selection delete part works, but anything past that does not execute. I've made several attempts at restructuring the if not to if else, but nothing seems to stick. Any wisdom here? Much appreciated.
Code:
Public Sub Process()        
    On Error Resume Next   
    Set wb = ThisWorkbook
    Set ws = wb.Sheets(2)
        With ws
            .Range("A2").Select
            On Error GoTo 0
            Set xRg = Application.InputBox("Please select the first survey after landing (and/or casing) depth:", "Select", xTxt, , , , , 8)
            If Not xRg Is Nothing Then
                xRg.Offset(1, 0).Select
                Rows(ActiveCell.Row & ":" & Rows.Count).Delete
            End If '\\\\\\bust - routine exits here
            If xRg = "" Then
                MsgBox "No processing occurred.", vbInformation, "Exit"
                Exit Sub
            End If
        End With
        Call Export 'evidence of call not run
        ws.Delete 'evidence of ws is not deleted
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Take out the On Error statement and see where the error occurs.
 
Upvote 0
object required error on
Code:
Set xRg = Application.InputBox("Please select the first survey after landing (and/or casing) depth:", "NexGen", xTxt, , , , , 8)
 
Upvote 0
xRng should be declared as a Range variable. I don't see it declared at all.
 
Upvote 0
Apologies for the delayed response. It was declared outside the routine. When I cancel the routine, it errors on xRg =.
Code:
Public Sub Begin_Macro()
    OptimizeCode_Begin
    Set wb = ThisWorkbook
    Set ws = wb.Sheets(1)
    Set rng = ws.Range("A2")
        With ws
            If WorksheetFunction.CountA(rng) <> 0 Then
                GoTo 101
            Else
                MsgBox "No!"
                Exit Sub
            End If
        End With
101:
        For Each ws In Worksheets
            If ws.Name = "name" Then
                Sheets("name").Delete
                End
            End If
        Next       
        Sheets.Add.Name = "name"
        Set ws = wb.Sheets(1)
            With ws
                i = .Range("A" & Rows.Count).End(xlUp).Row
                .Range("A1:E1" & i).Copy
                Sheets("name").Range("A1").PasteSpecial xlPasteValues
            Application.CutCopyMode = False
            End With
    ProcessData
End Sub

Public Sub ProcessData()        
    Dim xRg As Range
    Set wb = ThisWorkbook
    Set ws = wb.Sheets("name")
        With ws
            .Range("A2").Select
            Set xRg = Application.InputBox("Please select the first point after landing:", "NexGen", xTxt, , , , , 8)
            If Not xRg Is Nothing Then
                xRg.Offset(1, 0).Select
                Rows(ActiveCell.Row & ":" & Rows.Count).Delete
            End If
            If xRg = "" Then
                MsgBox "No processing occurred.", vbInformation
                Exit Sub
            End If
        End With
    OptimizeCode_End
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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