Add if condition to my macro

ansvk1

Board Regular
Joined
Oct 6, 2017
Messages
82
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hi there,
I have a macro that (created with help from this form) worked fine for what i intend it to do, but later on i came to find that it needs to have a check to be performed on a cell value being empty or not before running the whole program. So, now when i tried to insert an IF condition its not taking it...being new to vba coding i do not understand what or why its saying that error message. Can someone help me write this piece of code, correctly?

thanks in advance!

Here is my code:
Sub Populating_Summary_Sheet()


Application.ScreenUpdating = False

Dim s1 As Excel.Worksheet
Dim s2 As Excel.Worksheet
Dim iLastCellS2 As Excel.Range
Dim iLastRowS1 As Long
Dim Cnt As Long
Dim LstSht As String

LstSht = InputBox("Please enter the last sheet number, like 103")
If LstSht = "" Then Exit Sub
Set s2 = Sheets("Summary")

For Cnt = 2 To CLng(LstSht)
Set s1 = Sheets(CStr(Cnt))

If Range("C28") = "" Then GoTo JUMP
Else ' -->do the below, and i unable to get this done right.

iLastRowS1 = s1.Cells(s1.Rows.Count, "C").End(xlUp).Row

Set iLastCellS2 = s2.Cells(s2.Rows.Count, "N").End(xlUp).Offset(1, 0)

s1.Range("B28:C" & iLastRowS1).Copy
iLastCellS2.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False

s1.Range("F28:H" & iLastRowS1).Copy
iLastCellS2.Offset(0, 5).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False

JUMP:
Next Cnt
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Re: how to add if condition to my macro

Try this
Code:
Sub DelData()
    For Cnt = 2 To CLng(LstSht)
    Set s1 = Sheets(CStr(Cnt))
    
    If s1.Range("C28") <> "" Then
    
        iLastRowS1 = s1.Cells(s1.Rows.Count, "C").End(xlUp).Row
        
        Set iLastCellS2 = s2.Cells(s2.Rows.Count, "N").End(xlUp).Offset(1, 0)
        
        s1.Range("B28:C" & iLastRowS1).Copy
        iLastCellS2.Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False
        
        s1.Range("F28:H" & iLastRowS1).Copy
        iLastCellS2.Offset(0, 5).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False
    End If
    
JUMP:
    Next Cnt
 
Upvote 0
Re: how to add if condition to my macro

You are using the one line form of IF so you do not need the else.
 
Upvote 0
Re: how to add if condition to my macro

Try:
Rich (BB code):
Sub Popluate_Summary()
    
    Dim w       As Long
    Dim LRw     As Long
    Dim LRs     As Long
    Dim wksSum  As Worksheet
    
    Set wksSum = Sheets("Summary")
    
    Application.ScreenUpdating = False
    
    For w = 2 To Worksheets.count
        LRs = wksSum.Cells(Rows.count, 14).End(xlUp).row + 1
        With Sheets(w)
            If Len(.Cells(28, 3).Value) > 0 Then
                LRw = .Cells(.Rows.count, 3).End(xlUp).row - 27
                wksSum.Cells(LRs, 14).Resize(LRw, 2).Value = .Cells(28, 2).Resize(LRw, 2).Value
                wksSum.Cells(LRs, 19).Resize(LRw, 3).Value = .Cells(28, 6).Resize(LRw, 3).Value
            End With
        End With
    Next w
    
    Application.ScreenUpdating = False
    
    Set wksSum = Nothing
    
End Sub
 
Last edited:
Upvote 0
Re: how to add if condition to my macro

thanks for the help guys! i was able to make it work using Fluff's suggestion and learnt something new from Scott's response. Also thank Jack for your response!
 
Upvote 0
Re: how to add if condition to my macro

Glad we could help & thanks for the feedback
 
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