Command_button in VBA inside vs Shape with Assigned Macro - excel

I am trying to write a script for extracting data from web inside a userform. I am using Get Data From Web option in Excel (2019). The data gets updated when I refresh it manually or even when I do it with a macro assigned Autoshape. However, I have pasted the same code from the macro for a CommandButton that is inside my Userform. My intention is to refresh the data when I click the CommandButton. But when I click it, the background query keeps on running and doesn't terminate.
This is the code for a macro (for REFRESH DATA autoshape)
Sub refresh()
ActiveWorkbook.Connections("Query - stocklist").refresh
End Sub
This is the code inside my Userform (for REFRESH and GET commandbuttons). The GET button does a kind of Vlookup and works fine.
Private Sub cmd_refresh_Click()
ActiveWorkbook.Connections("Query - stocklist").refresh
End Sub
Private Sub cmd_get_Click()
txt_script.Text = UCase(txt_script.Text)
For Each cell In Range("Table_stock[Symbol]")
'For Each cell In Range("A1:I220")
If cell = txt_script.Value Then
txt_ltp.Value = cell.Offset(0, 4)
End If
Next cell
End Sub
ANY SUGGESTIONS?
it is more clear in the attached picture of excel template showing user form and macro button

Related

Excel VBA Userform read current values when opening

I am trying to make an user form for hiding/showing different columns of the worksheet.
So i've made a button which opens the userform (called 'hider') with the following code:
Private Sub CommandButton1_Click()
Hider.Show
End Sub
The userform than currently contains two checkboxes which hide the selected columns using:
Private Sub Week3_Click()
Range("N:Q").Columns.Hidden = Not Week3
End Sub
So if the checkbox is 'checked' the columns are shown and if 'unchecked' the columns are hidden, this part works, except every time the userform opens it will reset the checkboxes to their native state of 'unchecked' while the columns remain hidden (which is ok).
So my question is:
How can I sync the checkboxes in the userform to the currently active value of the columns? I was thinking about making a sync button on the userform or an action to read all the current values when opening the userform, but i couldn't get that to work.
If I understand your question correctly ("...or an action to read all the current values when opening the userform") just use the following code within the Userform code module to synchronize your checkbox when opening:
Private Sub UserForm_Initialize()
'Me.Week3.Value = IIf(ActiveSheet.Range("N:Q").Columns.Hidden = True, False, True)
Me.Week3.Value = Not ActiveSheet.Range("N:Q").Columns.Hidden ' simplified due to comment thx Mathieu Guindon
' ...
End Sub

Runtime Error 1004 for a userform which is still there

To simplify use of a spreadsheet and avoid anyone just adding or deleting rows as they wish (and therefore messing up the formulae) I have used a series of userforms. The first Userform gives the user the option to either 'Cancel', 'Delete Row', or 'Insert Row'. Selecting 'Cancel' unloads the form, and selecting 'Delete Row' unloads the original form and shows another Userform - this allows the user to select the row number they want to delete, and pressing OK deletes it. This all works fine.
The issue I have is with the 'Insert Row' button - this should unload the first Userform and show another userform. This new 'Insert Row' Userform allows the user to input the row number after which the new row is to be inserted. The user has the option to input further detail in various textboxes and comboboxes which will populate the new empty row - these include start and end dates.
I was having integer vs string issues with the option for manual input of the dates, so I opted to go with another 2 userforms, opened from the start and end dates which gave a calendar (select the date you want, press OK and the date is inserted in the relevant textbox in the 'Insert Row' userform). I tested these all the way through to iron out the wrinkles, and everything seemed to be working.
I then opened the first userform from the command button on the spreadsheet,
Selected 'Cancel' and 'Delete Row' to check they still worked, then selected 'Insert Row' and got 'Runt-time error '1004' Application-defined or object-defined error'. When I went to debug, it highlights the line: userform1.show
The only thing I can think of that could be a problem (and the only significant change I've made) are the two calendars, which reference userform1. I've carried out multiple searches, but haven't found anything which even looks like my problem - I just don't know what the issues are with having multi-layered userforms. I've included some of the code by way of further explanation:
Private Sub InsertRowButton_Click()
'unload the first userform when the user selects "Insert Row"
Unload ModifyProjectUserForm1
''This line highlights as the problem
UserForm1.Show
End Sub
'''the following subs are embedded in "UserForm1" (the one to Insert Row), allowing the user to select a date:
Private Sub InsertRowNumberStartDateCommandButton_Click()
frmCalendar1.Show
End Sub
Private Sub InsertRowNumberEndDateCommandButton_Click()
frmCalendar2.Show
End Sub
'''The following are the subs used to take the dates and place them in "UserForm1":
Private Sub StartDateCalendarOK_Click()
On Error Resume Next
UserForm1.InsertRowNumberStartDateTextBox.Value = frmCalendar1.Calendar1.Value
Unload frmCalendar1
End Sub
Private Sub EndDateCalendarOK_Click()
On Error Resume Next
UserForm1.InsertRowNumberEndDateTextBox.Value = frmCalendar2.Calendar2.Value
Unload frmCalendar2
End Sub
I expected the 'Insert Row' userform to open, allowing me to put a new row in, with associated data, and dates. What I got was a runtime error, and I'm at a complete loss.
I don't know if I'm going down completely the wrong route, but I'm loathe to post my entire code, because there's a lot of it!
Can anyone help?

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 VBA: Workbook_Open

I'm using Workbook_Open to call a userform when the application is opened, this is working fine. However I would like it to only run the first time it is opened.
I tried this and it work if I run the sub from the editor but not when I open the file.
Sub Workbook_Open()
If Worksheets("DataSheet").Range("A1").Value = "" Then
QuickStartForum.Show
End If
End Sub
Note: A1 contains a value that will be populated after the user form has run
It appears that the problem is that it opens the user form before the data is loaded into the worksheet.
Is this there a way around this or do I need to take a different approach ?
I think it is because you have this code in a Module. You need to put the code within 'ThisWorkBook'.
I tried this following code and had no issues when it was in the 'ThisWorkBook' it failed to run within 'Module1'
Private Sub Workbook_Open()
If Worksheets("DataSheet").Range("A1").Value = "" Then
QuickStartForum.Show
Worksheets("DataSheet").Range("A1").Value = "filled" ' <-- this fills the cell with data for testing, so that when you reopen the file it should not re-open the userform
Else
MsgBox ("not shown because the A1 cell has data")
End If
End Sub

Resources