使用命令按钮自动化不同的工作表

时间:2017-11-14 11:04:39

标签: excel vba cell

我正在尝试自动化我的excel工作簿包含5张。有一个摘要页面,其中包括一个命令按钮,它基本上允许上传文件,然后填写特定的工作表。 我得到的错误是填写指定工作表中的单元格,而不是填写摘要页面中的单元格,即带有按钮的页面。这是我到目前为止编写的代码。       以下是我尝试在第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

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我明白了,先选择纸张然后填充单元格。这不是最安全的方式。

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

您的部分

    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

我希望这会有所帮助,我认为这很简单。

相关问题