Select - Visible Cells - Excel Office-JS - excel

I'm working a subroutine which does filtering via hiding rows/cols. I realized that the end user if they copy data, will copy the hidden rows.
I'd like to just have my routine finish by having those cells selected, but I tried used rng.select(); w/ both visibleview and specialcells but both error saying select isn't approriate for that object.
var usedrng = ws.getUsedRange(true).getSpecialCells("Visible");
var usedrng = ws.getUsedRange(true).getVisibleView()
I'm resorting to coping the data to a new worksheet, but wondered if there was a way because I saw this quote:
Non-continuous selections are not only not supported in Office.js (for
Word, we DO support them for Excel though)
Update: I found a workaround that I'm happy with, but I'll leave question as my workaround requires turning on filtering which may not be desired. In my case, I wasn't filtering due to needing multiple criteria's more then filter can handle.
My workaround is to use my normal function to hide rows. Then grab getVisibleView and push filter column values to Array. I then unhide rows and apply a filter on values with that array of items.
You can then use getUsedRange(true) and select. When I used CTRL + C only the visible cells were selected as per normal filtering copy/paste SOP. This enables me not to have to copy data to a new WS and leave the existing WS active.

Related

How do I copy a range including hidden cells(by filter)?

I know basically the same questions has previosly been asked and answered on many forums, including stack overflow, but none of the answers satisfy my requirements.
I want to press a button, which will copy a range of around 100 cells. I then want to press a different button, in a totally different workbook (which is in no way connected to the first workbook) and that will paste my copied range.
The issue is that I have a filter in my workbook, which will hide some of the cells in the range. These are not being copied but I need to copy the full range. (The reason I want to copy the full range is because the values need to align when I paste it)
My issue with all other given solutions are:
One solution is to remove the filter when I copy the range. This is not something I want to do as I don't know a way to restore the filter. If this is done before I paste the values, the copied range will "exit?" (it will no longer be copied). And due to the files not being connected, I can't perform any actions with the paste button.
Using a loop to copy the range as a array(Variant), This doesn't work since I can't "transfer" that variant to another workbook which is not connected. Or at least I don't know how to do that.
Is there any other method I can use?
It is a bit of a hack, but because both workbooks have access to the set of custom lists defined for Excel, you could create a custom list with the information needed to identify the source range from the target workbook. The code for the copy button would record the range address, sheet name, and workbook name of the selected range as follows:
Sub copy_range_info()
Application.AddCustomList Array("DeleteMe", Selection.Address, Selection.Parent.Name, Selection.Parent.Parent.Name)
End Sub
On the destination worksheet, the code to paste data would look like this:
Sub paste_range_from_other_workbook()
Dim last_list As Variant
last_list = Application.GetCustomListContents(Application.CustomListCount)
If last_list(1) = "DeleteMe" Then
Workbooks(last_list(4)).Worksheets(last_list(3)).Range(last_list(2)).Copy
ActiveCell.PasteSpecial xlPasteValues
Application.DeleteCustomList Application.CustomListCount
Else
MsgBox "You need to copy a range first using that special button"
End If
End Sub
Because this creates a custom list in Excel that will be permanent, I'm deleting it just before the "else" in the code above. It might be advisable to scan the custom list and delete any lists that begin with "DeleteMe" so if someone does a copy without a corresponding paste, it won't result in more than one custom list of this type.

Setting Print title columns as dynamic ranges

I have 100s of pages of data that I need to print and I want to have a header and a side column on every Page that is printed.
I can do this manually by using the dynamic range that I have created but I have to manually enter the name of the range into the page layout settings. I also tried to use a macro to do this using my dynamic ranges but I get an error message when I run the following script;
Sheets("1. Paste Raw Data").Select
ActiveSheet.PageSetup.PrintTitleColumns = Sheets("1. Paste Raw Data").Range("Print_Side")
ActiveSheet.PageSetup.PrintTitleRows = Sheets("1. Paste Raw Data").Range("Print_Header")
I have also tried to use the range that is defined by excel (named Print_Titles) to change these settings when the pages are printed but it will not allow me to enter more than one dynamic range. Print_Titles is currently defined like this in name manager;
='1. Paste Raw Data'!$B:$B,'1. Paste Raw Data'!$4:$6
Is there a way of using the two dynamic ranges in to define the titles for the rows and the columns?
I am afraid what you are trying to do, is not accepted by Excel...
It allows setting of PrintTitleRows like Rows, not like a range (like your dynamic range). Excel allows setting only of the ROWSto be repeated. In case of using a range (manually selected), Excel extends the range, to include full rows. In terms of range, it accepts only a range of rows.
If you insist to use your dynamic range you can use:
ActiveSheet.PageSetup.PrintTitleRows = _
ActiveSheet.Rows(Names("Print_Header").RefersToRange.Row).Address
but it would be like you scratch your left side of your head using the right arm...
You can also proceed similarly for PrintTitleColumns, but you will obtain the same result just using of:
ActiveSheet.PageSetup.PrintTitleColumns = "$A:$A"
Even you would manually try setting of those parameters, you would observe that Excel does not allow selecting of ranges. It extends the selection to the whole row(s) or column(s) according to the setting specificity.

How to copy specific parts of a table from a sheet to another

I'm trying to get some details copied in Excel from Sheet 1 columns 1-5 to Sheet 2 columns 1-4, but only for lines that include text or values on sheet one under a specific column (in this case, Column 2). There are other columns in between, so I need to be able to use exact columns rather than A:D for example.
Example of what I'm trying to achieve:
I have tried using a simple IF function with A:A<>"" so it would include any rows that have any data in them, however this does not seem to copy as I need and occasionally based on my attempts i also get circular reference errors. Additionally, I’m not sure how to make sure this gets pasted at the bottom of a table that will expand with each addition.
I realize a probably easier option would be to simply copy Sheet 1 entirely and use a filter on row 1 to deselect Blanks on A:A, but the sheet has so much more info that it would be a waste, and additionally info is constantly added so I need something scale-able. It also occurred to me now that by doing this i would include info from the "header" and "footer", basically a frozen pane - which I do not need.
Could this be done via a simple function, or would it require a Macro?
Please keep in mind I'm rubbish at programming, just trying to make my life easier and learn as I go. A lot of excel forums help but still I'm no coder. I can understand to a pretty big degree what the code does and can adjust accordingly though :)
As suggested, this cannot be done with formulas. There are different ways to achieve this.. below is one approach:
Sub CopyFilteredRows()
Dim oSourceSheet As Worksheet: Set oSourceSheet = ThisWorkbook.Worksheets("Sheet3") ' Set your source sheet here
Dim oRng As Range: Set oRng = oSourceSheet.Range("A2:E" & oSourceSheet.Range("C" & oSourceSheet.Rows.Count).End(xlUp).Row)
' Set filter on column B
oRng.AutoFilter
oRng.AutoFilter 2, "<>"
' Copy to specified sheet
oRng.SpecialCells(xlCellTypeVisible).EntireRow.Copy ThisWorkbook.Worksheets("Sheet4").Range("A2") ' Change your destination sheet here
' Clear objects
Set oRng = Nothing
Set oSourceSheet = Nothing
End Sub
Paste the above UDF in a Module and then run it whenever you want to perform the copy. I suspect you would have to modify it a bit so that you can cater for your particular scenario but it should give you a start

Paste special with autofilter and unfilter after autofilter and paste is complete

I am using the solution from this post to autofilter a table and paste the selected columns to another sheet.
It is doing exactly what I want it to do with one exception. I am copying from a table that uses filtered drop down menus. As a result, the following message pops up while copying: The name already exists
The solution offered in this blog is not an option as the source table is dependent on the named values in it.
Is there an additional statement I can add to the below code that will make it paste special?
copyRange4.SpecialCells(xlCellTypeVisible).Copy tgt.Range("E10")
If not, can something be added to select "Yes" without the pop up appearing at all?
Finally, after it pastes to my target, can an additional line be added to unfilter the source?
Disable Alert to by-pass the alert,
Application.DisplayAlerts = False
copyRange4.SpecialCells(xlCellTypeVisible).Copy tgt.Range("E10")
Application.DisplayAlerts = True
To unfilter,
If (Range).AutoFilterMode Then
(Range).ShowAllData
End If
Where (Range) is your range with filter.

Excel copy-paste including hidden columns

I'm using a Excel 2016 worksheet that has a couple of columns hidden for UI reasons. I need to be able to filter out data and then copy-paste it to another sheet with hidden columns intact and showing after pasting in the destination (it will contain a longer log of similar transactions, not just one copy-paste).
Adding a pic of the objective - i.e. hoping to have the hidden contents of columns B and C being pasted into the destination spreadsheet. Is this possible at all?
Probably not great form to ask 2 questions in one post, however are there alternatives to performing filtering and copy-paste function to another spreadsheet manually? I.e.:
run manual filter to clear blanks in Quantity field;
make a selection
do manual Ctrl+C - Ctrl+V function
Is there a way to make it easier? Unfortunately no VBA or macro experience as of yet.
Edit - Completely misunderstood the question!
You want to include hidden cells when you copy - that's standard behavior for hidden cells but not for filtered columns. If you want to avoid VBA abd you're dealing with small contiguous ranges then a simple formula may be the easiest solution.
Using your example, I will arbitrarily name the source worksheet "Sheet1" and the destination "Sheet2". In Sheet2, click in cell A2 and type this into the formula bar: =Sheet1!A3 Now click the bottom right corner of cell A2 and drag it to the right through D2 then down to D7.
With the range highlighted, press ctrl C to copy, then right click to paste special values.
You're done!
Here's a VBA solution:
Sub copyrng()
Dim srcrng As Range
Dim tmprng As Range
Dim dstrng As Range
Dim srcws As Worksheet
Dim dstws As Worksheet
Set srcrng = Application.InputBox("Area to copy", "Source", Type:=8)
Set srcws = srcrng.Parent
Set tmprng = Application.InputBox("Top Left Corner of Destination", "Destination", Type:=8)
Set dstws = tmprng.Parent
Set dstrng = dstws.Range(tmprng.Address, tmprng.Parent.Cells(tmprng.Row + srcrng.Rows.Count - 1, tmprng.Column + srcrng.Columns.Count - 1))
dstrng = srcrng.Value
End Sub
First answer (answered wrong question)
You can copy visible cells using "Go To..."
Highlight the range you want to copy, press Ctrl G, click "Special...", select "Visible Cells Only", and then press Ctrl C to copy.
Now all hidden cells will be left behind when you paste.
No, you cannot do this with regular Excel features since Excel cannot know which columns/cells to skip when one of the column have blank values, this is something has to be decided and done by a human.
Maybe this is a good time to enter the world of Macros, since you do not need a custom code but can use the recorded macro without any further manipulation. This Excel feature is for inexperienced users just like you.
View / Macros / Record Macro
Name your macro
Do what you need to, keeping in mind that Excel is recording your every move by converting them into VBA codes in the background. For your case, do the following:
Filter the blanks using filter combo-box
Select the range by using CTRL-G / Special / Current Region (do not select the cells by mouse or with your keyboard, your code should be generic should not contain manual ranges since you do not want to do any coding)
CTRL-C to copy
If "to-be-pasted" cell is not fixed for all your cases, then you should stop recording your macro here. If pasting cell is fixed then Paste the contents while the macro is recording.
After the macro is recorded, assign a shortcut to your new Macro using:
Macros / View Macros / Options menu
Voila! Now you are able to do exactly what you have done when recording your macro by using that keyboard shortcut. If you did not paste the content when recording then you s/b using your macro short cut and go to the cell you want to paste and press CTRL-V.
When you feel confident enough, try the Edit menu in the Macros and see what code you have in hand, maybe make some small changes etc. I saw many people who are not familiar with basic coding at the beginning but somehow started writing their own codes after seeing this feature in Excel. Good Luck!
ProfoundlyOblivious code is pretty cool but the
dstws = activesheet
will always be the source since the activesheet passes back straight after the inputbox.
I tried changing it to
Set dstws = tmprng.Parent
but for some reason this then breaks the
Set dstrng = dstws.Range..
I get a Run time error 1004 Method range of object _Worksheet failed?!?!
If I could fix that this solution would work for you with any destination, even other files.
The alternative is to use vba to un-filter the data, then do a copy, then put same filter(s) back on. Once that is done you can go anywhere and paste what is now on the clipboard.

Resources