+ Reply to Thread
Results 1 to 4 of 4

Code to check if field already contains data ...???

Hybrid View

  1. #1
    Registered User
    Join Date
    11-15-2011
    Location
    Scotland
    MS-Off Ver
    Excel 2007
    Posts
    19

    Smile Code to check if field already contains data ...???

    Evening All,

    Hope someone can help me with the code below.

    I am trying to change the code below so that it has the following rules :- if the sheet is empty then update with the new HEADER else if the 1st cell of the sheet is not empty (already contains a header value) then go to the next available blank row, and activate that cell.

    Below is my own attempt ( I'm just new to excel VBA) but it doesn't appear to work.....

    Any help is much appreciated.

    Thanks

    Public Sub InOut_Header()
    If Parking_Survey.surveySheet.Cells(1, "A") = "" Then
        Parking_Survey.surveySheet.Cells(1, "A").Value = "Registration"
        Parking_Survey.surveySheet.Cells(1, "B").Value = "Time"
        Parking_Survey.surveySheet.Cells(1, "C").Value = "Class"
        Parking_Survey.surveySheet.Cells(1, "D").Value = "Type"
    Else
        Dim Lastrow As Long
        Lastrow = Cells(Rows.Count, 1).End(xlUp).Row
        End If
    End Sub
    Last edited by jeffreybrown; 09-02-2013 at 04:53 PM. Reason: As per Forum Rule #3, please use code tags…Thanks.

  2. #2
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Code to check if field already contains data ...???

    Last blank row or last row in the data ?

  3. #3
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Code to check if field already contains data ...???

    See if this helps

    Public Sub InOut_Header()
    
    Dim Lastrow As Long
    Lastrow = Cells(Rows.Count, 1).End(xlUp).Row
    
    With Sheets("surveySheet")
        If .Range("A1") = vbNullString Then
        
            .Range("A1").Resize(1, 4).Value = Array("Registration", _
                                                    "Time", _
                                                    "Class", _
                                                    "Type")
        Else
            .Range("A" & Lastrow + 1).Activate
        End If
            
    End With
    
    End Sub
    Thanks

  4. #4
    Registered User
    Join Date
    11-15-2011
    Location
    Scotland
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Code to check if field already contains data ...???

    Hi fredlo2010,

    thanks for your reply.

    To answer your first question, if there is a value in the 1st cell I would like the system to go to the next available blank row.

    With regards to the coding you have provided, i am getting a " RUNTIME ERROR 9, SUBSCRIPT IS OUT OF RANGE "

    When I go onto the debug code it step to the following line :- With Sheets("surveySheet")

    Hope you can assist.

    Thanks

+ 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. [SOLVED] vba code to be correct and check ,vba code to brig data for the same refreince
    By funtastic in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-25-2012, 02:29 PM
  2. Replies: 2
    Last Post: 06-26-2012, 05:09 PM
  3. Check each row, if each proceeding row matches, take data and put in required field t
    By mattsmithnl in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-08-2011, 03:27 PM
  4. Replies: 1
    Last Post: 12-12-2008, 11:38 AM
  5. vba code to create a list from data in one field
    By short_n_curly in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-10-2007, 11:16 AM

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