【问题标题】:Referencing different types of cells-(ie. strings, doubles)-multiple workbooks引用不同类型的单元格——(即字符串、双精度)——多个工作簿
【发布时间】:2012-03-23 12:22:54
【问题描述】:

首先,放轻松,我是新手。我需要从我创建并保存为多个工作簿的模板工作簿中引用不同类型的单元格(即字符串、双精度)。我已经想出了如何将值或字符串传输到我的主文件,但我希望能够打开其中一个工作簿并更改一个值,因此也可以更改主文件。此外,主工作簿保存在与其他工作簿不同的位置。任何帮助将不胜感激。

谢谢

Sub HyperLink()

   'This macro creates a new excel workbook based on the selected part number of the master file.
   'It also fills in three cells in the newly created excel file based on the master.
   'Certain cells in the newly created workbook are referenced back to the master
   'Finally the part number in the master excel is hyperlinked to the newly created excel

    Dim PartNumber As String
    Dim EAU As Integer
    Dim CurrentMaterial As Double
    Dim SimForging As String
    Dim EstForging As String
    Dim ProcessSavings As String

    ColumnLocation = ActiveCell.Column
    CellLocation = ActiveCell.Address

    PartNumber = Cells(ActiveCell.Row, ActiveCell.Column) 'Defines the part number for the active cell
    EAU = ActiveCell.Offset(0, 1).Value  'Defines the EAU for the selected part number (column C)
    CurrentMaterial = ActiveCell.Offset(0, 2).Value 'Defines the Mat. Cost for the selected part number (column D)

    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    If IsNull(PartNumber) = False Then
        Workbooks.Add
        FilePath = "C:\Documents and Settings\Brandon's\Desktop\Forging Justification\" & PartNumber & ".xlsx"
        Workbooks.Open Filename:="C:\Documents and Settings\Brandon's\Desktop\Forging Justification\Template.xls"
        Range("A2").Select
        ActiveWorkbook.SaveAs Filename:=FilePath, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        Range("A2").Select
        ActiveCell.Value = PartNumber
        ActiveCell.Offset(4, 0).Value = EAU
        ActiveCell.Offset(0, 3).Value = CCur(CurrentMaterial)

        'I dont know what to use here?????????????????????
        SimForging = Range("A8").FormulaR1C1
        ProcessSavings = Range("C11").FormulaR1C1
        EstForging = Range("F2").FormulaR1C1

        ActiveWorkbook.Save
        ActiveWindow.Close
        ActiveWindow.Close

        ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="Pricing\" & PartNumber & ".xlsx", TextToDisplay:=PartNumber

        'Again I don't know what to do here in order to make it dynamic????????????
        'I want to be able to change the EstForging in PartNumber.xlsx and have it update the master file
        ActiveCell.Offset(0, 4) = EstForging
        ActiveCell.Offset(0, 5) = SimForging
        ActiveCell.Offset(0, 7) = ProcessSavings

        ActiveWorkbook.Save

        With Application
            .ScreenUpdating = True
            .EnableEvents = True
            .Calculation = CalcMode
        End With
    End If

End Sub

悉达,

感谢您的回复,它应该像这样工作。

选择我要运行宏的单元格(PartNumber 即 1234)并启动宏

1) 定义 PartNumber、EAU 和 CurrentMaterial
PartNumber = 单元格(ActiveCell.Row,ActiveCell.Column) EAU = ActiveCell.Offset(0, 1).Value
CurrentMaterial = ActiveCell.Offset(0, 2).Value

2) 打开我创建的模板。 Workbooks.Open Filename:="X:\New Parts - Miller, Crum\Forging Justification\Template.xls"

3) 将 Template.xls 保存为新文件(即 1234.xlsx)
"C:\Documents and Settings\Brandon's\Desktop\Forging Justification\" & PartNumber & ".xlsx"

4) 将 PartNumber、EAU 和 CurrentMaterial 放入 1234.xlsx
范围(“A2”)。选择
ActiveCell.Value = PartNumber
ActiveCell.Offset(3, 0).Value = EAU
ActiveCell.Offset(0, 3).Value = CCur(CurrentMaterial)

5) 在 1234.xlsx 中定义 SimForging、EstForging 和 ProcessSavings
SimForging = ActiveCell.Offset( , )
EstForging = ActiveCell.Offset( , )
ProcessSavings = ActiveCell.Offset( , )

6) 保存 1234.xlsx 并关闭它。

7) 再次激活主文件后,将 Simforging、EstForging 和 ProcessSavings 与 PartNumber 放在同一行
ActiveCell.Offset( , ) = SimForging
ActiveCell.Offset( , ) = EstForging
ActiveCell.Offset( , ) = ProcessSavings

8) 将 PartNumber 单元格的超链接添加到我刚刚创建的文件 (1234.xlsx)
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="Pricing\" & PartNumber & ".xlsx",_TextToDisplay:=PartNumber

9) 保存主工作簿并停止宏。

然后我希望能够点击超链接并打开 1234.xlsx。
然后更改 1234.xlsx 中的 ProcessSavings,保存并关闭它。
我希望能够看到主工作簿中的更改。

现在我只能将值从 1234.xlsx 带过来,但是当我更改它们时,它不会在主文件中更新???? EstForging 和 ProcessSavings 是双倍的,而 SimForging 是一个字符串。

【问题讨论】:

  • 您能否澄清第 5 点?您要从哪里检索值?
  • 我正在尝试将新创建的 excel(即 1234.xlsx)中的单元格引用回主工作簿。所以在主工作簿中,我会做这样的事情 ActiveCell.Offset(0,23) = "='C:\Documents and Settings\Brandon's\Desktop\Forging Justification[1234.xlsx]Sheet1'!$C$11"
  • 那么您是否要将该公式存储在 SimForging 中?

标签: excel vba


【解决方案1】:
   'I dont know what to use here?????????????????????
    SimForging = Range("A8").FormulaR1C1
    ProcessSavings = Range("C11").FormulaR1C1
    EstForging = Range("F2").FormulaR1C1

这是你正在尝试的吗?

    'I dont know what to use here?????????????????????
    SimForging = Range("A8").Value
    ProcessSavings = Range("C11").Value
    EstForging = Range("F2").Value

我对您的要求感到困惑。您能否逐点说明宏的行为方式。我的意思是指定宏应该采取的步骤。在解释中使用文件名和单元格地址。看看这个例子

1) 启动宏

2) 从当前工作簿的活动单元格中获取 PartNumber、EAU、CurrentMaterial

3) 使用

决定文件名

"C:\Documents and Settings\Brandon's\Desktop\Forging Justification\" & PartNumber & ".xlsx"

4) 打开主文件"C:\Documents and Settings\Brandon's\Desktop\Forging Justification\Template.xls"

等等……

一旦我们了解您的确切需求,我们将能够以更好的方式为您提供帮助。 :)

席德

【讨论】:

  • 是的,我试过 ProcessSavings = Range("C11").Value 但它不会更新
  • @user1251120:请参阅我在您的问题中发布的评论。
猜你喜欢
  • 2021-06-20
  • 2019-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多