使用命令button自动执行不同的工作表

我正在尝试自动化我的Excel工作簿由5张纸组成。 有一个总结页面,其中包括一个命令button,基本上允许上传一个文件,然后填写特定的表格。 我得到的错误是填充指定表格中的单元格,而不是填写摘要页面中的单元格,即带有button的页面。 这是迄今为止我写的代码。 下面是一个例子,我试图在表3中指定带有扩展SIG的文件

Set fd = Application.FileDialog(msoFileDialogOpen) With fd .AllowMultiSelect = True .Filters.Clear .Filters.Add "All files", "*.*" .Filters.Add "ID Files", "*.ID", 1 .FilterIndex = 1 .Title = "Select SSI Identity File" .InitialFileName = "" If .Show = -1 Then ReDim selectedPaths(.SelectedItems.Count) For I = 0 To .SelectedItems.Count - 1 selectedPaths(I) = .SelectedItems(I + 1) Close #1 If Right$(selectedPaths(I), 3) = "SIG" Then Sheets(3).Select Open selectedPaths(I) For Input As #1 x = 4 txt = Input(LOF(1), 1) With RegExp .Pattern = "\B/.*" .Global = True .IgnoreCase = False txt = .Replace(txt, "") End With With RegExp .Pattern = "S[0-9A-Z][0-9A-Z][0-9]+" .Global = True Set matches = .Execute(txt) For Each Match In matches Debug.Print Match.Value If x Mod 30 = 0 Then x = x + 4 End If Cells(x, 1) = Match x = x + 1 Cells(x, 1) = Match If x Mod 30 <> 0 Then x = x + 1 End If Next End With End If Next End If End With End Sub 

谢谢您的帮助 !

我明白了,你先select表格,然后填充单元格。 这不是最安全的方法。

尝试使用对单元格的完整引用。

你的部分

  For Each Match In matches Debug.Print Match.Value If x Mod 30 = 0 Then x = x + 4 End If Cells(x, 1) = Match x = x + 1 Cells(x, 1) = Match If x Mod 30 <> 0 Then x = x + 1 End If Next 

应该看起来像这样:

  For Each Match In matches Debug.Print Match.Value If x Mod 30 = 0 Then x = x + 4 End If ThisWorkbook.Sheets(3).Cells(x, 1) = Match x = x + 1 ThisWorkbook.Sheets(3).Cells(x, 1) = Match If x Mod 30 <> 0 Then x = x + 1 End If Next 

我希望这有助于,我认为这确实很简单。