Solved

Excel Macro to loop thru code the finds cuts and pastes

Posted on 2015-01-08
6
107 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 34

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 51

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 34

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 51

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 34

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

728 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