Activate an excel sheet using an excel UserForm










0














I am trying to activate a spreadsheet using a pair of listboxes. lstWorkbooks stores the name of the workbook. lstSheets is a list of worksheets for the selected workbook in lstworksheets. My problem is selecting values from these list is producing random behaviour.



Please see my code below



Dim xlApp As Excel.Application
Dim wbk As Workbook

Private Sub lstSheets_AfterUpdate()
ActiveWorkbook.Sheets(lstSheets.Value).Activate
End Sub

Private Sub lstWorkbooks_AfterUpdate()
Me.lstSheets.Clear
For Each wbk In xlApp.Workbooks
If wbk.Name = Me.lstWorkbooks.Value Then
Dim sh As Worksheet
For Each sh In wbk.Worksheets
If sh.Visible = xlSheetVisible Then Me.lstSheets.AddItem sh.Name
Next sh
Exit For
End If
Next wbk
Windows(lstWorkbooks.Value).Activate
End Sub

Private Sub UserForm_Activate()
Set xlApp = GetObject(, "Excel.Application")
For Each wbk In xlApp.Workbooks
If wbk.Name <> "Personal.xlsb" Then Me.lstWorkbooks.AddItem wbk.Name
Next wbk
End Sub


Any help here so that it produces the desired behaviour is appreciated










share|improve this question























  • You don't need xlApp. As long as the code is in Excel it knows that the parent of Workbooks is the application.
    – Darren Bartrup-Cook
    Nov 12 at 13:59










  • Also - what kind of "random behaviour"?
    – Darren Bartrup-Cook
    Nov 12 at 14:03










  • @DarrenBartrup-Cook On clicking the a value in the list box, it, might make the form disappear, render all the controls unusable, or sometimes actually work as expected. Other times it would select the appropriate workbook, but the worksheet list box doesnt react to the the selection
    – Takudzwa
    Nov 13 at 13:19















0














I am trying to activate a spreadsheet using a pair of listboxes. lstWorkbooks stores the name of the workbook. lstSheets is a list of worksheets for the selected workbook in lstworksheets. My problem is selecting values from these list is producing random behaviour.



Please see my code below



Dim xlApp As Excel.Application
Dim wbk As Workbook

Private Sub lstSheets_AfterUpdate()
ActiveWorkbook.Sheets(lstSheets.Value).Activate
End Sub

Private Sub lstWorkbooks_AfterUpdate()
Me.lstSheets.Clear
For Each wbk In xlApp.Workbooks
If wbk.Name = Me.lstWorkbooks.Value Then
Dim sh As Worksheet
For Each sh In wbk.Worksheets
If sh.Visible = xlSheetVisible Then Me.lstSheets.AddItem sh.Name
Next sh
Exit For
End If
Next wbk
Windows(lstWorkbooks.Value).Activate
End Sub

Private Sub UserForm_Activate()
Set xlApp = GetObject(, "Excel.Application")
For Each wbk In xlApp.Workbooks
If wbk.Name <> "Personal.xlsb" Then Me.lstWorkbooks.AddItem wbk.Name
Next wbk
End Sub


Any help here so that it produces the desired behaviour is appreciated










share|improve this question























  • You don't need xlApp. As long as the code is in Excel it knows that the parent of Workbooks is the application.
    – Darren Bartrup-Cook
    Nov 12 at 13:59










  • Also - what kind of "random behaviour"?
    – Darren Bartrup-Cook
    Nov 12 at 14:03










  • @DarrenBartrup-Cook On clicking the a value in the list box, it, might make the form disappear, render all the controls unusable, or sometimes actually work as expected. Other times it would select the appropriate workbook, but the worksheet list box doesnt react to the the selection
    – Takudzwa
    Nov 13 at 13:19













0












0








0







I am trying to activate a spreadsheet using a pair of listboxes. lstWorkbooks stores the name of the workbook. lstSheets is a list of worksheets for the selected workbook in lstworksheets. My problem is selecting values from these list is producing random behaviour.



Please see my code below



Dim xlApp As Excel.Application
Dim wbk As Workbook

Private Sub lstSheets_AfterUpdate()
ActiveWorkbook.Sheets(lstSheets.Value).Activate
End Sub

Private Sub lstWorkbooks_AfterUpdate()
Me.lstSheets.Clear
For Each wbk In xlApp.Workbooks
If wbk.Name = Me.lstWorkbooks.Value Then
Dim sh As Worksheet
For Each sh In wbk.Worksheets
If sh.Visible = xlSheetVisible Then Me.lstSheets.AddItem sh.Name
Next sh
Exit For
End If
Next wbk
Windows(lstWorkbooks.Value).Activate
End Sub

Private Sub UserForm_Activate()
Set xlApp = GetObject(, "Excel.Application")
For Each wbk In xlApp.Workbooks
If wbk.Name <> "Personal.xlsb" Then Me.lstWorkbooks.AddItem wbk.Name
Next wbk
End Sub


Any help here so that it produces the desired behaviour is appreciated










share|improve this question















I am trying to activate a spreadsheet using a pair of listboxes. lstWorkbooks stores the name of the workbook. lstSheets is a list of worksheets for the selected workbook in lstworksheets. My problem is selecting values from these list is producing random behaviour.



Please see my code below



Dim xlApp As Excel.Application
Dim wbk As Workbook

Private Sub lstSheets_AfterUpdate()
ActiveWorkbook.Sheets(lstSheets.Value).Activate
End Sub

Private Sub lstWorkbooks_AfterUpdate()
Me.lstSheets.Clear
For Each wbk In xlApp.Workbooks
If wbk.Name = Me.lstWorkbooks.Value Then
Dim sh As Worksheet
For Each sh In wbk.Worksheets
If sh.Visible = xlSheetVisible Then Me.lstSheets.AddItem sh.Name
Next sh
Exit For
End If
Next wbk
Windows(lstWorkbooks.Value).Activate
End Sub

Private Sub UserForm_Activate()
Set xlApp = GetObject(, "Excel.Application")
For Each wbk In xlApp.Workbooks
If wbk.Name <> "Personal.xlsb" Then Me.lstWorkbooks.AddItem wbk.Name
Next wbk
End Sub


Any help here so that it produces the desired behaviour is appreciated







excel vba excel-vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 14:05









Pᴇʜ

20.2k42650




20.2k42650










asked Nov 12 at 13:55









Takudzwa

1615




1615











  • You don't need xlApp. As long as the code is in Excel it knows that the parent of Workbooks is the application.
    – Darren Bartrup-Cook
    Nov 12 at 13:59










  • Also - what kind of "random behaviour"?
    – Darren Bartrup-Cook
    Nov 12 at 14:03










  • @DarrenBartrup-Cook On clicking the a value in the list box, it, might make the form disappear, render all the controls unusable, or sometimes actually work as expected. Other times it would select the appropriate workbook, but the worksheet list box doesnt react to the the selection
    – Takudzwa
    Nov 13 at 13:19
















  • You don't need xlApp. As long as the code is in Excel it knows that the parent of Workbooks is the application.
    – Darren Bartrup-Cook
    Nov 12 at 13:59










  • Also - what kind of "random behaviour"?
    – Darren Bartrup-Cook
    Nov 12 at 14:03










  • @DarrenBartrup-Cook On clicking the a value in the list box, it, might make the form disappear, render all the controls unusable, or sometimes actually work as expected. Other times it would select the appropriate workbook, but the worksheet list box doesnt react to the the selection
    – Takudzwa
    Nov 13 at 13:19















You don't need xlApp. As long as the code is in Excel it knows that the parent of Workbooks is the application.
– Darren Bartrup-Cook
Nov 12 at 13:59




You don't need xlApp. As long as the code is in Excel it knows that the parent of Workbooks is the application.
– Darren Bartrup-Cook
Nov 12 at 13:59












Also - what kind of "random behaviour"?
– Darren Bartrup-Cook
Nov 12 at 14:03




Also - what kind of "random behaviour"?
– Darren Bartrup-Cook
Nov 12 at 14:03












@DarrenBartrup-Cook On clicking the a value in the list box, it, might make the form disappear, render all the controls unusable, or sometimes actually work as expected. Other times it would select the appropriate workbook, but the worksheet list box doesnt react to the the selection
– Takudzwa
Nov 13 at 13:19




@DarrenBartrup-Cook On clicking the a value in the list box, it, might make the form disappear, render all the controls unusable, or sometimes actually work as expected. Other times it would select the appropriate workbook, but the worksheet list box doesnt react to the the selection
– Takudzwa
Nov 13 at 13:19












1 Answer
1






active

oldest

votes


















0














You have



 Private Sub lstSheets_AfterUpdate()
ActiveWorkbook.Sheets(lstSheets.Value).Activate
End Sub


but lstsheets could list sheets from any open workbook. You can't assume the sheet is on the activeworkbook. You need to ensure the workbook is active first, because the user could have activated a different workbook between selecting the workbook and selecting the sheet.



But another point is why are you trying to activate a sheet anyway. VB code doesn't require it, and the user would surely find it easier to just activate a sheet with the mouse if they wanted to see one rather than use a form?






share|improve this answer






















  • No, it's not listing sheets from all open workbooks. Appears to be working fine for me - except "Personal.xlsb" is case sensitive so it's still showing "PERSONAL.XLSB".
    – Darren Bartrup-Cook
    Nov 12 at 14:10






  • 1




    I've edited to make my point clearer - thanks for the correction
    – Harassed Dad
    Nov 12 at 14:15










  • The reason I am doing this is to be able to select an open excel workbook to load data from into another workbook automatically. But I thought provide that part of the project was not necessary to solve the problem at hand.
    – Takudzwa
    Nov 13 at 13:23










  • But you don't need to select or activate a workbook or sheet to access its data through vba - just specify its name.
    – Harassed Dad
    Nov 13 at 13:43










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263675%2factivate-an-excel-sheet-using-an-excel-userform%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You have



 Private Sub lstSheets_AfterUpdate()
ActiveWorkbook.Sheets(lstSheets.Value).Activate
End Sub


but lstsheets could list sheets from any open workbook. You can't assume the sheet is on the activeworkbook. You need to ensure the workbook is active first, because the user could have activated a different workbook between selecting the workbook and selecting the sheet.



But another point is why are you trying to activate a sheet anyway. VB code doesn't require it, and the user would surely find it easier to just activate a sheet with the mouse if they wanted to see one rather than use a form?






share|improve this answer






















  • No, it's not listing sheets from all open workbooks. Appears to be working fine for me - except "Personal.xlsb" is case sensitive so it's still showing "PERSONAL.XLSB".
    – Darren Bartrup-Cook
    Nov 12 at 14:10






  • 1




    I've edited to make my point clearer - thanks for the correction
    – Harassed Dad
    Nov 12 at 14:15










  • The reason I am doing this is to be able to select an open excel workbook to load data from into another workbook automatically. But I thought provide that part of the project was not necessary to solve the problem at hand.
    – Takudzwa
    Nov 13 at 13:23










  • But you don't need to select or activate a workbook or sheet to access its data through vba - just specify its name.
    – Harassed Dad
    Nov 13 at 13:43















0














You have



 Private Sub lstSheets_AfterUpdate()
ActiveWorkbook.Sheets(lstSheets.Value).Activate
End Sub


but lstsheets could list sheets from any open workbook. You can't assume the sheet is on the activeworkbook. You need to ensure the workbook is active first, because the user could have activated a different workbook between selecting the workbook and selecting the sheet.



But another point is why are you trying to activate a sheet anyway. VB code doesn't require it, and the user would surely find it easier to just activate a sheet with the mouse if they wanted to see one rather than use a form?






share|improve this answer






















  • No, it's not listing sheets from all open workbooks. Appears to be working fine for me - except "Personal.xlsb" is case sensitive so it's still showing "PERSONAL.XLSB".
    – Darren Bartrup-Cook
    Nov 12 at 14:10






  • 1




    I've edited to make my point clearer - thanks for the correction
    – Harassed Dad
    Nov 12 at 14:15










  • The reason I am doing this is to be able to select an open excel workbook to load data from into another workbook automatically. But I thought provide that part of the project was not necessary to solve the problem at hand.
    – Takudzwa
    Nov 13 at 13:23










  • But you don't need to select or activate a workbook or sheet to access its data through vba - just specify its name.
    – Harassed Dad
    Nov 13 at 13:43













0












0








0






You have



 Private Sub lstSheets_AfterUpdate()
ActiveWorkbook.Sheets(lstSheets.Value).Activate
End Sub


but lstsheets could list sheets from any open workbook. You can't assume the sheet is on the activeworkbook. You need to ensure the workbook is active first, because the user could have activated a different workbook between selecting the workbook and selecting the sheet.



But another point is why are you trying to activate a sheet anyway. VB code doesn't require it, and the user would surely find it easier to just activate a sheet with the mouse if they wanted to see one rather than use a form?






share|improve this answer














You have



 Private Sub lstSheets_AfterUpdate()
ActiveWorkbook.Sheets(lstSheets.Value).Activate
End Sub


but lstsheets could list sheets from any open workbook. You can't assume the sheet is on the activeworkbook. You need to ensure the workbook is active first, because the user could have activated a different workbook between selecting the workbook and selecting the sheet.



But another point is why are you trying to activate a sheet anyway. VB code doesn't require it, and the user would surely find it easier to just activate a sheet with the mouse if they wanted to see one rather than use a form?







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 14:13

























answered Nov 12 at 14:06









Harassed Dad

2,8061612




2,8061612











  • No, it's not listing sheets from all open workbooks. Appears to be working fine for me - except "Personal.xlsb" is case sensitive so it's still showing "PERSONAL.XLSB".
    – Darren Bartrup-Cook
    Nov 12 at 14:10






  • 1




    I've edited to make my point clearer - thanks for the correction
    – Harassed Dad
    Nov 12 at 14:15










  • The reason I am doing this is to be able to select an open excel workbook to load data from into another workbook automatically. But I thought provide that part of the project was not necessary to solve the problem at hand.
    – Takudzwa
    Nov 13 at 13:23










  • But you don't need to select or activate a workbook or sheet to access its data through vba - just specify its name.
    – Harassed Dad
    Nov 13 at 13:43
















  • No, it's not listing sheets from all open workbooks. Appears to be working fine for me - except "Personal.xlsb" is case sensitive so it's still showing "PERSONAL.XLSB".
    – Darren Bartrup-Cook
    Nov 12 at 14:10






  • 1




    I've edited to make my point clearer - thanks for the correction
    – Harassed Dad
    Nov 12 at 14:15










  • The reason I am doing this is to be able to select an open excel workbook to load data from into another workbook automatically. But I thought provide that part of the project was not necessary to solve the problem at hand.
    – Takudzwa
    Nov 13 at 13:23










  • But you don't need to select or activate a workbook or sheet to access its data through vba - just specify its name.
    – Harassed Dad
    Nov 13 at 13:43















No, it's not listing sheets from all open workbooks. Appears to be working fine for me - except "Personal.xlsb" is case sensitive so it's still showing "PERSONAL.XLSB".
– Darren Bartrup-Cook
Nov 12 at 14:10




No, it's not listing sheets from all open workbooks. Appears to be working fine for me - except "Personal.xlsb" is case sensitive so it's still showing "PERSONAL.XLSB".
– Darren Bartrup-Cook
Nov 12 at 14:10




1




1




I've edited to make my point clearer - thanks for the correction
– Harassed Dad
Nov 12 at 14:15




I've edited to make my point clearer - thanks for the correction
– Harassed Dad
Nov 12 at 14:15












The reason I am doing this is to be able to select an open excel workbook to load data from into another workbook automatically. But I thought provide that part of the project was not necessary to solve the problem at hand.
– Takudzwa
Nov 13 at 13:23




The reason I am doing this is to be able to select an open excel workbook to load data from into another workbook automatically. But I thought provide that part of the project was not necessary to solve the problem at hand.
– Takudzwa
Nov 13 at 13:23












But you don't need to select or activate a workbook or sheet to access its data through vba - just specify its name.
– Harassed Dad
Nov 13 at 13:43




But you don't need to select or activate a workbook or sheet to access its data through vba - just specify its name.
– Harassed Dad
Nov 13 at 13:43

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263675%2factivate-an-excel-sheet-using-an-excel-userform%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Museum of Modern and Contemporary Art of Trento and Rovereto

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully