0

I'm trying to activate a workbook with a partial name.

The workbook is always called "Task_State_(Pivot)_xxxxxx" the x's are random digits and these digits are what changes.

My code:

Sub changingWorkbooks()

Dim wb As Workbook
Dim excelfile As String

'deletes the table (have to eventually put that at the beginning of my macro)
Windows("macro").Activate
Sheets(1).Select
Rows("3:3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp

For Each wb In Application.Workbooks
    If Left(wb.Name, 10) = "Task_State" Then
        excelfile = wb.Name 
    End If
Next wb

'going from horasphere data status+date, making it readable by
' converting it with the comma and pasting it into your masterfile table.
'have to find a way to have the macro find the file without a name
' as the name will always change.
Workbooks(excelfile).Activate
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
    Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
    :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
    Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1 _
    ), Array(14, 1), Array(15, 1), Array(16, 1)), TrailingMinusNumbers:=True
'this top part is to make the data readable by going into Data - Text to columns - etc

I get a runtime error: type mismatch

My error occurs here: Workbooks(excelfile).Activate

Community
  • 1
  • 1
Joel Bastien
  • 121
  • 2
  • 11
  • Possible duplicate of [VBA recognizing workbook by partial name](https://stackoverflow.com/questions/30358293/vba-recognizing-workbook-by-partial-name) – BigBen Dec 20 '18 at 02:51
  • it works well. Where line occur error. – Dy.Lee Dec 20 '18 at 02:57
  • @dy.lee my error occurs here : Workbooks(excelfile).Activate – Joel Bastien Dec 20 '18 at 03:01
  • What is the value of `excelfile`? – TinMan Dec 20 '18 at 03:23
  • @tinman my goal is to have excel file be a string, and have that string associated with the unknown name of the excel file so I can easily refer to it as "excel file" instead of the name. – Joel Bastien Dec 20 '18 at 12:28
  • Okay, but was `excelfile` every assigned a value? Is `Windows("macro").Activate` the actual code ? – TinMan Dec 20 '18 at 12:39
  • @tinman no I guess excelfile was never actually assigned a value. And yes it is the actual code. Macro is the name of my excel file that I use. – Joel Bastien Dec 20 '18 at 12:46
  • I'm surprised that `Windows("macro").Activate` works without a file extension. I guess we found the problem. Did you mean to open the workbook? – TinMan Dec 20 '18 at 12:53
  • Well the workbook is already opened. What I want to do is ideally just activate the workbook. What would I need to do to solve this issue? – Joel Bastien Dec 20 '18 at 15:54
  • Have you tried using `Like` yet? Again, see [VBA recognizing workbook by partial name](https://stackoverflow.com/questions/30358293/vba-recognizing-workbook-by-partial-name). Note that you actually don't need to activate either. See [how to avoid using Select](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba/) – BigBen Dec 20 '18 at 17:05

0 Answers0