Adding an If statement into a Loop

JeremyA1976

Board Regular
Joined
Aug 3, 2015
Messages
59
I have the following code that updates my inventory sheet with new totals. The problem is, when I enter something in that does not show up on the inventory sheet, I get an error message and when I debug, it points to the section of code shown below. My question is how to add an if statement to the loop that would check to see if H2 = #N/A, and if it does, to have it skip to deleting the row instead of searching the inventory (basically, if its miscellaneous material, and not on inventory, it just deletes row 2 from the LOGCOUNT sheet and moves on with the code)
Is this possible?

Code:
Sub ProcessOut()

    Dim wb1 As Workbook
    Dim wb2 As Workbook
    Dim wb3 As Workbook
    
    Set wb1 = Workbooks("Charge Out Template.xlsm")
    Set wb2 = Workbooks("Charge Out Log.xlsm")
    Set wb3 = Workbooks("Inventory Sheet.xlsm")
    
    
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim ws3 As Worksheet
    Dim ws4 As Worksheet
    
    Set ws1 = wb1.Sheets("CHARGE OUT FORM")
    Set ws2 = wb2.Sheets("CHARGE OUT LOG")
    Set ws3 = wb3.Sheets("INVENTORY")
    Set ws4 = wb1.Sheets("LOGCOUNT")
    Set ws5 = wb3.Sheets("Inventory Backup")

'more code above but not part of issue

'COPY FROM TEMPLATE "LOGCOUNT" TO INVENTORY
'FIRST ROW ENTER START
'LOOP
    Do While Len(ws4.Range("A2").Value) > 0
        
        ws4.Activate
        ActiveSheet.Range("G2").Select
        Selection.Copy
    
        ws3.Activate
        ActiveSheet.Range("AC1").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    
        ws4.Activate
        ActiveSheet.Range("H2").Select
        Selection.Copy
        
        ws3.Activate
        ActiveSheet.Range(Range("AC1").Value).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    
        ws4.Activate
        ActiveSheet.Rows([2]).EntireRow.Delete
        
    Loop

'there is more code below, but has nothing to do with the problem

End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Maybe
Code:
Do While Len(ws4.Range("A2").Value) > 0
        
   ws4.Range("G2").Copy
   
   ws3.Range("AC1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
      :=False, Transpose:=False
   
   If Not IsError(ws4.Range("H2")) Then
      ws4.Range("H2").Copy
      ws3.Range(Range("AC1").Value).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
         :=False, Transpose:=False
   End If
   
   ws4.Rows(2).EntireRow.Delete

Loop
 
Upvote 0
Im sorry Fluff. I celebrated too early on that one. When I ran a test to see if miscellaneous items would go through, it did and I forgot to run a test on items that are on inventory. When i do, it gives me an "error 1004, Method 'Range' of object'_Workseet' failed"
The line of code it highlights in yellow is this one:

Code:
[COLOR=#333333]ws3.Range(Range("AC1").Value).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
[/COLOR][COLOR=#333333]         :=False, Transpose:=False[/COLOR]

Any idea on what is happening here?
 
Upvote 0
Try
Code:
ws3.Range([COLOR=#ff0000]ws3.[/COLOR]Range("AC1").Value).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
         :=False, Transpose:=False
 
Upvote 0

Forum statistics

Threads
1,215,339
Messages
6,124,381
Members
449,155
Latest member
ravioli44

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