Solved

Excel Macro to loop thru code the finds cuts and pastes

Posted on 2015-01-08
6
104 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 50

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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 50

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
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…

830 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