首页 > 解决方案 > 如何将变量分配给具有部分已知名称的 Excel 工作表

问题描述

有人可以帮我解决以下问题:

我对“ Set ch1 =”有疑问

文件名(或者更确切地说,文件名的最后几个字符每天都会略有变化),因此我需要添加通配符 (*) 或类似的东西。

如何使用名称更改的文件“设置 ch1 =”?

这是我开发的部分代码。

Option Explicit
Sub Open_Latest_Cheque()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Call Open_Latest_File

Dim ch1 As Workbook, AC As Workbook, temp As Workbook
Dim ch1ws As Worksheet, ACws As Worksheet
Dim ACwsLR As Long, tempName As String

**Set ch1 = Workbooks("Sum100123.csv")**
Set ch1ws = ch1.Sheets(1)
Set AC = Workbooks("Mon.xlsm")
Set ACws = AC.Sheets("Data")

Dim MyPath  As String, MyFile  As String, LatestFile  As String
Dim LatestDate  As Date, LMD  As Date.............

谢谢

标签: vbaexcel

解决方案


尝试这个:

Sub test_partial_name()

Dim pt_nm As Workbook

For Each pt_nm In Application.Workbooks
   If (pt_nm.Name) Like "Sum1#123.csv" Then
Exit For
End If

Next pt_nm
   If Not pt_nm Is Nothing Then
   pt_nm.Activate
Else
MsgBox "The file has not opened!"
End If

With Sheets(1)

Cells(1, 1).Value = "its working? Yes"

End With

推荐阅读