0

我正在使用以下代码打开一个工作簿中的数据并将其复制到另一个工作簿中。这段代码来自我们使用的另一个文件,由不再与我们合作的人完成。

我需要添加到代码中,以便它从要打开的文件中提取数据的选项卡名称位于单元格 Y2 的工作表 1 上的当前工作簿中。我从中提取的工作簿有 14 个标签为 1-14 的标签,我在 Y2 中键入的数字应该确定要从中复制数据的标签的名称。带有 (????????????) 的代码是我需要帮助的地方。

我希望这是有道理的,比我想的更难解释。

谢谢!


Option Explicit

' ************************************************
' Variables For File Open Dialogue Box
' ************************************************
Public strDialogueFileTitle As String
Public strFilt As String
Public intFilterIndex As Integer
Public strCancel As String
Public strWorkbookNameAndPath As String
Public strWorkbookName As String
Public strWorksheetName As String

Public Sub Copy_TY()
Dim wkbVarianceWorkbook As Workbook
Dim wksVarianceWorksheett As Worksheet
Dim wkbImportedWorkbook As Workbook
Dim wksImportedWorksheet As Worksheet
Dim rngImportCopyRange As Range

Application.ScreenUpdating = False
Set wkbVarianceWorkbook = ThisWorkbook
Set wksVarianceWorksheett = Sheets("TY")

' ****************************************************************************
' Prompt In The Dialogue Box
' ****************************************************************************
intFilterIndex = 1
strDialogueFileTitle = "Select The Workbook You Want To Import"

' ****************************************************************************
' Present the Open File Dialogue To The User
' ****************************************************************************
Call OpenFileDialogue

' ****************************************************************************
' Notify The User If No File Was Successfully Opened
' ****************************************************************************
If strCancel = "Y" Then
    MsgBox ("An Open Error Occurred Importing Your File Selection")
    Exit Sub
End If

' ****************************************************************************
' Set Imported Workbook and Worksheet Variables
' ****************************************************************************
Set wkbImportedWorkbook = ActiveWorkbook
Set wksImportedWorksheet = wkbImportedWorkbook.Sheets("?????????")

' ****************************************************************************
' Copy The Data (Paste Special Will Be The Next Example)
' ****************************************************************************
Set rngImportCopyRange = Range(wksImportedWorksheet.Cells(2, 1), Cells(250, 1)).EntireRow
rngImportCopyRange.Copy wksVarianceWorksheett.Cells(2, 1)

wkbVarianceWorkbook.Activate
Application.DisplayAlerts = False
wkbImportedWorkbook.Close Savechanges:=False
Application.DisplayAlerts = True

wksVarianceWorksheett.Activate
wksVarianceWorksheett.Cells(1, 1).Select

Application.ScreenUpdating = True

Sheets("Variance").Select

End Sub

Private Sub OpenFileDialogue()

' ************************************************
' Display a File Open Dialogue Box For The User
' ************************************************
strCancel = "N"
strWorkbookNameAndPath = Application.GetOpenFilename _
    (FileFilter:=strFilt, _
     FilterIndex:=intFilterIndex, _
     Title:=strDialogueFileTitle)

' ************************************************
' Exit If No File Selected
' ************************************************
If strWorkbookNameAndPath = "" Then
    MsgBox ("No Filename Selected")
    strCancel = "Y"
    Exit Sub
ElseIf strWorkbookNameAndPath = "False" Then
    MsgBox ("You Clicked The Cancel Button")
    strCancel = "Y"
    Exit Sub
End If

' ******************************************************
' Now That You Have The User Selected File Name, Open It
' ******************************************************
Workbooks.Open strWorkbookNameAndPath
End Sub
4

1 回答 1

0

给你,替换这一行:

Set wkbImportedWorkbook = ActiveWorkbook
Set wksImportedWorksheet = wkbImportedWorkbook.Sheets("?????????")

通过这一行:

Dim wsName as String

wsName = wksVarianceWorksheet.Range("Y2").Value 'I only assumed you get Y2 value from this sheet which was declared in above. you can change and make another variable if your not pulling data from sheets TY
Set wkbImportedWorkbook = ActiveWorkbook
Set wksImportedWorksheet = wkbImportedWorkbook.Sheets(wsName)

希望这是你需要的。

于 2013-11-14T00:45:15.703 回答