VBa显示错误的值

时间:2018-04-09 14:59:59

标签: excel vba excel-vba

我在reddit上问了这个问题,但我认为在这里尝试也不会有什么问题:这是the link。我将复制并粘贴我在那里写的内容。

嗨,

所以对于工作我有大约90个Excel,我需要每周从中提取数据。我很新,但我想我会为它写一个VBA,因为我不想每周花3天时间就这样做。
但显然我搞砸了。

这就是我的代码。

Sub Makro1()
Dim oWB1210 As Workbook: Set oWB1210 = Application.Workbooks.Open(<"file path">)
Dim oWS1210 As Worksheet: Set oWS1210 = oWB1210.Worksheets("Table1")

Dim Variable As Integer
Dim Variable1 As Integer
Dim Variable2 As Integer
Dim Variable3 As Integer
Dim Variable4 As Integer
Dim Variable5 As Integer
Dim Variable6 As Integer
Dim Variable7 As Integer
Dim Variable8 As Integer


Dim Cell As Range


'Optimize Macro Speed
Application.ScreenUpdating = False  
Application.EnableEvents = False  
Application.Calculation = xlCalculationManual  

For Each Cell In ThisWorkbook.ActiveSheet.Range("A3:A22")
Variable1 = Application.WorksheetFunction.CountIfs(oWS9020.Range("A:A"), Cell.Value, oWS9020.Range("D:D"), "x", oWS9020.Range("F:F"), "y") _
+ Application.WorksheetFunction.CountIfs(oWS8052.Range("A:A"), Cell.Value, oWS8052.Range("D:D"), "x", oWS8052.Range("F:F"), "y") _
+ <several more in a similar fashion, just another name>
Next Cell

oWB9020.Close False
oWB8052.Close False
<several more with different names>

Dim oWBNorth As Workbook: Set oWBNorth = Application.Workbooks.Open(<"file path">)
Dim oWSNorth As Worksheet: Set oWSNorth = oWBNorth.Worksheets("Table1")

<several more in a similar fashion, just another name and another path>

 For Each Cell In ThisWorkbook.ActiveSheet.Range("A3:A22")
Variable2 = Application.WorksheetFunction.CountIfs(oWSNorth.Range("A:A"), Cell.Value, oWSNorth.Range("D:D"), "x", oWSNorth.Range("F:F"), "y") _
+ Application.WorksheetFunction.CountIfs(oWSSouth.Range("A:A"), Cell.Value, oWSSouth.Range("D:D"), "x", oWSSouth.Range("F:F"), "y") _
+ <several more in a similar fashion, just another name>
Next Cell

你知道我这样做,直到我在Variable8

然后我做

For Each Cell In ThisWorkbook.ActiveSheet.Range("A3:A22")
Variable = Variable1 + Variable2 + Variable3 + Variable4 + Variable5 + Variable6 + Variable7 + Variable8
Cell.Offset(0, 15).Value = Variable
Next Cell

ResetSettings:  
'Reset Macro Optimization Settings  
Application.EnableEvents = True  
Application.Calculation = xlCalculationAutomatic  
Application.ScreenUpdating = True  

 End Sub

然而,这表明我在每个单元格中都是2而不是13或者其他东西。

任何帮助都会受到赞赏,即使您只是在我的代码中看到一些不必要的复杂内容并且可以更轻松地完成。

不知道我在做什么:D

1 个答案:

答案 0 :(得分:0)

替换这部分代码:

For Each Cell In ThisWorkbook.ActiveSheet.Range("A3:A22")
    Debug.Print "Variable1 is ---> " & variable1
    Debug.Print "Variable2 is ---> " & variable2
    Debug.Print "Variable3 is ---> " & variable3
    Debug.Print "Variable4 is ---> " & variable4
    Debug.Print "Variable5 is ---> " & variable5
    Debug.Print "Variable6 is ---> " & variable6
    Debug.Print "Variable7 is ---> " & variable7
    Debug.Print "Variable8 is ---> " & variable8

    Variable = variable1 + variable2 + variable3 + variable4 + variable5 + variable6 + variable7 + variable8
    Cell.Offset(0, 15).Value = Variable

    Stop ' and read the values in the immediate window (CTRL+G)

Next Cell

使用此代码:

Stop

一旦它停在import { Injectable } from '@angular/core'; import { PicItem } from './Models/Pictures'; @Injectable() export class PicturesService { private readonly time = 10000; public image1:string = ""; public image2:string = ""; private _picList: string[]; private index: number = 0; constructor() { this._picList = [ "", "" ] setInterval(() => { this.image1 = this.getImage(); this.image2 = this.getImage(); }, this.time); } getImage(){ return this._picList[this.index++]; } } 行,请按 Ctrl + G 并查看立即窗口上写的内容。它会显示您需要的所有值。