简体   繁体   English

如何在VBA中调整和设置变量工作簿名称?

[英]How to Dim and Set a variable workbook name in VBA?

I have a workbook that gets created every week that has a variable name structure. 我有一个每周都会创建的工作簿,该工作簿具有可变的名称结构。 The name structure is as follows : Week of Year & Invoice & date . 名称结构如下: Week of YearInvoicedate So a sample file might be called 1_Invoice_01052018.xlsm 因此,示例文件可能称为1_Invoice_01052018.xlsm

I have to update the report every week. 我必须每周更新报告。 I want to declare the variable workbook name as a variable in VBA. 我想在VBA中将变量工作簿名称声明为变量。 I have another workbook that contains the output of the report that is created via VBA. 我还有另一个工作簿,其中包含通过VBA创建的报告的输出。 In this other workbook I want to be able to call the Invoice spreadsheet but since it has a variable name, I am having issues finding it. 在此其他工作簿中,我希望能够调用Invoice电子表格,但是由于它具有变量名,因此在查找它时遇到了问题。 So I put together the VBA below. 因此,我在下面整理了VBA。

Sub Test()

Dim wb As Workbook
Dim ws As Worksheet

Set wb = "*Invoice*" & ".xlsm"
Set ws = Sheets("Sheet1")

wb.Activate
ws.Select

End Sub

However, this results in a "Type mismatch" error. 但是,这导致“类型不匹配”错误。

I also tried the following: 我还尝试了以下方法:

Sub Test2()

Windows("*Invoice*" & ".xlsm").Activate

End Sub

This also resulted in an error. 这也导致了错误。

Any ideas on how to set a variable workbook name as a variable in VBA? 关于如何在VBA中将变量工作簿名称设置为变量的任何想法? I would only have one of these workbooks open at a time, so I wouldn't run into any issues 我一次只能打开其中一本工作簿,所以不会遇到任何问题

You have to set the workbook correctly: 您必须正确设置工作簿:

Sub Test()

  Dim wb As Workbook
  Dim ws As Worksheet

  Dim FilePath As Sring
  FilePath = "C:\" & AnotherVariable & ".xlsx"

  Set wb = Workbooks(FilePath)
  Set ws = Sheets("Sheet1")

  wb.Activate
  ws.Select

End Sub

To create a new workbook you'd use Workbooks.Add . 要创建一个新的工作簿,您可以使用Workbooks.Add To open an existing one you'd use Workbooks.Open and then reference the worksheet within that workbook. 要打开现有的工作簿 ,请使用Workbooks.Open ,然后在该工作簿中引用工作表。
To change the name of the file you'd save it with a new name using the SaveAs method. 要更改文件名,您可以使用SaveAs方法将其保存为新名称。

Sub Test()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim FileName As String

    Set wb = Workbooks.Add 'Creates a new workbook with default name.
    'Set wb = Workbooks.Open("<path to folder>\" & FileName & ".xlsm") 'Open an existing file.

    Set ws = wb.Worksheets("Sheet1")

    wb.SaveAs "<path to folder>\" & FileName & ".xlsm" 'Save and rename here.

    With ws
        .Range("A1") = "Adding some text to this cell"
    End With

End Sub  

As a further example, the code below will create two workbooks before copying the sheet from the first workbook to the end of the second workbook. 作为另一个示例,下面的代码将在将工作表从第一个工作簿复制到第二个工作簿的末尾之前创建两个工作簿。

Sub Test1()

    Dim wb As Workbook, wb1 As Workbook
    Dim ws As Worksheet

    'Create first workbook so it contains only 1 sheet (xlWBATWorksheet)
    ', reference Sheet1 and add some data to it.
    Set wb = Workbooks.Add(xlWBATWorksheet)
    Set ws = wb.Worksheets("Sheet1")
    ws.Range("A1") = "This cell populated in first workbook."

    'Create second workbook with default number of sheets
    'and copy Sheet1 from first book to the end of this one.
    Set wb1 = Workbooks.Add
    ws.Copy After:=wb1.Sheets(wb1.Sheets.Count)

End Sub  

Edit again: 再次编辑:

To figure out the workbook name based on WeekNumber_Invoice_Date you could use: 要根据WeekNumber_Invoice_Date找出工作簿名称,可以使用:

Sub Test2()

    Dim wb As Workbook
    Dim sPath As String
    Dim dDate As Date

    dDate = Date 'Todays date
    sPath = "C:\MyFolder\"
    sPath = sPath & _
        WorksheetFunction.WeekNum(dDate, 2) & "_Invoice_" & Format(dDate, "ddmmyyyy") & ".xlsm"

    'Open if already exists.
    'Set wb = Workbooks.Open(sPath)

    'Create and SaveAs new name.
    Set wb = Workbooks.Add
    wb.SaveAs sPath

End Sub

This would give a file path of C:\\MyFolder\\43_Invoice_22102018.xlsm based on todays date of 22nd October '18 . 根据今天的18年10月22日,这将给出一个文件路径C:\\MyFolder\\43_Invoice_22102018.xlsm

Note: The WEEKNUM function considers the week containing January 1 to be the first week of the year. 注意: WEEKNUM函数将包含1月1日的一周视为一年的第一周。

I was able to get what I need from the following link: 我可以从以下链接中获得所需的信息:
excel-vba-extract-text-between-2-characters Excel中的VBA提取文本之间的两个字符

I reviewed the link above and put together the VBA below. 我查看了上面的链接,并将下面的VBA放在一起。

Sub test2()

Dim str As String
Dim openPos As Integer
Dim closePos As Integer
Dim midBit As String

str = Range("b1").Value
openPos = InStr(str, "[")
closePos = InStr(str, "]")
midBit = Mid(str, openPos + 1, closePos - openPos - 1)

'MsgBox (midBit)
Windows(midBit).Activate

End Sub

I ended up creating a dynamic file path in cell B1 that contained a concatenated file path string that contained look ups to pull in the Week of Year and Date based on the Current Date . 我最终在单元格B1中创建了一个动态文件路径,该文件路径包含一个串联的文件路径字符串,该字符串包含了根据“ Current Date引入Week of Year和“ Date ”的Current Date Since this path is dynamic it will always point to the right path given that I open the Invoice on the correct week. 由于此路径是动态的,因此,只要我在正确的星期打开Invoice ,它将始终指向正确的路径。 I pulling the file name from the path and opening based on the file name which is dynamic. 我从路径中拉出文件名并基于动态文件名打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM