I would like to be able to print everything I have from columns A:H in landscape mode, and I was wondering if anyone knows how to do this such that whenever you open this specific document and try to print it, it will automatically print under these specific settings. Additionally I would only like to print where there actually is text and avoid printing the white space (I have equations filled in all cells A1:H50, but I only actually have numbers filled in RIGHT NOW (but this will change over time) on cells A1:H30. i.e. A31:H50 are for now all filled with the empty space character "")
Additionally, the top row of my document has headers, which I would like to be displayed for every page that I print out. (i.e. headers of stock name, stock price, stock price 1 year ago, and then those columns are filled with 30 stocks). I would like it so that if I have 4 pages (variable) worth of information that end up being printed, that the top row of stock name and price always get printed at the top of each page.
Thanks a lot!
You could use a BeforePrint event to reset the print area to your specifications each time the user tries to print it. No matter what settings the user tries to choose, it would override with the ones you specify in the event code. Try putting the code below in the ThisWorkbook module:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Application.PrintCommunication = False
With ThisWorkbook.Sheets("Sheet 1")
.AutoFilterMode = False
.Range("$A:$H").AutoFilter Field:=1, Criteria1:="<>", Operator:=xlFilterValues 'if column A (Field:=1) is blank then hide those rows before setting print range
With .PageSetup
.Orientation = xlLandscape
.PrintArea = "$A:$H"
.PrintTitleRows = "$1:$1" 'change to whichever row numbers you want to print on every page
End With
End With
Application.PrintCommunication = True
End Sub
The potential upside/downside to doing it this way would be that it doesn't allow the user to print it in any format other than the one you specify. If you didn't want to limit the user's ability to print it in different formats, you could use the same code inside a non-event Sub and have the user run that macro whenever they need to print it out with these settings.
Okay you're asking a few different questions, and some are easier to answer than others.
Firstly, you want to set a specific area (A1:H50) to be printed every time someone prints the page, and you want it to print in landscape. This is simple - go to the Page Layout ribbon, select the area A1:H50, and click Print Area -> Set Print Area. Then right beside that, click Orientation -> Landscape.
Your third question, about setting a header, is also simple. Go to the Page Layout Ribbon again, and click on Print Tiles. You can either click on the Sheet tab and tell it to repeat some rows at the top, or you can click Header/Footer and manually type in the headers yourself.
Your second question does not have a simple, automated approach (that I know of. Either accept the fact that blank space will be included in the printout (which is fine anyway, as you likely want each page to be spaced the same anyway, regardless of how many rows are filled), or manually "Hide" blank rows.
VBA automated solutions would be possible here, but I don't recommend that you use an automated solution which you don't understand over something so trivial to do manually.
Note that this question is not really suited to this site. This is a question better suited to superuser.com, which is part of the same group of sites as stackoverflow.com, but is geared towards non-programming questions about software - especiallly Excel.
Related
I have the following code to simulate some cells behave like buttons
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Application.ScreenUpdating = False
If Target.Cells.Count = 1 Then
'~~~~~~ pseudocode ~~~~~~
If {select cell is one of the chose ones}
{do some stuff}
End If
'~~~~~~ pseudocode ~~~~~~
End If
Range("A1").Select
'Application.ScreenUpdating = True
End Sub
It doesn't matter whereas I use the ScreenUpdating code, the "Selection box" is flying around from A1 to the selected cell and back and it make the sheet much slower.
Can this flying (animation) selection box be stoped?
So far I have found this, not possible to hide, but noting about the flying efect:
Hide the cell selection box in Excel
Edit:
I need (I think so) edit capabilities on the sheet, therefore the option of not changing selection cell is not applicable. Due to:
most of the sheet is informative, and should be available for copy (not edited)
some cells are input forms (free text thing), selection as usual
some cells should behave like buttons (plus/minus for a numeric value, sclaes, simple stuff, but thousand of then, so much easier do/maintain by code), and user must not edit them
grouping should be available, (so that's complicate protecting the sheet)
I am not closed to the option : Range("A1").Select after each (most) of user interaction, but no other method comes into mind to me now.
An example:
I know some would say: "you should make this out from excel", and I agree with you, but this is a mandatory thing, I do not have the power to raise this question
As you can see, I got the "flying selection" that I try to get rid off
cell A1 is already hodden, that will do most of the trick
final version sure will go with hidden gridlines and headlines
rows groups exist, and are important, so no protection possible
all the functionality, I can do easy with vba, just problem with the animation
Maybe this is not the answer that you have been waiting for, but as Mathieu mentioned in his comment, please try to avoid using Selection.
It does make things slower and often causes errors (in example try selecting cell from hidden sheet). Instead just do something with the range that you define with your if statements directly. Every property of Cell or Range can be accessed directly.
Hope it helps.
Not sure how you can achieve you "flying select box" problem, but at least you could add this code, so opening/closing groups are available on protected sheets:
'Password Protect Current Sheet
ActiveSheet.Protect Password:="Add_here_your_password", UserInterfaceOnly:=True
'Enable Group Collapse/Expand Capabilities
ActiveSheet.EnableOutlining = True
How about trying to remove the "back to A1" as much as possible?
Maybe do it only on absolutely necessary, or move back to the changed value (the 33 in your example), or to the question title (in your multi-option example)
Trying to delete, rather than hide from one worksheet (where the filter button is to another where the catalogue list is. i.e. trying to create something like (best way to describe this) a shopping basket based upon the users selection and reducing the (long) list on the other worksheet after selection.
I am beginning to pull my hair out on this one and after having read and watched many, many articles on deleting rows after filtering on empty cells and today nothing has really helped as there are more issues than solutions when following codes from other, thus far.
Basically, I have a Hugh catalogue on a separate worksheet and if the user says yes, this should be shown and if not if blank (but with a value) it should be deleted. See so very basic script I have that works perfectly, but I have to delete and not hide. It appears EntireRow.delete is something beyond me, as it introduces many many issues, where hide simply worked so smoothly.
Trying many other scripts, they all really fail in simplifying the answer and 99% are actually for a single worksheet and range rather than a specific worksheet and specific columns i.e. E:E (script script below shows more) I am using a table too, so this is a little different too.
For a = 2 To 150
If Worksheets("Requirements").Cells(a, 5).Value = "High" Then
Worksheets("Requirements").Rows(a).Hidden = True
End If
Next
Anyone with a brilliant one or two liner to delete rather than hide, or delete all hidden if necessary
Many thanks in advance
Consider:
Sub sjdhfs()
For a = 150 To 2 Step -1
If Worksheets("Requirements").Cells(a, 5).Value = "High" Then
Worksheets("Requirements").Cells(a, 5).EntireRow.Delete
End If
Next a
End Sub
Note we run the loop from the bottom to the top.
Apologies for not showing my code but after much trial and error I'm thinking the issue I describe could possibly be a textbox property issue rather than coding error or ommission. The code itself works as it should but there is a phenomenom which frustratingly persists.
On a wsheet amongst a number of ActiveX controls I have a textbox and 2 images. They are used for a search function. As you would expect the textbox is for user entry and the images are for 'run search' and 'erase search'. I set the search text as a string.
My issue is when hitting either 'run search' OR 'erase search' the textbox momentarily shows the previous text string. I have set this previous string to "" all over but without success.
This is best observed when setting a search text which will knowingly fail.
The sequence is...
1) Enter 'XXXX' to search
2) Hit 'run search'
3) Search code executes
4) Prior to textbox narrative returning "XXXX not found" it momentarily shows the previous entry, say "AAAA", before returning the correct result.
How can this be prevented?
EDIT
With no response I posted this on Jon Peltier's site at https://peltiertech.com/forms-controls-and-activex-controls-in-excel/#comment-1481602
Kindly he tested and concluded "It looks like an ActiveX thing, and I guess you’re stuck with it."
From tests this phenomenon occurs even when selecting any cell, not only the image controls. In other words it is triggered as soon as the textbox loses focus. Arguably, because it is a momentary change it does not seem possible to trap the text.
I was experiencing a similar problem and raised a question myself, but managed to have more luck in getting some responses, see:
After setting ActiveX textbox to empty value, previous text briefly appears in box before disappearing again
Through the help of the responders I was able to create a work around using Application.SendKeys. Possibly one of the answers might be able to help you in getting around this issue.
For me personally Application.SendKeys was required as this overwrote the value in the text box and refreshed it meaning the previous value was no longer present. For completeness here is a snippet of the code I used:
'Select text box to update
Sheet1.userName.Activate
DoEvents
'Replace value with ""
Sheet1.userName.Application.SendKeys ("")
'Copy above for other textboxes on sheet
Sheet1.emailAddr.Activate
DoEvents
Sheet1.emailAddr.Application.SendKeys ("")
'Change text box currently active to force change values to take place
Sheet1.userName.Activate
I am selecting my text boxes one at a time then using SendKeys("") as a way to emulate the user deleting the current field in the box, after that I switch back to another text box as this is needed to force the refresh of the value.
Being able to force the text boxes to be blank after I was done with the values meant that there were no previous values that could inadvertently appear later on.
Thank you Skenworthy for your suggestion. Over time I kept returning to my issue but without success however your code has formed the basis of a solution.
I only had 1 textbox used as a search input plus 2 buttons, 1 to begin search and the other to clear.
My solution was to create a hidden dummy textbox and each time I wished to make a comment in the search textbox I would use sendkeys to the dummy box. However that was not the final code. Switching focus between the 3 controls became too complex given the type of potential user errors that could be made. So I abandoned the Begin search button relying instead on the Enterkey and the Clear search button.
Now each time I need to return a response message I use the code below immediately prior to clear the search textbox. It has consistently worked without flaw. So, a long time coming, my specific solution seems so simple in hindsight but thank you again for the pointer.
Sub RemovePrevText()
''' use dummy txtbox to clear old message from SearchInput
With Sheets(1)
.txtDummy.Activate
DoEvents
.txtDummy.Application.SendKeys ("")
End With
End Sub
I posted earlier, but since have made some good progress and figured some stuff out by myself!
I have nearly finished my project, but need to make the last real bit of code, then just some general tidying up.
What I want to do
I currently have two page in a MultiPage userform - one called 'main' and one called 'extra
I want Main to always be shown to the user
I want Extra to be hidden and for this page to be shown automatically under a specific condition
I have a question on my userform (using Option Buttons):
"Did the customer ask about an extra product today?"
If the answer to this question is no
When the user hits the command button (after filling out the rest of the form), do nothing special - just return all the values to the worksheet.
If the answer is yes
When the user hits the command button, I want them to be taken to the 'extra' page. They will fill out some additional check-boxes, then hit another command box, which will return the info from both the 'main' and 'extra' sheets together, into the same row of the worksheet.
In brief:
I want my 'extra' tab to be hidden, only to appear when 'yes' is selected, and for the results all to go back to the worksheet. If you want to see my workbook:
https://drive.google.com/file/d/0B2F...it?usp=sharing
Can this be done? Thank you for your help :D
How I have hidden my Pages
I have changed the properties of the multipage to : "Style: fmTabStyleNone."
Now I just need the code to make my command button send the user to the next page, if required.
This isn't working for me. Any ideas??
If ProductEnquiryYes.Value = True Then
With MultiPage1
If .SelectedItem.Index < .Pages.Count - 1 Then
.Value = .SelectedItem.Index + 1
End If
End With
End If
You can use the propriety Visible.
If the Tabs name is **TabStrip1:**
TabStrip1.Tabs(1).Visible = False
hide the second Tab. The Index Start From 0 to n-1 tabs.
When you Show, you can also active the tabs with:
TabStrip1.Tabs(1).Visible = True
TabStrip1.Value = 1
With Multipages:
MultiPage1.Pages(1).Visible = True
MultiPage1.Value = 1
I am using excel 2010.
I want to clear the content of a combo box in my sheet(clear to blank like when it's not selected), but I don't know how to select and clear. I tried to select a combo box like this:
Sheet1.ComboBox1.Clear
But no 'ComboBox' is under the Sheet1 object. The only to select my combo box is use this:
Sheet1.Shapes("Drop Down 24")
I don't know how to select and clear the content, can anyone help me?
What about
ActiveSheet.Shapes.Range(Array("Drop Down 24")).Select
With Selection
.ListFillRange = ""
End With
I assume you actually want to make the displayed value of your control blank. In that case, for a Drop Down Object, as you indicated you would do this:
Sheet1.Shapes("Drop Down 2").OLEFormat.Object.Value = 0
Where 0 indicates which element from the list is selected I.E. none.
If that doesn't work, then you're probably actually dealing with a ComboBox in which case you want to use this:
Sheet1.Shapes("Drop Down 2").OLEFormat.Object.Object.Value = ""
Note this code was created and tested in Excel 2003 (what I have on this machine) so the path to reaching the actual object might vary slightly on Excel 2010.)
As an autentic programmer I would rather prefer this way. That don't depends on excel "range memory selection" on the sheet.
Set oCombo = Sheets("SheetName").Shapes("cmbComboName").ControlFormat
For I = oCombo.ListCount To 1 Step -1
oCombo.RemoveItem (I)
Next
To reset a Drop Down List to a blank cell but still maintaining the list for future use.
Create a Macro to clear the cells. During recording simply select the cell and select "clear contents". This will set the selection to a blank cell but still keeps the drop down list in place.
It would look like this for example.
Range("H3").Select
Selection.ClearContents