Solved

Copy and Paste without the empty Cells Part 2

Posted on 2012-04-05
8
248 Views
Last Modified: 2012-04-05
I asked a question yesterday about copying non-blank cells from a single row which I manually highlight prior to running the macro. Kyle responded with the below macro:

Sub SelectNonBlanks()
Dim rngNoBlank As Range
Set rngNoBlank = Union(Selection.SpecialCells(xlCellTypeConstants), _
                        Selection.SpecialCells(xlCellTypeFormulas))
rngNoBlank.Select
End Sub

         
After reading last night, I modified it because I don't need the formulas


Sub SelectNonBlanks()
Dim rngNoBlank As Range
Set rngNoBlank = Selection.SpecialCells(xlCellTypeConstants)
rngNoBlank.Copy
End Sub

After trial and error I figured out works perfect in excel, but when I paste into the master calendar I am still having issues. So my thought process (which I am still very new at coding), is instead directly pasting into the master calendar, that I take my copied selection and paste it into a hidden worksheet. Now that it is pasted correctly, the marco would copy that selection and I could manually paste into the master calendar.
 Example
Sub SelectNonBlanks()
Dim rngNoBlank As Range
Set rngNoBlank = Selection.SpecialCells(xlCellTypeConstants)
rngNoBlank.Copy
' paste into a hidden worksheet which would include finding the last row, paste underneath it.
'The hidden worksheet could simply just "grow" and I won't need to worry about deleting previous copied rows.
'Then, copy the new selection.
End Sub

Then I could paste manually into the master calendar. I realize this might be a little sloppy, but it is what I could come up with limited knowledge. I would manually highlight the initial selection and it would always be just one row. There is only one person using this, so it does not have to be 100% bullet proof. If there is an error, it can easily be addressed.

Thanks
Brentexpert-exchange-copy-and-paste--.xls
0
Comment
Question by:bvanscoy678
  • 4
  • 4
8 Comments
 
LVL 12

Accepted Solution

by:
kgerb earned 500 total points
ID: 37811689
Try this.  I think it will do what you want.

Kyle
Sub SelectNonBlanks()
Dim rngNoBlank As Range, sht As Worksheet, rCopyTo As Range
Set sht = Sheets("Hidden")
Set rngNoBlank = Selection.SpecialCells(xlCellTypeConstants)
Set rCopyTo = sht.Cells(Rows.Count, 1).End(xlUp).Offset(1)
rngNoBlank.Copy rCopyTo
sht.Range(rCopyTo, rCopyTo.End(xlToRight)).Copy
End Sub

Open in new window

Q-27663808-RevA.xls
0
 

Author Closing Comment

by:bvanscoy678
ID: 37811726
Yes, that worked perfect. I'll go over it and break it down step by step to understand it, but it looks straightforward enough that I'll get it.

Thank you!
0
 
LVL 12

Expert Comment

by:kgerb
ID: 37811734
You're welcome.  Glad to help.  If you need additional explanation let me know.
Kyle
0
 

Author Comment

by:bvanscoy678
ID: 37811843
Last question, promise!

I wanted to autofit the columns. I tried this, but got an error.

Sub SelectNonBlanks()
Dim rngNoBlank As Range, sht As Worksheet, rCopyTo As Range
Set sht = Sheets("Hidden")
Set rngNoBlank = Selection.SpecialCells(xlCellTypeConstants)
Set rCopyTo = sht.Cells(Rows.Count, 1).End(xlUp).Offset(1)
rngNoBlank.Copy rCopyTo
ws.Columns.AutoFit
sht.Range(rCopyTo, rCopyTo.End(xlToRight)).Copy
End Sub
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Expert Comment

by:kgerb
ID: 37811882
Which sheet contains the columns you are trying to Autofit?  You are getting an error b/c you have not defined the variable ws to be a specific worksheet.  If you want to autofit the columns in the hidden worksheet then do...

sht.Columns.AutoFit

If you are trying to autofit the columns of the worksheet from which you are copying then do...

Activesheet.Columns.AutoFit

If you only want to autofit a few columns and not every column in the worksheet then do...

Activesheet.Columns("A:C").AutoFit

Make sense?

Kyle
0
 

Author Comment

by:bvanscoy678
ID: 37811938
yes, the hidden worksheet. I get what you are saying.

Sub SelectNonBlanks()
Dim rngNoBlank As Range, sht As Worksheet, rCopyTo As Range
Set sht = Sheets("Hidden")
Set rngNoBlank = Selection.SpecialCells(xlCellTypeConstants)
Set rCopyTo = sht.Cells(Rows.Count, 1).End(xlUp).Offset(1)
rngNoBlank.Copy rCopyTo
ActiveSheet.Columns.AutoFit
sht.Range(rCopyTo, rCopyTo.End(xlToRight)).Copy
End Sub

let me try it.

thanks
0
 

Author Comment

by:bvanscoy678
ID: 37812087
Okay. Got it. I want the hidden sheet, so
sht.Columns.AutoFit

Funny, I was trying the ActiveSheet.columns.AutoFit and thinking, I can't get it to work. It took me a minute to reread it and think, oh yeah, the hidden sheet is not active!

Thanks for you help and patience.
0
 
LVL 12

Expert Comment

by:kgerb
ID: 37812325
You're welcome.  I'm glad you understand.  You can do many things to ranges not on the active worksheet.  In my classes I spend quite a bit of time explaining this concept.  It's hard for to people to understand sometimes, especially if they are familiar with code recorded by the macro recorder.  Since the recorder is just recording what you do, the code generated always selects an object and then performs an action on the selection.  In reality you can skip the middle man and perform the action directly on the object without ever selecting it.  It's much more efficient to to do it this way.

Kyle
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Question to Pivot table 1 33
Remove row and column 3 45
Moving Excel to AaaS 4 36
Mac Excel column treating text as date 2 27
Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

943 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

6 Experts available now in Live!

Get 1:1 Help Now