Solved

Excel Macro to loop thru code the finds cuts and pastes

Posted on 2015-01-08
6
101 Views
Last Modified: 2015-01-09
I have a macro I am working on.  I have found a way do the find, cut and paste, but it does not loop,  It stops after the first  find, cut and paste.

When there are blanks in K there will be data data sitting in L that needs to be moved to P.  See the attachment please.
snip of excel sheet

Below is the code I used to find a blank K to then cut L to paste in P, but it only works for the first line.  I need it to loop through a variable length sheet.  A column that will be full of data until the end of the sheet if needed to reference an end is column C.  

Range("K:K").Select
 Selection.SpecialCells(xlCellTypeBlanks).Select
ActiveCell.Offset(0, 1).Select
    Selection.Cut
    ActiveCell.Offset(0, 4).Select
    ActiveSheet.Paste
0
Comment
Question by:jenit
  • 3
  • 2
6 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 40538854
Perhaps.
Dim rngBlanks As Range
Dim cl As Range

    Set rngBlanks = Range("K:K").SpecialCells(xlCellTypeBlanks).Select
    
    For Each cl In rngBlanks.Cells
        cl.Offset(0, 1).Cut cl.Offset(0, 5)
    Next cl

Open in new window

0
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 40539603
Hi,

I would put a limit to the range

Dim rngBlanks As Range
Dim cl As Range

    Set rngBlanks = Range("K1:K"& Range("L"& Cells.Rows.count).End(xlUp).Row).SpecialCells(xlCellTypeBlanks)
    
    For Each cl In rngBlanks.Cells
        cl.Offset(0, 1).Cut cl.Offset(0, 5)
    Next cl

Open in new window

Regards
0
 
LVL 33

Expert Comment

by:Norie
ID: 40539628
There's no real need to add code to limit the range, when you use Special cells automatically does it.

For example, if you have data from K1:K10 and cells K3, K6 and K8 are blank this Range("K:K").SpecialCells(xlCellTypeBlanks) will only return K3, K6 and K8.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 40539698
Right I have been deceived by the file I used

But the select at the set instruction gives an error

then try

Dim rngBlanks As Range
Dim cl As Range

    Set rngBlanks = Range("K:K").SpecialCells(xlCellTypeBlanks)
   
    For Each cl In rngBlanks.Cells
        cl.Offset(0, 1).Cut cl.Offset(0, 5)
    Next cl
0
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
ID: 40539713
Oops, that's a typo left over from the original code.:)

Should be this.
Dim rngBlanks As Range
Dim cl As Range

    Set rngBlanks = Range("K:K").SpecialCells(xlCellTypeBlanks)
    
    For Each cl In rngBlanks.Cells
        cl.Offset(0, 1).Cut cl.Offset(0, 5)
    Next cl

Open in new window

0
 

Author Closing Comment

by:jenit
ID: 40540103
Thanks!!!  That did the trick!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now