Brent
asked on
Copy and Paste without the empty Cells Part 2
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.SpecialCel ls(xlCellT ypeConstan ts), _
Selection.SpecialCells(xlC ellTypeFor mulas))
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(xlC ellTypeCon stants)
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(xlC ellTypeCon stants)
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
Sub SelectNonBlanks()
Dim rngNoBlank As Range
Set rngNoBlank = Union(Selection.SpecialCel
Selection.SpecialCells(xlC
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(xlC
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(xlC
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You're welcome. Glad to help. If you need additional explanation let me know.
Kyle
Kyle
ASKER
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(xlC ellTypeCon stants)
Set rCopyTo = sht.Cells(Rows.Count, 1).End(xlUp).Offset(1)
rngNoBlank.Copy rCopyTo
ws.Columns.AutoFit
sht.Range(rCopyTo, rCopyTo.End(xlToRight)).Co py
End Sub
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(xlC
Set rCopyTo = sht.Cells(Rows.Count, 1).End(xlUp).Offset(1)
rngNoBlank.Copy rCopyTo
ws.Columns.AutoFit
sht.Range(rCopyTo, rCopyTo.End(xlToRight)).Co
End Sub
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.AutoFi t
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
sht.Columns.AutoFit
If you are trying to autofit the columns of the worksheet from which you are copying then do...
Activesheet.Columns.AutoFi
If you only want to autofit a few columns and not every column in the worksheet then do...
Activesheet.Columns("A:C")
Make sense?
Kyle
ASKER
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(xlC ellTypeCon stants)
Set rCopyTo = sht.Cells(Rows.Count, 1).End(xlUp).Offset(1)
rngNoBlank.Copy rCopyTo
ActiveSheet.Columns.AutoFi t
sht.Range(rCopyTo, rCopyTo.End(xlToRight)).Co py
End Sub
let me try it.
thanks
Sub SelectNonBlanks()
Dim rngNoBlank As Range, sht As Worksheet, rCopyTo As Range
Set sht = Sheets("Hidden")
Set rngNoBlank = Selection.SpecialCells(xlC
Set rCopyTo = sht.Cells(Rows.Count, 1).End(xlUp).Offset(1)
rngNoBlank.Copy rCopyTo
ActiveSheet.Columns.AutoFi
sht.Range(rCopyTo, rCopyTo.End(xlToRight)).Co
End Sub
let me try it.
thanks
ASKER
Okay. Got it. I want the hidden sheet, so
sht.Columns.AutoFit
Funny, I was trying the ActiveSheet.columns.AutoFi t 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.
sht.Columns.AutoFit
Funny, I was trying the ActiveSheet.columns.AutoFi
Thanks for you help and patience.
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
Kyle
ASKER
Thank you!