我使用以下代码打开数据并将数据从一个工作簿复制到另一个工作簿。此代码来自我们使用的另一个文件,该文件由不再与我们合作的人完成。
我需要添加代码,以便从要打开的文件中提取数据的Tab名称位于单元格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
答案 0 :(得分: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)
希望这就是你所需要的。