Textbox to a cell in hidden sheet - excel

Im trying to make a macro for userform to input data in to hiden sheet. I have tryied application screening then visible the sheet and then input the data in a specific cell but ints not working like that.
Anyone know how it works?

You don't need to make a sheet visible to populate it.
For example, create a new workbook and make sure there is a sheet in there called Sheet2
Hide Sheet2
Put this into the debug window (ctrl-g) in the VB Editor
Sheets("Sheet2").range("A1").Formula = "Text in a hidden sheet"
press enter
Unhide Sheet2 and look at cell A1

Looks like thats not the only problem i have got. I will try to explain it. When a press a button a userform pop up and there is a text box in it. You write a name in the box, click a button and the name appears is a cell in one of the sheets that is hidden. That we already did.
Then i want to move to another sub which is located in one of the sheets. The other sub is for printing out a hidden sheet.
I have an Option Explicit there and
A sub called PrintFile.
Application.ScreenUpdating = False
With Sheets("Opis")
.Visible = True
.PrintOut
.Visible = False
End With
Application.ScreenUpdating = True
The problem is that it's giving me an error and i can't switch to the other sub.
Where im doing it wrong?

Related

How to generate a copy of a page from a drop down menu in Excel?

I am developing an electronic audit sheet for work, where the desire is to generate a new work sheet each day, copied from a blank master sheet. This part is easy, I have a simple macro attached to a button that generates a copy of my master sheet, places it last in the tab, and automatically names it the current day's date.
Sub NewDay()
Sheets("Master").Copy After:=Worksheets(Worksheets.Count)
NewPageName = Format(Date, "dd-mm-yyyy")
ActiveSheet.Name = NewPageName
End Sub
What I would now like to do is to give the ability to create a new sheet from a selection of 'master' sheets, while retaining the date-as-sheet-name part.
Ideally, the user experience would be to select from a drop down menu the sheet, and click a button to create the new sheet, or to click the button, be presented with the list of options to select, and then generate.
I am relatively inexperienced at VBA, and this is beginning to go out of what little realm of mastery I have. Any and all help would be greatly appreciated.
For selecting items from a list in a User form, I strongly recommend ComboBox. In Project Explorer, insert a userform, and add a ComboBox and a Button. To create the list for the user to select from. We need to have our macro do ComboBox.Add(SheetName,Index) for each of the Master Sheets. To keep track of everything, it's best to have them declared in an array.
We then pop-up the form for the user to interact with using UserForm.Show. Once the user has made their selection, the select button does UserForm.Hide to close the form.
Since we created the combobox list from an array, the list index of the selected item is equivalent to the array index of the sheet we want to copy.
Sub Start()
Dim MasterSheets() As Variant, ShtSelect As Integer
MasterSheets = Array(Sheet1, Sheet2, Sheet3, Sheet4)
'This is your list of Master Sheets.
'Change Sheet1 and Sheet2 to Sheets("Master1") and Sheets("Master2")
With UserForm1
.ComboBox1.Clear
For i = LBound(MasterSheets) To UBound(MasterSheets)
.ComboBox1.AddItem MasterSheets(i).Name, i
Next i
.Show
ShtSelect = .ComboBox1.ListIndex
'This is how to get the user selected item from the ComboBox
End With
'The Selected Sheet is referenced by MasterSheets(ShtSelect)
'Your code from before
MasterSheets(ShtSelect).Copy After:=Worksheets(Worksheets.Count)
NewPageName = Format(Date, "dd-mm-yyyy")
ActiveSheet.Name = NewPageName
End Sub
These are the only lines inside UserForm1
Private Sub CommandButton1_Click()
If UserForm1.ComboBox1.ListIndex <> -1 Then Me.Hide
'ListIndex = -1 means that the user has not yet selected anything.
End Sub
You may also want to check to see if a sheet with that name already exists before attempting to create it. Otherwise you may run into errors if someone runs the macro twice in one day.
I would suggest something like:
NewPageName = Format(Date, "dd-mm-yyyy")
If Sheets(NewPageName) Is Nothing Then 'Only copy if it doesn't already exist
MasterSheets(ShtSelect).Copy After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = NewPageName
End If

Detect ComboBox Change Excel VBA

I have a sheet with a bunch of ComboBoxes(form control) and I want to detect when a user changes any one of them and write text in a cell. Using Worksheet_Change on the target cells doesn't work. I have tried a bunch of things that don't work. I'm not sure what needs to be in the private sub line or the if statement.
Private Sub DropDowns_DropButtonClick()
If ActiveSheet.DropDowns.Value > 1 Then
Cells(13, 5).Font.Bold = True
Cells(13, 5).Font.Color = vbRed
Cells(13, 5).Value = "!!! Selections have been changed. !!!"
End If
End Sub
I have tried
ComboBox_AfterUpdate()
ComboBox_Change()
DropDowns_AfterUpdate()
DropsDowns_Change()
and anything else I could find. I've also tried a few different things in the if statement with no luck.
I appreciate any help.
Chris
If I'm reading you correctly, you're comboboxes are in a userform. If I'm correct, simply open your userform in 'Visual Basic' and double click on the relavant combobox. This will open the code pane and create an empty Private Sub routine called 'Private Sub <Combobox Name> ()'.
Enter your code to place your data in the sheet (or whatever else you want) into the subroutine and Bob should be your uncle.
Apologies in advance if there's something I've missed.
RannochRob
Edit...
OK, my mistake, it's a form control.
My first comment is that it's easier to use an activex control if you can... however, with a form control, should (a) Use the cell link box in the 'Format Control' drop down ('Control' tab) to place the result in a cell... however, that result will not be the content of the box but an integer equal to the position of the selected entry on the list of entries in the combobox. You then need to (b) assign a macro to the combobox which will pick up the result and use it to get the required information from the range containing the list of entries. Like I say, much easier with an activex control...
RannochRob
Here's how you can do it by assigning a macro to the combobox (right click on the combobox>assign macro) as #BigBen mentioned in the comments section:
Option Explicit
Sub DropDown1_Change()
Dim sht As Worksheet
Dim dd As DropDown
Set sht = ThisWorkbook.Worksheets("Name of your Worksheet") 'Name of the worksheet in which the combobox is located
Set dd = sht.DropDowns("Drop Down 1") 'name of your combobox
sht.Range("G1").Value = "The selected value is: " & dd.List(dd.Value) 'dd.value returns the index of the selected value
End Sub
You can use the same code for each one of your comboboxes.
For demonstration purposes i have used the following set-up:
You can easily modify the code to best fit your needs.

Excel vba - Userform - doesn't change output - focus worksheet

Trying to use excel vba userforms to automate many tasks in a spreadsheet. There is a Button1 on Sheet1 that has two commands.
sub Button1_click()
sheet1.range("a3").select
userform1.show
end sub
As an example there is a data entry worksheet (sheet2) that we want to switch to to input data values to a list.
To simplify in this example and to show my issue the userform has one button
sub CommandButton1_click()
userform1.hide ' hide the form
sheet2.activate
sheet2.range("b2").select
end sub
What I want to be able to do is use the user form button to switch to sheet2, select b2, and be able to enter data starting there immediately.
What I've been getting is a selection box on sheet2.range("b2") BUT I show color starting at sheet1!a3, then sheet1!b4,... I have entry occurring on sheet1!b2 etc.
Shows beginning and entry
Entry colors from sheet1 showing on visible sheet2 - data not appearing
Actual data is entered on sheet1 not sheet2 -
The problem seems to be (as you mention in the comments) that, at the end of Button1_Click, the focus returns to the sheet containing the button even though the ActiveSheet is now a different sheet.
For the moment (until someone comes up with a better solution) a "workaround" is to allow the Button1_Click event to finish running before showing the Form. That can be achieved by changing Button1_Click to something like:
Sub Button1_click()
Sheet1.Range("a3").Select
Application.OnTime Now(), "BypassBug"
End Sub
Sub BypassBug()
UserForm1.Show
End Sub

Excel ActiveX Combobox shows previous selection when losing focus

I have this code which fills a combobox on Sheet1 with the Name column of Table1 on Sheet2.
Public Sub Worksheet_Activate()
Me.ComboBox1.List = Worksheets("Sheet2").ListObjects("Table1")_
.ListColumns("Name").DataBodyRange.Value
End Sub
Works fine but it has a weird effect when I click off the combobox onto the sheet. The selected entry in the box quickly flashes to the previous entry. For example, the currently selected item is "b" and then I select "c". If I click on the worksheet the entry in the box quickly flashes to "b" before going back to "c".
I've put this code alone in a new file and I still get the same effect. Has anyone else seen this?
Edit regarding reason for Public Sub:
Forgot to include the Workbook_Open code so that Sheet1 is considered Activated when you open the Workbook. But it doesn't matter if I keep that code or not, I still see the effect.
Private Sub Workbook_Open()
Call ActiveSheet.Worksheet_Activate
End Sub
Adding a LostFocus event with code that selects a cell on your worksheet should cause the flicker not to happen when you select a cell after changing the ComboBox's value.
Like the following:
Private Sub ComboBox1_LostFocus()
ActiveSheet.Range("A1").select
End Sub

Excel Userform Combobox Properties Rowsource box issues?

I have a userform in Excel that works as a calculator.
In this userform I have two ComboBoxs (1 & 2)
In VBA editor, with ComboBox1 selected, In Properties, under Rowsourse I have: Sheet1!a4:a5
In Sheet1, A4 = Auckland and A5 = Christchurch
This is fine and when I run the userform there is a drop down arrow with the two options (Auckland or Christchurch).
However my problem is that when you open this workbook I have a VBA command to hide it from the users sight, leaving them only the userform to work with which is what is desired.
The issue is that if you have another workbook open then open this calculator workbook (which automatically hides itself). Then the combobox list is populated by Sheet1!a4:a5 on the other workbook that was already open, not the workbook that actually contains "Auckland" & "Christchurch" from which the userform is from.
I have tried making the Rowsource for the comboboxes more specific by putting the following in the rowsource box in properties: [book1.xlsm]sheet1!a4:a5 but this comes up with a "Invalid Property Value" error message.
I have also tried making a:
Private Sub Userform1_Initialize()
ComboBox1.Additem "Auckland"
ComboBox1.Additem "Christchurch"
End Sub
And also tried this:
Private Sub Userform1_Initialize()
ComboBox1.RowSource = Workbooks("book1.xlsm").Sheets("Sheet1").Range("a4:a5").Value
End Sub
However with both codes when it opens and runs now the comboboxes are empty and there is no list.
I think the easist solution would be to somehow put the full path (including workbook name) into the rowsource box under properties. But I must be missing something as its coming up with that error for me?
All help would be greatly appreciated.
Thanks
You are missing ' in your full path row source.
It should be like this:
Me.ComboBox1.RowSource = "'[book1.xlsm]Sheet1'!$A$4:$A$5"
I have similar question that can be found HERE.
Set the row source property of the combobox as: SheetName!$Col$Row:$Col$Row, e.g.: Location!$A$1:$A$3.
You may try adding this code on userform:
Private Sub UserForm_Initialize()
ComboBox1.list = Array("Auckland","Christchurch")
End Sub
Then set Combobox propert "MatchEntry" to "1".

Resources