2016-08-05 133 views
-1

下面的代码在我的活动工作表中复制“ADXL364”工作表,但如果它包含“XL364”或“364”如果工作表包含使用VBA的特定字符串,请引用单元格

如果我把星号'C:\ data [adxl364.xls] * ADXL364_QC'!A1放入我的代码中,它不起作用。

Sub GetRange() 
    With Range("A:Z") 
     .Formula = "=If('C:\data\[adxl364.xls]ADXL364_QC'!A1 > 0,'C:\data\[adxl364.xls]ADXL364_QC'!A1,Text(,))" 
     .Formula = .Value 
    End With 
    End Sub 

长的代码将被从用户获取文件的位置,然后复制包含ADXL364XL364

With ActiveWorkbook 
    Sheets.Add.Name = "Flow_table" 
    Application.EnableEvents = False 

    TP_location = Left(TextBox1.Value, InStrRev(TextBox1.Value, "\")) 
    TP_filename = Right(TextBox1.Value, Len(TextBox1.Value) - InStrRev(TextBox1.Value, "\")) 
    TP_filename = "[" & TP_filename & "]" 
    TP_formula = "'" & TP_location & TP_filename & TextBox2.Value & "'!A1" 

    getcellvalue = "=if(" & TP_formula & ">0," & TP_formula & "," & """"")" 

     With Range("A:Z") 
     .Formula = getcellvalue 
     .Formula = .Value 
     End With 

    Sheets.Add.Name = "Job_lists" 

End With 
Unload UserForm2 
End Sub 
+0

哪里复制发生? –

+0

与细胞A1,写在该小区的公式是=如果( 'C:\数据\ [adxl364.xls] ADXL364_QC' A1> 0, 'C:\数据\ [adxl364.xls] ADXL364_QC'!A1,” “)。循环发生在A到Z之间。因此,正确的术语可能是从已关闭的工作簿中引用单元格。 –

+0

您可以尝试编辑路径位置并选择要“引用”的工作表并将该模块粘贴到新的工作表中。它会给你一份参考表。在这种情况下,ADXL364表,范围A到Z –

回答

0

一个丑陋的,但可能的方式将是一个暴力错误捕获技术。

然而,更优雅的解决方案可能是使用ADO。例如,您可以运行两个“查询”:表格模式中的第一个将为您指定文件中的工作表名称,第二个用于找到的工作表名称。这将产生一个RecordSet,其中包含可以使用.CopyFromRecordset方法直接写入Range的封闭页的数据。当然,你可以运行第一个查询来找到你的工作表名称,然后像在发布的代码中一样继续。

下面的例子显示了两个查询的代码。这是所有迟到的,所以你不需要参考ADO库,但我会把这个决定留给你。我已经在模块的顶部放置了一些常量,这些常量可能需要更改,具体取决于您拥有的Excel版本。您还需要编写自己的错误处理(特别是关闭连接),但是,我将再次为您保留该错误处理。

Option Explicit 
Private Const SCHEMA_TABLES As Integer = 20 
Private Const OPEN_FORWARD_ONLY As Integer = 0 
Private Const LOCK_READ_ONLY As Integer = 1 
Private Const CMD_TEXT As Long = 1 
Private Const PROVIDER As String = "Microsoft.ACE.OLEDB.12.0" 
Private Const XL_PROP As String = """Excel 12.0;HDR=No""" 
Private Const SHEETS_FIELD_NAME As String = "TABLE_NAME" 

Public Sub AcquireData() 
    Dim fPath As String 
    Dim fName As String 
    Dim key As String 
    Dim addr As String 
    Dim oConn As Object 
    Dim oRS As Object 
    Dim connString As String 
    Dim sql As String 
    Dim found As Boolean 
    Dim sheetField As String 

    'Define the path and file name 
    fPath = "C:\Users\User\Documents\StackOverflow" 
    fName = "closed_book.xlsx" 

    'Define the search key 
    key = "XL364" 

    'Define the address of closed worksheet 
    'If reading one cell then use [address:address], eg "A1:A1" 
    addr = "A1:E5" 

    'Late bind the ADO objects 
    Set oConn = CreateObject("ADODB.Connection") 
    Set oRS = CreateObject("ADODB.Recordset") 

    'Open conection 
    connString = "Provider=" & PROVIDER & ";" & _ 
       "Data Source=" & fPath & "\" & fName & ";" & _ 
       "Extended Properties=" & XL_PROP & ";" 

    oConn.Open connString 

    'Search for the sheet name containing your key 
    'in the tables (ie sheets) schema 
    found = False 
    oRS.Open oConn.OpenSchema(SCHEMA_TABLES) 
    Do While Not oRS.EOF 
     sheetField = oRS.Fields(SHEETS_FIELD_NAME).Value 
     If InStr(sheetField, key) > 0 Then 
      found = True 
      Exit Do 
     End If 
     oRS.MoveNext 
    Loop 
    oRS.Close 

    'Read the target data 
    If found Then 
     sql = "SELECT * FROM [" & _ 
       sheetField & addr & "];" 

     oRS.Open sql, oConn, OPEN_FORWARD_ONLY, LOCK_READ_ONLY, CMD_TEXT 

     'Write the data to your worksheet 
     If Not oRS.EOF Then 
      ThisWorkbook.Worksheets("Sheet1").Range("A1") _ 
       .CopyFromRecordset oRS 
     End If 

    End If 


    'Housekeeping 
    oRS.Close 
    Set oRS = Nothing 
    oConn.Close 
    Set oConn = Nothing 

End Sub 
+0

这很酷。 :) 非常感谢。 –

0

您可以测试文本“XL364”是一个工作表在表格名称中循环显示每张表格并使用InStr(在字符串中)功能。例如:

For Each ws in Workbooks.Open(filepathStringFromUserInput) 
    If InStr(1, ws.Name, "XL364") > 0 Then 
     MsgBox "hi" 
     'Set hwSheet = ws 
    End If 
Next ws 


With hwSheet 
    'do some code eg: 
    .Range("A1").Value = "Hi" 
End With 
+0

你好@Oliver Carr,ADXL364.xls是一个封闭的工作簿。所以我认为代码不会工作。 –

+0

@J_Gonzales我不认为有一种方法可以按照您要查找的方式查看工作表。试试我编辑的答案来打开指定的文件并循环查找。 – Carrosive

相关问题