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

时间:2018-10-22 12:13:59

标签: excel vba variables

我有一个每周都会创建的工作簿,它具有可变的名称结构。名称结构如下:Week of YearInvoicedate。因此,示例文件可能称为1_Invoice_01052018.xlsm

我必须每周更新报告。我想在VBA中将变量工作簿名称声明为变量。我还有另一个工作簿,其中包含通过VBA创建的报告的输出。在另一个工作簿中,我希望能够调用Invoice电子表格,但是由于它具有变量名,因此在查找它时遇到了问题。因此,我在下面整理了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

但是,这将导致“类型不匹配”错误。

我还尝试了以下方法:

Sub Test2()

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

End Sub

这也导致了错误。

关于如何在VBA中将变量工作簿名称设置为变量的任何想法?我一次只能打开其中一个工作簿,所以不会遇到任何问题

3 个答案:

答案 0 :(得分:1)

您必须正确设置工作簿:

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

答案 1 :(得分:1)

要创建新工作簿,请使用Workbooks.Add。要打开现有文件,请使用Workbooks.Open,然后引用该工作簿中的工作表。
要更改文件名,您可以使用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  

作为另一个示例,下面的代码将创建两个工作簿,然后将工作表从第一个工作簿复制到第二个工作簿的末尾。

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  

再次编辑:

要找出基于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

这将基于今天的日期 18年10月22日给出C:\MyFolder\43_Invoice_22102018.xlsm的文件路径。

注意:WEEKNUM函数将包含1月1日的一周视为一年的第一周。

答案 2 :(得分:0)

我可以从以下链接中获得所需的内容:
excel-vba-extract-text-between-2-characters

我查看了上面的链接,并将下面的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

我最终在单元格B1中创建了一个动态文件路径,该文件路径包含一个串联的文件路径字符串,该字符串包含根据{{1 }}。由于此路径是动态的,因此只要我在正确的一周打开Week of Year,它就始终指向正确的路径。我从路径中拉出文件名,然后根据动态文件名打开文件。