等待一个函数完成然后继续

时间:2021-03-07 11:56:16

标签: excel vba

我有一个名为 export class VerticalMenuComponent implements OnInit { public settings: Settings; public products: Product[] = null; public productID = null public modules = {}; private token = this.cookieService.get('token') private id = this.cookieService.get('id') public isModuleOpen = {}; constructor(public appSettings:AppSettings, public menuService:MenuService, public router:Router, public productsService:ProductsService, private cookieService: CookieService, private modulesService:ModulesService, public dialog: MatDialog) { this.settings = this.appSettings.settings; } ngOnInit() { // this.parentMenu = this.menuItems.filter(item => item.parentId == this.menuParentId); if(this.token) { this.productsService.getAllProdObserv.subscribe(res => this.products = res); } } redirectTo(uri, moduleId, moduleName, product) { this.router.navigateByUrl('/', {skipLocationChange: true}).then(() => this.router.navigate([uri], {state: {data: {moduleId, moduleName,product}}})); } showModules(productId) { this.modulesService.getModules(productId) .subscribe(m => { this.modules[productId] = m if(this.modules[productId].length > 0) { if(this.isModuleOpen[productId]) { this.isModuleOpen[productId] = false }else { this.isModuleOpen[productId] = true } } }) } public openProductDialog(product){ let dialogRef = this.dialog.open(ProductDialogComponent, { data: product }); dialogRef.afterClosed().subscribe(result => { if(result) { this.refreshProducts() } }) } public refreshProducts(){ this.productsService.all(this.id, this.token); } } 的宏,其中有一个名为 Setup 的函数。当一个按钮被按下时宏开始执行,然后他调用Game,现在,宏继续运行而不等待Game完成,然后我在Game之后做的计算无效,因为游戏尚未完成。

  • 我希望我的宏等待 Game 完成,然后根据游戏计算得分。
Game

从上面的代码可以看出,Sub Setup() Game CalculateHighestScore End Sub 开始时没有等待游戏结束,然后统计数据就错了

1 个答案:

答案 0 :(得分:-1)

您需要确保CalculateHighestScore 需要的变量/数据是全局的,以便在Game 函数执行后数据仍然有效并且 可以访问CalculateHighestScore 函数

如果函数是从excel范围中提取数据,请尝试添加

    Sub Setup()
        Game
        Application.calculate
        CalculateHighestScore
    End Sub

并检查它是否有效,这将在处理CalculateHighestScore之前重新计算所有excel公式