引用不同类型的单元格(即字符串,双精度) - 多个工作簿

时间:2012-03-06 01:32:07

标签: excel-vba vba excel

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

由于

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

Siddarth,

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

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

1)定义PartNumber,EAU和CurrentMaterial
      PartNumber = Cells(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是一个字符串。

1 个答案:

答案 0 :(得分:0)

  
    
      
   '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"

等等......

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

西特