User Prompt to Select Worksheet to Import from Selected Workbook

wdp74

New Member
Joined
Jun 24, 2021
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I have a macro that allows the user to select a workbook and specific sheet to import (sheet names are not consistent). However, running into the coding I have that errors when a sheet number is greater than 9 (sheet10, 11, 12 etc..). This is the code after the user has selected the workbook to open. Help!

With ActiveWorkbook
For i = 1 To .Worksheets.Count
msg = msg & "(" & i & ") " & .Sheets(i).Name & vbCrLf
Next i

response = InputBox(msg, "Type the number to select the wsFile worksheet to import")

If response = Null Then Exit Sub

For x = 1 To ActiveWorkbook.Worksheets.Count
If InStr(response, x) > 0 Then
Dim wsFile As Worksheet
Set wsFile = ActiveWorkbook.Sheets(x)
 

Attachments

  • 2021-06-24_17-45-54.jpg
    2021-06-24_17-45-54.jpg
    24.7 KB · Views: 14

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hello wdp74, welcome to the MrExcel message board!

Within your code, replace this
Code:
If InStr(response, x) > 0 Then

by this
VBA Code:
If x = CLng(response) Then
and it should be fine.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,988
Messages
6,122,620
Members
449,092
Latest member
amyap

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top