Help - create a button to ask the user to input the name of the sheet

jimmygogo

New Member
Joined
Mar 29, 2022
Messages
18
Office Version
  1. 365
Platform
  1. Windows
Hi there, I am creating a VBA code which copies data to my sheet from various workbooks stored in another folder. The excel files in the workbook have many sheet. I need to create a button where user is asked to input the name of the sheet where the code copies data from ? Currently, I have setup like this. Instead of having the sheet name in the code, I want user to input it.

Do While myFile <> ""
Workbooks.Open Filename:=(myfolder & myFile), UpdateLinks:=0
a = ActiveWorkbook.Sheets("09-01-2022").Range("B13").Text
x = ActiveWorkbook.Sheets("09-01-2022").Range("N13").Value
y = ActiveWorkbook.Sheets("09-01-2022").Range("N14").Value

Thank you a lot in advance
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try:
VBA Code:
Dim sh As String
sh = InputBox("Enter the name of the sheet.")
If sh = "" Then Exit Sub
Do While myFile <> ""
    Workbooks.Open Filename:=(myfolder & myFile), UpdateLinks:=0
    With ActiveWorkbook.Sheets(sh)
        a = .Range("B13").Text
        x = .Range("N13").Value
        y = .Range("N14").Value
    End With
Loop
 
Upvote 0
Try:
VBA Code:
Dim sh As String
sh = InputBox("Enter the name of the sheet.")
If sh = "" Then Exit Sub
Do While myFile <> ""
    Workbooks.Open Filename:=(myfolder & myFile), UpdateLinks:=0
    With ActiveWorkbook.Sheets(sh)
        a = .Range("B13").Text
        x = .Range("N13").Value
        y = .Range("N14").Value
    End With
Loop
Thank you! works perfect - Can I ask you a favor ? Can you help me make my code better as I am very new at it. Its working now but I feel it can be cleaned up.

Basically, my sheets looks at the employees timesheet in a folder and gets total time spent (Column N) on each project (column B). As I am using absolute reference, its showing me projects with zero hours.

Do While myFile <> ""
Workbooks.Open Filename:=(myfolder & myFile), UpdateLinks:=0
With ActiveWorkbook.sheets(sh)
a = .Range("B13").Text
x = .Range("N13").Value
b = .Range("B14").Text
y = .Range("N14").Value
c = .Range("B15").Text
d = .Range("N15").Value
e = .Range("B16").Text
f = .Range("N16").Value
g = .Range("B17").Text
h = .Range("N17").Value
k = .Range("B18").Text
l = .Range("N18").Value
m = .Range("B19").Text
n = .Range("N19").Value
z = myFile
End With

ActiveWorkbook.Close SaveChanges:=False

Windows("cook.xltm").Activate
ActiveWorkbook.sheets("cook").Cells(i, 2).Value = a
ActiveWorkbook.sheets("cook").Cells(i, 3).Value = x
ActiveWorkbook.sheets("cook").Cells(i, 4).Value = b
ActiveWorkbook.sheets("cook").Cells(i, 5).Value = y
ActiveWorkbook.sheets("cook").Cells(i, 6).Value = c
ActiveWorkbook.sheets("cook").Cells(i, 7).Value = d
ActiveWorkbook.sheets("cook").Cells(i, 8).Value = e
ActiveWorkbook.sheets("cook").Cells(i, 9).Value = f
ActiveWorkbook.sheets("cook").Cells(i, 10).Value = g
ActiveWorkbook.sheets("cook").Cells(i, 11).Value = h
ActiveWorkbook.sheets("cook").Cells(i, 12).Value = k
ActiveWorkbook.sheets("cook").Cells(i, 13).Value = l
ActiveWorkbook.sheets("cook").Cells(i, 14).Value = m
ActiveWorkbook.sheets("cook").Cells(i, 15).Value = n
ActiveWorkbook.sheets("cook").Cells(i, 18) = z


myFile = Dir
i = i + 1

Loop

Result I am getting -

1648669141155.png
 
Upvote 0
@jimmygogo The following is a shorter, hopefully less confusing version of the code you posted:

VBA Code:
    Dim ArrayRow                As Long, RowCount           As Long
    Dim Column_B_Array          As Variant, Column_N_Array  As Variant
    Dim OutputArray(1 To 14)    As Variant
'
'-------------------------------------------------------------------
'
    Do While myFile <> ""
        Workbooks.Open Filename:=(myfolder & myFile), UpdateLinks:=0
'
        With ActiveWorkbook.Sheets(sh)
' Load Column_B_Array
            Column_B_Array = Range("B13:B19")
' Load Column_N_Array
            Column_N_Array = Range("N13:N19")
'
            Z = myFile
        End With
'
        ActiveWorkbook.Close SaveChanges:=False
'
'-------------------------------------------------------------------
'
' Create OutputArray
        RowCount = -1
'
        For ArrayRow = 1 To 7
            RowCount = RowCount + 2
'
                OutputArray(RowCount) = Column_B_Array(ArrayRow, 1)
            OutputArray(RowCount + 1) = Column_N_Array(ArrayRow, 1)
        Next
'
'-------------------------------------------------------------------
'
        Windows("cook.xltm").Activate
'
' Display OutputArray
        ActiveWorkbook.Sheets("cook").Cells(i, 2).Resize(1, UBound(OutputArray)) = OutputArray
'
        ActiveWorkbook.Sheets("cook").Cells(i, 18) = Z
'
        myFile = Dir
'
        i = i + 1
    Loop

Let us know where you are at after trying that in place of the code you submitted.
 
Upvote 0
Try:
VBA Code:
Sub test()
    Dim srcWB As Workbook, srcWS As Worksheet
    Do While myFile <> ""
        Set srcWB = Workbooks.Open(myfolder & myFile, UpdateLinks:=0)
        Set srcWS = Sheets(sh)
        With Workbooks("cook.xlsm").Sheets("cook")
            .Cells(.Rows.Count, "B").End(xlUp).Offset(1).Resize(, 14).Value = Array(srcWS.Range("B13"), srcWS.Range("N13"), srcWS.Range("B14"), _
                srcWS.Range("N14"), srcWS.Range("B15"), srcWS.Range("N15"), srcWS.Range("B16"), srcWS.Range("B16"), srcWS.Range("B17"), _
                srcWS.Range("N17"), srcWS.Range("B18"), srcWS.Range("N18"), srcWS.Range("B19"), srcWS.Range("N19"))
            .Cells(.Rows.Count, "R").End(xlUp).Offset(1) = myFile
        End With
        srcWB.Close False
        myFile = Dir
    Loop
End Sub
 
Upvote 0
@jimmygogo The following is a shorter, hopefully less confusing version of the code you posted:

VBA Code:
    Dim ArrayRow                As Long, RowCount           As Long
    Dim Column_B_Array          As Variant, Column_N_Array  As Variant
    Dim OutputArray(1 To 14)    As Variant
'
'-------------------------------------------------------------------
'
    Do While myFile <> ""
        Workbooks.Open Filename:=(myfolder & myFile), UpdateLinks:=0
'
        With ActiveWorkbook.Sheets(sh)
' Load Column_B_Array
            Column_B_Array = Range("B13:B19")
' Load Column_N_Array
            Column_N_Array = Range("N13:N19")
'
            Z = myFile
        End With
'
        ActiveWorkbook.Close SaveChanges:=False
'
'-------------------------------------------------------------------
'
' Create OutputArray
        RowCount = -1
'
        For ArrayRow = 1 To 7
            RowCount = RowCount + 2
'
                OutputArray(RowCount) = Column_B_Array(ArrayRow, 1)
            OutputArray(RowCount + 1) = Column_N_Array(ArrayRow, 1)
        Next
'
'-------------------------------------------------------------------
'
        Windows("cook.xltm").Activate
'
' Display OutputArray
        ActiveWorkbook.Sheets("cook").Cells(i, 2).Resize(1, UBound(OutputArray)) = OutputArray
'
        ActiveWorkbook.Sheets("cook").Cells(i, 18) = Z
'
        myFile = Dir
'
        i = i + 1
    Loop

Let us know where you are at after trying that in place of the code you submitted.
I tried but I am getting the following error

1648737928626.png
 
Upvote 0
Try:
VBA Code:
Sub test()
    Dim srcWB As Workbook, srcWS As Worksheet
    Do While myFile <> ""
        Set srcWB = Workbooks.Open(myfolder & myFile, UpdateLinks:=0)
        Set srcWS = Sheets(sh)
        With Workbooks("cook.xlsm").Sheets("cook")
            .Cells(.Rows.Count, "B").End(xlUp).Offset(1).Resize(, 14).Value = Array(srcWS.Range("B13"), srcWS.Range("N13"), srcWS.Range("B14"), _
                srcWS.Range("N14"), srcWS.Range("B15"), srcWS.Range("N15"), srcWS.Range("B16"), srcWS.Range("B16"), srcWS.Range("B17"), _
                srcWS.Range("N17"), srcWS.Range("B18"), srcWS.Range("N18"), srcWS.Range("B19"), srcWS.Range("N19"))
            .Cells(.Rows.Count, "R").End(xlUp).Offset(1) = myFile
        End With
        srcWB.Close False
        myFile = Dir
    Loop
End Sub
Thanks for sending this. I did try this but I am getting this error

1648738311885.png
 
Upvote 0
Can this code be programmed to only select text (project number) in column B if there is value in column N (amount of hours) ?
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,591
Members
449,089
Latest member
Motoracer88

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