VBA - Skip blank cells in loop

jastisid

New Member
Joined
Aug 8, 2020
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi there,

I have an excel template where I use the loop function to run several simulations and paste the results in an output sheet, and I use the following code. However, I have a situation now where in some iteration one or two rows can be blank, is there anyway to skip the blank rows while running the loop function, such that in the output sheet only rows with value are pasted?

Any help is much appreciated

Sub Output()
Application.ScreenUpdating = False

[ECL_Consol].ClearContents

Dim Temp1 As Integer
Dim i As Integer

Temp1 = Range("ID").Value

For i = 1 To Range("Num_Sims").Value

[ECL_Simulations].Copy
Range("ID").Value = i

Sheets("ECL output - per Loan basis").Select
Sheets("ECL output - per Loan basis").Cells(13 + i, 2).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.StatusBar = ([Num_Sims] - i)


Next i

End Sub
 
We can return to the original Copy, the detail was to find the last row with data in the "Output" sheet
Try this:

VBA Code:
Sub Output()
  Dim Temp1 As Integer
  Dim i As Integer, lr As Long
  
  Application.ScreenUpdating = False
  
  [Output_Consol].ClearContents
  lr = 14
  Temp1 = Range("ID").Value
  
  For i = 1 To Range("Num_Sims").Value
    [Sample_Simulations].Copy
    Range("ID").Value = i
    Sheets("Output").Range("C" & lr).PasteSpecial xlPasteValues, , True
    lr = Sheets("Output").Range("C:C").Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row + 1
    Application.StatusBar = ([Num_Sims] - i)
  Next i
  
  Application.StatusBar = False
  Application.CutCopyMode = False
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Thanks you very much, that resolves my query, and the code works perfectly fine. Thanks again and appreciate you time!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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