Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign variable to an Excel worksheet with a partially known name

Tags:

excel

vba

Can someone help me with the following:

I have an issue with "Set ch1 ="

The file name (or rather the last few characters of its name change slightly everyday), thus I need to add wildcard (*) or something similar.

How do I do "Set ch1 =" with a file, which name changes?

Here is part of the code I have developed.

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.............

Thanks

like image 501
West Avatar asked Nov 08 '22 01:11

West


1 Answers

Try this:

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
like image 60
Wheeliam Avatar answered Nov 15 '22 05:11

Wheeliam