简体   繁体   English

使用 VB 宏修改电子表格

[英]Modifying a spreadsheet using a VB macro

I have two spreadsheets... when one gets modified in a certain way I want to have a macro run that modifies the second in an appropriate manner.我有两个电子表格......当一个电子表格以某种方式被修改时,我想要一个宏运行,以适当的方式修改第二个。 I've already isolated the event I need to act on (the modification of any cell in a particular column), I just can't seem to find any concrete information on accessing and modifying another spreadsheet (this spreadsheet is located on a different LAN share also... the user has access to both, though).我已经隔离了我需要采取行动的事件(修改特定列中的任何单元格),我似乎无法找到有关访问和修改另一个电子表格的任何具体信息(该电子表格位于不同的 LAN 上)也共享......用户可以访问两者,但是)。

Any help would be great.任何帮助都会很棒。 References on how to do this or something similar are just as good as concrete code samples.关于如何执行此操作或类似操作的参考与具体的代码示例一样好。

In Excel, you would likely just write code to open the other worksheet, modify it and then save the data.在 Excel 中,您可能只需编写代码即可打开另一个工作表,对其进行修改,然后保存数据。

See this tutorial for more info.有关更多信息,请参阅本教程

I'll have to edit my VBA later, so pretend this is pseudocode, but it should look something like:稍后我将不得不编辑我的 VBA,所以假设这是伪代码,但它应该看起来像:

Dim xl: Set xl = CreateObject("Excel.Application")
xl.Open "\\the\share\file.xls"

Dim ws: Set ws = xl.Worksheets(1)
ws.Cells(0,1).Value = "New Value"
ws.Save

xl.Quit constSilent

You can open a spreadsheet in a single line:您可以在一行中打开电子表格:

Workbooks.Open FileName:="\\the\share\file.xls"

and refer to it as the active workbook:并将其称为活动工作簿:

Range("A1").value = "New value"

Copy the following in your ThisWorkbook object to watch for specific changes.在您的ThisWorkbook对象中复制以下内容以查看特定更改。 In this case when you increase a numeric value to another numeric value.在这种情况下,当您将一个数值增加到另一个数值时。

NB: you will have to replace Workbook-SheetChange and Workbook-SheetSelectionChange with an underscore.注意:您必须用下划线替换Workbook-SheetChangeWorkbook-SheetSelectionChange Ex: Workbook_SheetChange and Workbook_SheetSelectionChange the underscore gets escaped in Markdown code.例如: Workbook_SheetChangeWorkbook_SheetSelectionChange下划线在 Markdown 代码中被转义。

Option Explicit
Dim varPreviousValue As Variant ' required for IsThisMyChange() . This should be made more unique since it's in the global space.


Private Sub Workbook-SheetChange(ByVal Sh As Object, ByVal Target As Range)
  ' required for IsThisMyChange()
  IsThisMyChange Sh, Target
End Sub

Private Sub Workbook-SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
  '  This implements and awful way of accessing the previous value via a global.
  '  not pretty but required for IsThisMyChange()
  varPreviousValue = Target.Cells(1, 1).Value ' NB: This is used so that if a Merged set of cells if referenced only the first cell is used
End Sub

Private Sub IsThisMyChange(Sh As Object, Target As Range)
  Dim isMyChange As Boolean
  Dim dblValue As Double
  Dim dblPreviousValue As Double

  isMyChange = False

  ' Simple catch all. If either number cant be expressed as doubles, then exit.
  On Error GoTo ErrorHandler
  dblValue = CDbl(Target.Value)
  dblPreviousValue = CDbl(varPreviousValue)
  On Error GoTo 0 ' This turns off "On Error" statements in VBA.


  If dblValue > dblPreviousValue Then
     isMyChange = True
  End If


  If isMyChange Then
    MsgBox ("You've increased the value of " & Target.Address)
  End If


  ' end of normal execution
  Exit Sub


ErrorHandler:
  ' Do nothing much.
  Exit Sub

End Sub

If you are wishing to change another workbook based on this, i'd think about checking to see if the workbook is already open first... or even better design a solution that can batch up all your changes and do them at once.如果您希望基于此更改另一个工作簿,我会考虑先检查该工作簿是否已经打开......或者甚至更好地设计一个可以批量处理所有更改并立即执行的解决方案。 Continuously changing another spreadsheet based on you listening to this one could be painful.根据您收听的内容不断更改另一个电子表格可能会很痛苦。

After playing with this for a while, I found the Michael's pseudo-code was the closest, but here's how I did it:玩了一段时间后,我发现迈克尔的伪代码是最接近的,但我是这样做的:

Dim xl As Excel.Application
Set xl = CreateObject("Excel.Application")
xl.Workbooks.Open "\\owghome1\bennejm$\testing.xls"
xl.Sheets("Sheet1").Select

Then, manipulate the sheet... maybe like this:然后,操作工作表......也许是这样的:

xl.Cells(x, y).Value = "Some text"

When you're done, use these lines to finish up:完成后,使用这些行完成:

xl.Workbooks.Close
xl.Quit

If changes were made, the user will be prompted to save the file before it's closed.如果进行了更改,将提示用户在关闭文件之前保存文件。 There might be a way to save automatically, but this way is actually better so I'm leaving it like it is.可能有一种自动保存的方法,但这种方法实际上更好,所以我保持原样。

Thanks for all the help!感谢所有的帮助!

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

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