是否可以使用输入框 select 来自 excel sheet 的一行,然后使用来自该行的 select 特定单元格值来填写 VBA 代码的其余部分?

Is it possible to use input box to select a row from excel sheet and then select specific cell values from that row to fill in rest of VBA code?

我正在学习使用VBA从excel制作PPT。我已经编写了整个功能,但想让用户更容易使用它。所以,我想看看是否可以创建一个输入框,用户在其中添加行号,然后选择该行中的特定单元格以在 PPT 中添加值。

例如,假设用户在输入框中输入“208”,单元格 B208、I208、J208 中的值将在函数中使用,如下所示。

 ppSlide.Select
    Range("B208").Copy
    ppSlide.Shapes.Paste
    With pppres.Slides(1).Shapes(1)
        .Top = 30
    End With

    ppSlide.Select
    Range("I208").Copy
    ppSlide.Shapes.Paste
    With pppres.Slides(1).Shapes(2)
        .Left = 140
        .Top = 73
    End With

    ppSlide.Select
    Range("J208").Copy
    ppSlide.Shapes.Paste
    With pppres.Slides(1).Shapes(3)
        .Left = 480
        .Top = 73
    End With

因此,用户无需在 15-16 个不同位置更改值,只需将其添加到输入框中一次即可。并且不必弄乱代码。

我认为这就是您的想法 - 或多或少。请看一下。

Sub TestPPslide()

    Dim ppPres As Object                    ' which type of object?
    Dim ppSlide As Object                   ' which data type?
    Dim Inp As Variant
    Dim R As Long

    R = 2
    Do
        Inp = InputBox("Enter a valid row number.", "Slide selector", R)
        ' test if Inp is valid
        If IsNumeric(Inp) Then
            R = Int(Val(Inp))
            If R > 1 And R < 100 Then Exit Do
        Else
            If Len(Inp) = 0 Then Exit Sub       ' user presserd cancel
        End If
    Loop

    SetSlide R, ppSlide, ppPres.Slides(1)
End Sub

Private Function SetSlide(ByVal R As Long, _
                          ppSlide As Object, _
                          PresSlide As Object) As Boolean

    Dim Clm As Variant
    Dim Left As Variant, Top As Variant
    Dim C As Long

    Clm = Array("B", "I", "J")
    Left = Array(0, 140, 480)
    Top = Array(30, 73, 73)

    For C = 0 To UBound(Clm)
        Cells(R, Clm(C)).Copy
        ppSlide.Shapes.Paste
        With PresSlide.Shapes(C + 1)
            .Left = Left(C)
            .Top = Top(C)
        End With
    Next C
End Function

我不知道你的对象是什么,是来自Excel还是来自PP。因此我可能无法正确处理这些对象。或者您自己的代码在他们方面可能存在一些不一致。以我的代码为指导如何设置它。

  1. 创建一个带有子过程和函数的 Main。不要只是将一个想法一个接一个地连接成一个过程。
  2. Main 必须包含任务的输入。然后它必须将工作交给下一个过程,在这种情况下,它反馈子 SetSlide 修改的两个对象以供 Main 或下一个子例程进一步关注。
  3. 避免 SelectActivate。您无需执行此操作即可访问每个对象的每个部分。