VBA Paste Error (because "Nothing" is copied)

gerald24

Board Regular
Joined
Apr 28, 2017
Messages
95
HTML:
Sub FT()

    Dim InsType As Range
    Dim Rng As Range
    

    Sheets("Computation").Select
    
   For Each InsType In Range("H2", Range("H" & Rows.Count).End(xlUp))
            If InsType.Value = "Cross Rate" Or InsType.Value = "Currency" And InStr(InsType.Offset(0, 8).Value, "Yes") Then
                If Rng Is Nothing Then
                    Set Rng = InsType.Offset(0, -6)
                Else
                    Set Rng = Union(Rng, InsType.Offset(0, -6))
                End If
            End If
    Next InsType
            If Not Rng Is Nothing Then Rng.Copy
            Sheets("Load to VA").Select
            Range("A3").Select
            ActiveCell.PasteSpecial xlPasteValues
            Selection.NumberFormat = "General"
                        
End Sub


I think the error is because Rng is = nothing, I don't know why but what I want to happen is if Rng is nothing, it should not do anything.

advance Thanks!!!!
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi gerald24,

The issue is around the way you've constructed the If statement. This should do the job:

Code:
Option Explicit
Sub FT()

    Dim InsType As Range
    Dim Rng As Range

    Sheets("Computation").Select
    
   For Each InsType In Range("H2", Range("H" & Rows.Count).End(xlUp))
            If InsType.Value = "Cross Rate" Or InsType.Value = "Currency" And InStr(InsType.Offset(0, 8).Value, "Yes") Then
                If Rng Is Nothing Then
                    Set Rng = InsType.Offset(0, -6)
                Else
                    Set Rng = Union(Rng, InsType.Offset(0, -6))
                End If
            End If
    Next InsType
    If Not Rng Is Nothing Then
        Rng.Copy
        Sheets("Load to VA").Select
        Range("A3").Select
        ActiveCell.PasteSpecial xlPasteValues
        Selection.NumberFormat = "General"
    End If
                        
End Sub

Regards,

Robert
 
Upvote 0
Hi Robert,

I think the "IF NOT" Statement does not need "End If". An error says "Compile Error: End If without block if"

The actual error I am getting (without the End If) is "Run-time error '1004': PasteSpecial Method of Range class failed" which I think is due to the code is doing the paste function even thought there's nothing copied.
 
Last edited by a moderator:
Upvote 0
I think the "IF NOT" Statement does not need "End If".

The way you've written it doesn't but if step through your code (by pressing F8) you'll see what I mean. Your code will go straight to the Sheets("Load to VA").Select line if the Rng variable is nothing (i.e. hasn't been set) which is incorrect.

An error says "Compile Error: End If without block if"

That's odd. I don't get that error :confused:

The actual error I am getting (without the End If) is "Run-time error '1004': PasteSpecial Method of Range class failed" which I think is due to the code is doing the paste function even thought there's nothing copied.

Correct - see my first point.
 
Upvote 0
Hi Robert,

Your code worked perfectly. Could you education me on what you have tweaked in my codes? Also what is "Option Explicit" fore?

Thank you!
 
Upvote 0
Your code worked perfectly.

You're welcome. Thanks for like :)

Could you education me on what you have tweaked in my codes?

All I did was change the way your IF statement was structured to the standard logic test, value if true, value if false (which there was nothing to do).

Option Explicit

This forces you to declare all your variables. If you don't use it any variables that are not declared are assigned as a variant - the most expensive type of variable in programming terms.

HTH

Robert
 
Upvote 0

Forum statistics

Threads
1,214,821
Messages
6,121,755
Members
449,049
Latest member
excelknuckles

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