skipping code

Cerebum

Board Regular
Joined
Dec 2, 2008
Messages
183
The following is an extract from a longer piece of code that transfers vehicles from one location to another. The section below handles a log which shows outstanding transfers. The sheet works on serial numbers. The code is designed to list new vehicle loans and remove them from the list once the vehicle is returned. What I am struggling with is skipping the code between the notes (code which adds vehicles to the list) when the initial instruction clears out vehicle details.

Code:
Sub tester()
With Sheets("X")
    Set F = .Columns("C").Find(What:=.Range("D448").Value, LookIn:=xlValues, LookAt:=xlWhole)
    If Not F Is Nothing Then F.EntireRow.ClearContents
End With
' need to add a goto here somewhere but only when it clears contents

    With Sheets("X")
    .Range("B448:G448").Copy
    .Range("A445").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    .Range("A442:F445").Sort Key1:=Range("A442"), Order1:=xlAscending, Header:=xlGuess _
        , OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
    End With
  'need it to go here
    Range("F438:F440").Select
    Selection.Copy
    Range("F442").Select
    ActiveSheet.Paste
    
End Sub

Please excuse the rough code. I have improved significantly, thanks to the help I have received from this forum, but sometimes my dodgy foundations let me down.

Thanks
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Code:
Sub tester()
With Sheets("X")
    Set F = .Columns("C").Find(What:=.Range("D448").Value, LookIn:=xlValues, LookAt:=xlWhole)
[COLOR=red][B]    If Not F Is Nothing Then [/B][/COLOR]
[COLOR=red][B]        F.EntireRow.ClearContents[/B][/COLOR]
[COLOR=red][B]        Exit Sub[/B][/COLOR]
[COLOR=red][B]    End If
[/B][/COLOR]End With
' need to add a goto here somewhere but only when it clears contents
 
Upvote 0
Code:
Sub tester()
With Sheets("X")
    Set F = .Columns("C").Find(What:=.Range("D448").Value, LookIn:=xlValues, LookAt:=xlWhole)
[COLOR=red][B]    If Not F Is Nothing Then [/B][/COLOR]
[COLOR=red][B]        F.EntireRow.ClearContents[/B][/COLOR]
[COLOR=red][B]        Exit Sub[/B][/COLOR]
[COLOR=red][B]    End If
[/B][/COLOR]End With
' need to add a goto here somewhere but only when it clears contents

thanks, although this would drop me out of the code but I need to skip a couple of lines
 
Upvote 0
Maybe something like this...

Rich (BB code):
Sub tester()
With Sheets("X")
    Set F = .Columns("C").Find(What:=.Range("D448").Value, LookIn:=xlValues, LookAt:=xlWhole)
    If Not F Is Nothing Then F.EntireRow.ClearContents
'Why not remove the below End With continuing under your If statement

End With
' need to add a goto here somewhere but only when it clears contents
'and removing the below With Sheets("X") since you are continuing with the original With
    With Sheets("X")
    .Range("B448:G448").Copy
    .Range("A445").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    .Range("A442:F445").Sort Key1:=Range("A442"), Order1:=xlAscending, Header:=xlGuess _
        , OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
    End With  'AND REMOVE THIS
  'need it to go here
ELSE  ' INSERT THIS !!!
'Since your With statement has not been closed modify your remaining code to look like this:

    .Range("F438:F440").Copy
    .Range("F442")
End if
End With    
End Sub
 
Upvote 0
Maybe something like this...

Rich (BB code):
Sub tester()
With Sheets("X")
    Set F = .Columns("C").Find(What:=.Range("D448").Value, LookIn:=xlValues, LookAt:=xlWhole)
    If Not F Is Nothing Then F.EntireRow.ClearContents
'Why not remove the below End With continuing under your If statement

End With
' need to add a goto here somewhere but only when it clears contents
'and removing the below With Sheets("X") since you are continuing with the original With
    With Sheets("X")
    .Range("B448:G448").Copy
    .Range("A445").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    .Range("A442:F445").Sort Key1:=Range("A442"), Order1:=xlAscending, Header:=xlGuess _
        , OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
    End With  'AND REMOVE THIS
  'need it to go here
ELSE  ' INSERT THIS !!!
'Since your With statement has not been closed modify your remaining code to look like this:

    .Range("F438:F440").Copy
    .Range("F442")
End if
End With    
End Sub

This is definitely getting there :) The code I need to skip is the stuff just after the first unwanted end with, so, if I put the "Else" there that should do it, shouldn't it?
 
Upvote 0
For me, I still have to Step-through all code (Using the F8 Function key) to "watch" each line of code to SEE if it is doing what I want; Try stepping thru code and write back with results.
Someone can help from there, if there is a problem//
 
Upvote 0
For me, I still have to Step-through all code (Using the F8 Function key) to "watch" each line of code to SEE if it is doing what I want; Try stepping thru code and write back with results.
Someone can help from there, if there is a problem//

I tried putting "Else" in but it doesn't recognize it as an "IF" statement. I will try the F8 suggestion. Thanks.
 
Upvote 0
RESOLVED

I used a case select & ran a section of the code selected by a countif in the sheet. Basically I added a countif to count if the serial number already appeared in the selection. if it found one it deleted it, if it found none it entered it into the grid.

This suggestion was a reworking of a solution to an earlier problem that i found in my user CP

God I love this site

Here's the code

Code:
Sub tester()
With Sheets("X")

Select Case Range("A448")
  Case 1
'if serial numbers match do the stuff below
    Set F = .Columns("C").Find(What:=.Range("D448").Value, LookIn:=xlValues, LookAt:=xlWhole)
    If Not F Is Nothing Then F.EntireRow.ClearContents
    .Range("A442:F445").Sort Key1:=Range("A442"), Order1:=xlAscending, Header:=xlGuess _
        , OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
        Range("F438:F440").Select
    Selection.Copy
    Range("F442").Select
    ActiveSheet.Paste

    Case 0
    .Range("B448:G448").Copy
    .Range("A445").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    .Range("A442:F445").Sort Key1:=Range("A442"), Order1:=xlAscending, Header:=xlGuess _
        , OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
    Range("F438:F440").Select
    Selection.Copy
    Range("F442").Select
    ActiveSheet.Paste
   
   Case Else
      MsgBox ("Houston, we have a problem!")
End Select
End With
End Sub

Cheers guys
 
Upvote 0

Forum statistics

Threads
1,224,596
Messages
6,179,807
Members
452,944
Latest member
2558216095

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