比较两个工作簿表excel vba

时间:2016-04-13 12:45:13

标签: excel vba excel-vba macros

我正在研究一个excel新jfor 1周,我想比较打开的excel文件当前打开的文件,

我做了所有可能但是每当我尝试读取行时,它只读取打开的值,我无法访问读取当前工作簿,我的宏被编码

Sub test1()
Dim iComp
Dim sheet As String
Dim wbTarget As Worksheet
Dim wbThis As Worksheet
Dim bsmWS As Worksheet
Dim c As Integer
Dim x As Integer
Dim strValue As String
Static value As Integer

Dim myPath As String
Dim folderPath As String
k = 3


Filename = Application.GetOpenFilename("Excel files (*.xls*),*.xl*", Title:="Open data") ' Choosing the Trigger Discription
'Set wbTarget = ActiveWorkbook.ActiveSheet
Set theRange = Range("A2:A4")
c = theRange.Rows.Count
strValue = vbNullString
For x = 1 To c
strValue = strValue & theRange.Cells(x, 1).value
Next x


'Set tabWS = Sheets("Tabelle1")



folderPath = Application.ActiveWorkbook.Path
myPath = Application.ActiveWorkbook.FullName
Set bsmWS = Sheets("Tabelle1")

Set wbkA = Workbooks.Open(Filename:="myPath")
Set varSheetA = wbkA.Worksheets("Balance sheet").Range(strRangeToCheck)

它的1000行代码,我只是放了代码片段。

我在我编写的工作簿中有myworksheet。我想打开另一个工作表,获取值并将其与我当前的工作表进行比较。如果字符串匹配(ex range(A1:A2))则msgbox yes

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用ThisWorkbook.Sheets(“工作表名称”)。范围(“A2:A4”)或ThisWorkbook.ActiveSheet.Range(“A2:A4”)。这将确保引用代码所在的工作簿。

有关Application.ThisWorkbook的更多信息 https://msdn.microsoft.com/en-us/library/office/ff193227.aspx