+ Reply to Thread
Results 1 to 4 of 4

Need to check is cells are empty before pasting

Hybrid View

  1. #1
    Registered User
    Join Date
    01-23-2015
    Location
    Dublin
    MS-Off Ver
    2010
    Posts
    10

    Need to check is cells are empty before pasting

    Hi,

    I have written a simple copy&paste macro.

    I was wondering if anyone had any suggestions for how to check if there is no data on the row before pasting? (Seeing as there is no undo)

    I guess I need and If IsEmpty type function and Else then give an error.


    Sub Update_Month()

    ' Update the fills for the specific day

    Dim number As Integer
    Dim row_number As Integer

    'enter number in day box at B8, then this will paste to the correct place on sheet

    row_number = Sheets("Data").Range("B8").Value + 3

    'I need an if statement for here I think that looks at the row entered (row_number) and checks that it is empty or zero from column 2 to 500

    Sheets("Data").Select
    Range("A5:HD5").Select
    Selection.Copy
    Sheets("Current").Select
    Cells(row_number, 2).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

    'Else an error message

    End Sub


    Could anyone help me on this? Particularly in using my row_number to look across a range.

    Thanks

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Need to check is cells are empty before pasting

    Do you mean like this?

    If Len(Sheets("Data").Range("B8").Value) = 0 Then
        'empty value
        row_number = 2 'or anything you want to do in this situation
    Else
        row_number = Sheets("Data").Range("B8").Value + 3
    End If

  3. #3
    Registered User
    Join Date
    01-23-2015
    Location
    Dublin
    MS-Off Ver
    2010
    Posts
    10

    Re: Need to check is cells are empty before pasting

    Hi,

    Thank you for your offer of help, but this is not what I am really looking for.

    For example if value 12 is put in the day box (Sheets("Data").Range("B8").Value) , then data gets pasted to Sheets("Current").Cells(row_number, 2)

    I need the macro to look across row_number and from columns B - GG. To see if these are empty

    The issue is I don't know how to define this range using row_number and get it to check that there is no data before pasting

    In this example it would be Range (15B:15GG). But I need the row to change with row_number.

    Thanks

  4. #4
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Need to check is cells are empty before pasting

    This is still clear as mud for me. Maybe this will explain things for you

    Sub Example()
    Dim row_number As Long, num_blanks As Long
    Dim rng As Range
    
    'in this situation Range B8 is 12
    row_number = Sheets("Data").Range("B8").Value + 3
    
    'this will resize your range to encorporate columns B to GG for that given row_number
    Set rng = Sheets("Current").Cells(row_number, 2).Resize(1, 188)
    
    'proof it works
    MsgBox rng.Address
    
    
    'to see if there is at least 1 empty cell in rng
    num_blanks = Application.WorksheetFunction.CountBlank(rng)
    
    'proof it works
    If Application.WorksheetFunction.CountBlank(rng) > 0 Then
        MsgBox ("There are " & num_blanks & " blank cells in Range" & rng.Address)
    Else
        MsgBox ("There are no blanks in Range" & rng.Address)
    End If
     
    End Sub
    Last edited by stnkynts; 02-05-2015 at 05:16 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Need to check if cells empty before pasting
    By karlosdejackal in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-05-2015, 03:20 PM
  2. Empty cells not evaluating as empty - multiple check methods tried
    By rafadavidc in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 02-03-2014, 05:54 PM
  3. pasting values into EMPTY cells
    By jonny9781 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-16-2008, 07:44 AM
  4. Check for empty cells but ignoring value ZERO
    By munkayboi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-11-2007, 02:10 PM
  5. check for non-empty cells outside of a certain range
    By Abe in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-28-2006, 01:50 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1