Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-05-2023, 12:45 AM
soroush.kalantari soroush.kalantari is offline Error when pasting to just visible cells Windows 10 Error when pasting to just visible cells Office 2016
Competent Performer
Error when pasting to just visible cells
 
Join Date: Jun 2021
Posts: 115
soroush.kalantari is on a distinguished road
Default Error when pasting to just visible cells

have written following macro to paste copied data to just visible cells. It fails to work for the cells which are immediately after a hidden cell. (See the attachment, if I copy range A1:c1 from “ rowdata sheet” and then select range c1:f1 from “pastdata”sheet, when running the code, I expect c1 value of “ rowdata sheet” sheet be pasted to f1 in “pastdata” sheet but it is pasted on c6 in the “pastdata”sheet instead.
Can you guide me on this issue?

Sub pasttojustvisblecells()
Application.DisplayAlerts = False
Dim ws As Worksheet
Dim rng As Range
Dim rng1 As Range
Dim n As Integer
Dim i As Integer

Dim ws1 As Worksheet
Set ws = ActiveSheet

Set rng = Selection.SpecialCells(xlCellTypeVisible)
n = rng.SpecialCells(xlCellTypeVisible).Cells.Count
MsgBox rng.Address


Sheets.Add After:=ActiveSheet
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Set rng1 = Selection
Set ws1 = ActiveSheet


For i = 1 To n
ws1.Activate
rng1.Cells(i).Select
Selection.Copy
ws.Activate


rng.Cells(i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

Next i
ws1.Activate
ActiveSheet.Delete
ws.Activate
Application.DisplayAlerts = True
End Sub
Attached Files
File Type: xlsm pasttojustvisblecells.xlsm (34.1 KB, 1 views)
Reply With Quote
  #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: 871
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, 3 views)
Reply With Quote
  #3  
Old 08-05-2023, 08:09 PM
soroush.kalantari soroush.kalantari is offline Error when pasting to just visible cells Windows 10 Error when pasting to just visible cells Office 2016
Competent Performer
Error when pasting to just visible cells
 
Join Date: Jun 2021
Posts: 115
soroush.kalantari is on a distinguished road
Default

Quote:
Originally Posted by p45cal View Post
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
Thank you for your complete and constructive answer which solved my problem. I added a new worksheet because without this, I didn’t know how to set rng1. Do you know a way to use copied data in this code without adding new worksheet? Can you tell me why the line “rng.Cells(i).Select” in my code failed? (these questions are just for improving my understanding of vba)
Reply With Quote
  #4  
Old 08-06-2023, 06:55 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: 871
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

Quote:
Originally Posted by soroush.kalantari View Post
I added a new worksheet because without this, I didn’t know how to set rng1. Do you know a way to use copied data in this code without adding new worksheet?
OK, I understand. It's fine.
Quote:
Originally Posted by soroush.kalantari View Post
Can you tell me why the line “rng.Cells(i).Select” in my code failed? (these questions are just for improving my understanding of vba)
Since rng is a noncontiguous range (as you could see with your MsgBox rng.Address), it has multiple areas (each of these areas is separated by a comma in the .Address), but when you use rng.cells(i) it in fact only refers to area 1, and when that area is too small, say it's got only 2 cells, then when you say rng.cells(3) it refers to a different but clearly defined cell; to demonstrate:
execute this line:
Code:
range("H10:I10, K10").Select
the green cells will be selected, then execute:
Code:
selection.cells(8).select
the cell with the label 8 will be selected where I've inserted the numbering system:
2023-08-06_144659.jpg
Using For each cll in rng.cells does iterate properly through a noncontiguous range.


Note that with the new code, it does matter how you select the destination range; you will get a different result if you start at the left when dragging, from if you start at the right, as the MsgBox rng.Address will show!
Reply With Quote
  #5  
Old 08-06-2023, 08:15 PM
soroush.kalantari soroush.kalantari is offline Error when pasting to just visible cells Windows 10 Error when pasting to just visible cells Office 2016
Competent Performer
Error when pasting to just visible cells
 
Join Date: Jun 2021
Posts: 115
soroush.kalantari is on a distinguished road
Default

Ok. I got it. Thank you very much.
Reply With Quote
Reply

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 12:24 AM.


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