+ Reply to Thread
Results 1 to 5 of 5

Copying cells till blank rows end

Hybrid View

  1. #1
    Forum Contributor SOS's Avatar
    Join Date
    01-26-2004
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel 2003
    Posts
    327

    Copying cells till blank rows end

    Hi all,

    I have data in A2, then blank till A18 (variable number each time), then data in A19 and blank till A35 (and more and more).

    Using VBA how would I code to copy A1 into every cell till A18, then copy A19 into every cell till A35 and so on.

    Perhaps using SpecialCells(xlTypeConstant) or something?

    Thanks in adance

    Seamus

  2. #2
    Forum Contributor SOS's Avatar
    Join Date
    01-26-2004
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel 2003
    Posts
    327
    Should have done some more searching before posting - I found the answer.

    Sub FillColBlanks()
    Dim wks As Worksheet
    Dim rng As Range
    Dim LastRow As Long
    Dim col As Long
    
    Set wks = ActiveSheet
    With wks
       col = ActiveCell.Column
       Set rng = .UsedRange
       LastRow = .Cells.SpecialCells(xlCellTypeLastCell).Row
       Set rng = Nothing
       On Error Resume Next
       Set rng = .Range(.Cells(2, col), .Cells(LastRow, col)) _
                      .Cells.SpecialCells(xlCellTypeBlanks)
       On Error GoTo 0
       If rng Is Nothing Then
           MsgBox "No blanks found"
           Exit Sub
       Else
           rng.FormulaR1C1 = "=R[-1]C"
       End If
       With .Cells(1, col).EntireColumn
           .Value = .Value
       End With
    End With
    End Sub
    Hope this helps

    Seamus

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Why not adapt the code that I gave you earlier?

    Dim rng   As Range
        Dim cl     As Range
    
        Set rng = Range(Cells(2, 1), Cells(Rows.Count, 1).End(xlUp))
        For Each cl In rng
            If Not IsEmpty(cl) Then cl.Offset(1, 0).Value = cl.Offset(-1, 0).Value
        Next cl
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Forum Contributor SOS's Avatar
    Join Date
    01-26-2004
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel 2003
    Posts
    327
    Roy,

    That code appears to say:

    If A2 is not empty then A3's value = A1's Value.

    So if I have data in A2 and want it copied down to A10 your code takes A1's value (header cell) and places it in A3, then takes A2's value (hospital name) and places it in A4 and so on.

    Sorry

    Seamus

  5. #5
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650
    Hi Here's an alternative
    Dim Last As Integer, oData, Dn As Integer
    Last = Range("A" & Rows.Count).End(xlUp).Row
    
    For Dn = 1 To Last
            oData = Cells(Dn, "A")
        Do While Cells(Dn, "A") = "" Or Cells(Dn, "A") = oData
            Cells(Dn, "A") = oData
                Dn = Dn + 1
                    If Dn > Last Then Exit Sub
             Loop
        
        oData = Cells(Dn, "A")
        Dn = Dn - 1
    Next Dn
    Regards Mick

+ 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. Count including Blank Cells
    By bhofsetz in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 02-17-2015, 02:11 PM
  2. Exclude Blank cells when matching against other cells.
    By Neil07979 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-05-2008, 09:28 AM
  3. [SOLVED] Compressing data or Trimming Blank Rows
    By all4excel in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-07-2008, 07:28 AM
  4. Repeat data in blank cells
    By mackey in forum Excel General
    Replies: 2
    Last Post: 02-06-2008, 09:18 AM
  5. Excel doesn't think blank cells are not really blank?
    By leonnord in forum Excel General
    Replies: 3
    Last Post: 08-01-2007, 02:34 PM

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