使用用户输入进行操作

时间:2019-07-07 18:54:15

标签: excel vba

I want to make open worksheets which user write in inputbox. There are 20 worksheets to choose from. without declaring each worksheet in code for ex. If userentry= ".xlsx" for each worksheet, How can I do it easy way?

I wrote vba code for each worksheet but wrote worksheet name manually.

例程openworksheet()要求用户输入要打开的工作簿的名称。例程Copy_OperationalDowntime_Data()复制数据并粘贴到vba打开文件中,然后复制data.xlsm文件。对所有子例程执行相同的复制和粘贴操作,并将数据粘贴到vba打开文件和复制data.xlsm文件中。

Public Sub openworksheet()
'open RTM
Dim usersel, userentry, worksheetpath, worksheetname, nameofws As String
    usersel = MsgBox("enter the date of the worksheet you would like to open") & vbCrLf & _
                    "enter file name in the format 20 Jun 19 RTM.xlsm"
    userentry = InputBox(usersel, vbOKCancel)
    worksheetpath = "C:\Users\DESHARMA\Desktop\"
    worksheetname = worksheetpath & userentry
    nameofws = worksheetname
    Workbooks.Open Filename:=nameofws
End Sub

Public Sub Copy_OperationalDowntime_Data()
    Dim keepdata As String
    openworksheet
    ActiveWorkbook.Activate
    Worksheets("Main Menu").Select
    Worksheets("Avail L1 DAYS").Select
    Range("G5").Select
         For i = 5 To 64
              If Cells(i, 7) <> "" Then
                    Range(Cells(i, 7), Cells(i, 21)).Select
                    Selection.Copy
                    Workbooks("vba open file and copy data.xlsm").Activate
                    ActiveSheet.Range("b2").Select
                        If Range("b2").Value <> "" Then
                            ActiveCell.Offset(1, 0).Select
                         End If
              ActiveSheet.Paste
            Else
               ActiveCell.Offset(1, 0).Select
            End If
            Windows("nameofws").Activate
            Worksheets("Avail L1 DAYS").Select
       Next i
'Copy L2 days data
       Worksheets("Avail L2 DAYS").Select
             Range("G5").Select
              For i = 5 To 64
                   If Cells(i, 7) <> "" Then
                         Range(Cells(i, 7), Cells(i, 21)).Select
                         Selection.Copy
                         Workbooks("vba open file and copy data.xlsm").Activate
                         Range("b2").Select
                         Range("b2").End(xlDown).Select
                         Selection.End(xlUp).Select
                         Selection.Offset(2, 0).seelct
                         ActiveSheet.Paste
                 Else
                    ActiveCell.Offset(1, 0).Select
                 End If
                 Windows("nameofws").Activate
                 Worksheets("Avail L2 DAYS").Select
            Next i
    'Workbooks("vba open file and copy data.xlsm").Activate
    'keepdata = MsgBox("Do you want to keep data or delete it" & vbCrLf & _
     '               "To keep data,Press Yes", vbYesNo, "Keep or Delete data")

'copy L1 Nights data
        Worksheets("Avail L1 Nights").Select
             Range("G5").Select
              For i = 5 To 64
                   If Cells(i, 7) <> "" Then
                         Range(Cells(i, 7), Cells(i, 21)).Select
                         Selection.Copy
                         Workbooks("vba open file and copy data.xlsm").Activate
                         Range("b2").Select
                         Range("b2").End(xlDown).Select
                         Selection.End(xlUp).Select
                         Selection.Offset(2, 0).seelct
                         ActiveSheet.Paste
                 Else
                    ActiveCell.Offset(1, 0).Select
                 End If
                 Windows("nameofws").Activate
                 Worksheets("Avail L1 Nights").Select
            Next i

'copy L2 Nights data
        Worksheets("Avail L2 Nights").Select
             Range("G5").Select
              For i = 5 To 64
                   If Cells(i, 7) <> "" Then
                         Range(Cells(i, 7), Cells(i, 21)).Select
                         Selection.Copy
                         Workbooks("vba open file and copy data.xlsm").Activate
                         Range("b2").Select
                         Range("b2").End(xlDown).Select
                         Selection.End(xlUp).Select
                         Selection.Offset(2, 0).Select
                         ActiveSheet.Paste
                 Else
                    ActiveCell.Offset(1, 0).Select
                 End If
                 Windows("nameofws").Activate
                 Worksheets("Avail L2 Nights").Select
            Next i
          Workbooks("vba open file and copy data.xlsm").Activate

End Sub


I am not sure how to refer user entered workbook name.

0 个答案:

没有答案