Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 08-05-2023, 10:14 AM
p45cal's Avatar
p45cal p45cal is offline Error when pasting to just visible cells Windows 10 Error when pasting to just visible cells Office 2021
Expert
 
Join Date: Apr 2014
Posts: 962
p45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond repute
Default

See attached and comments in code.
Code:
Sub pasttojustvisblecells()
Application.DisplayAlerts = False
Dim AtLeast1CellVisible, ws As Worksheet, rng As Range, rng1 As Range, cll, i As Long

Dim ws1 As Worksheet
Set ws = ActiveSheet
Set rng = Selection
'This next section is to cater for when only one cell is selected on the destination sheet; if only one cell is selected
'.SpecialCells includes all the visible cells on the entire worksheet!
'*********************
For Each cll In rng.Cells 'this loop ensures at least one cell in the selection is visible.
  If Not (cll.EntireColumn.Hidden Or cll.EntireRow.Hidden) Then
    AtLeast1CellVisible = True
    Exit For
  End If
Next cll
'*********************

If AtLeast1CellVisible Then
  If rng.Cells.Count = 1 Then Set rng = Selection Else Set rng = rng.SpecialCells(xlCellTypeVisible)
'  MsgBox rng.Address
  Set ws1 = Sheets.Add ' After:=ActiveSheet 'I'm not sure why you do this…
  ws1.Cells(1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
  Set rng1 = Selection

  i = 0
  For Each cll In rng
    i = i + 1
    cll.Value = rng1.Cells(i).Value
  Next cll

  ws1.Delete
  'ws.Activate 'shouldn't need this
Else
  MsgBox "No visible cells selected to paste to" 'this could happen if you hid all selected cells.
End If
Application.DisplayAlerts = True
End Sub
Attached Files
File Type: xlsm msofficeforums51190pasttojustvisblecells.xlsm (28.3 KB, 5 views)
Reply With Quote
 

Tags
hidden, paste



Similar Threads
Thread Thread Starter Forum Replies Last Post
Error when pasting to just visible cells error handler: inserting building blocks when multiple cells in a table are selected creates error LSLSLS Word VBA 2 01-07-2022 05:46 AM
for visible cells (filtered), how find value lowest than 4.00 and return the cell number murfy69 Excel 6 08-03-2017 12:03 AM
VBA color of visible gridlines of tables word (with some merged cells in first rows) Alvaro.passi Word VBA 0 07-18-2017 09:11 AM
Losing rows when pasting split cells Gitley Word Tables 1 01-15-2013 07:49 AM
In Excel 2007-After Selecting Visibe Cells-How do I "Copy to Visible cells" Only mag Excel 0 10-28-2012 08:04 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:07 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft