Vba 如何使我的UserForm代码在每次我在Excel中运行所述UserForm时更改代码的输出单元格

Vba 如何使我的UserForm代码在每次我在Excel中运行所述UserForm时更改代码的输出单元格,vba,excel,Vba,Excel,我研究过网络,但找不到对我有帮助的解决方案 我有一张Excel表格,创建了一个UserForm,它有一个墨水图片框,设置为允许用户打开用户表单并绘制签名。有一个CommandButton被分配用于获取签名并将其从剪贴板保存到电脑上。我有另一个CommandButton被分配用于从电脑路径检索所保存的图片,并将其带回工作簿中的特定工作表,并将其插入特定单元格 我的需要如下: 我需要的代码,我正在使用更新什么细胞,它将插入图像在每次我点击按钮。(换句话说,我需要代码从C3变为C50,每次按下按钮,代

我研究过网络,但找不到对我有帮助的解决方案

我有一张Excel表格,创建了一个UserForm,它有一个墨水图片框,设置为允许用户打开用户表单并绘制签名。有一个CommandButton被分配用于获取签名并将其从剪贴板保存到电脑上。我有另一个CommandButton被分配用于从电脑路径检索所保存的图片,并将其带回工作簿中的特定工作表,并将其插入特定单元格

我的需要如下: 我需要的代码,我正在使用更新什么细胞,它将插入图像在每次我点击按钮。(换句话说,我需要代码从C3变为C50,每次按下按钮,代码都会增加一位数,然后返回C3)

这能做到吗

此代码将图像带回我的工作表,但每次单击CommandButton时,图像都会插入C3中(因此所有图像都位于彼此的顶部,而不是下方):

Private子命令按钮4\u单击()
将ws设置为工作表
Dim ImgPath作为字符串
调暗W为双精度,H为双精度
我和你一样长,我和你一样长
设置ws=ThisWorkbook.Sheets(“移动POS记录表”)
“~~>这是我当前的pic文件路径
ImgPath=“C:\Users\raphaelo\Downloads\test.gif”
与ws

我一定是误解了你的问题。您需要在列“C”中找到最后一行,而不是“最后使用的行”,该行中有一个形状。此更改的代码将在C列中找到最后一行的形状,然后在该数字上添加两行,然后将图像放置在最低一行的下面两行。您可以将这些行更改为所需的行数。如果运行此操作时工作表上没有形状,则可能会出现错误。如果是这样,那么您必须为此创建一个错误捕获

Private Sub CommandButton4_Click()

Dim ws As Worksheet
Dim ImgPath As String
Dim W As Double, H As Double
Dim L As Long, T As Long
Dim myArr() As Variant, myArrCounter As Long
Dim newRowNumb As Long

Set ws = ThisWorkbook.Sheets("Mobile POS Log Sheet")

'////////////////////////////////////////   This section will find the row of the bottom most shape in Column C
ReDim myArr(1 To 1)
myArrCounter = 0

For Each wshape In ws.Shapes
    myArrCounter = myArrCounter + 1
    If myArrCounter = 1 And wshape.TopLeftCell.Column = 3 Then
        myArr(myArrCounter) = wshape.TopLeftCell.row
    Else:
        If wshape.TopLeftCell.Column = 3 And wshape.TopLeftCell.row > myArr(UBound(myArr)) Then
            ReDim Preserve myArr(1 To myArrCounter)
            myArr(myArrCounter) = wshape.TopLeftCell.row
        End If
    End If
Next wshape
newRowNumb = myArr(UBound(myArr)) + 2 ' this adds two rows to place the new picure.  Change the "2" to how many rows you need



'~~> This is my current pic file path
ImgPath = "C:\Users\raphaelo\Downloads\test.gif"


    With ws
        W = 30                  '<~~ Width
        H = 11                  '<~~ Height
        L = .Range("c" & newRowNumb).Left   '<~~ This is what should be changing each time I run the command
        T = .Range("c" & newRowNumb).Top    '<~~ This is what should be changing each time I run the command
         '<~~ Both the L and T Range entries should change to the next cell (C3 to C4 to C5 and so on) One digit up every time I run the Command Code
         '<~~ Unless it's the Placement entry below?
        With .Pictures.Insert(ImgPath)
            With .ShapeRange
                .LockAspectRatio = msoTrue
                .Width = W
                .Height = H
            End With
            .Left = L
            .Top = T
            .Placement = 1 '<~~ Not sure if this is the one that should change one digit up each time I run the Command instead?
        End With
    End With

End Sub
Private子命令按钮4\u单击()
将ws设置为工作表
Dim ImgPath作为字符串
调暗W为双精度,H为双精度
我和你一样长,我和你一样长
Dim myArr()作为变量,myArrCounter作为Long
朦胧的新生,麻木的一样长
设置ws=ThisWorkbook.Sheets(“移动POS记录表”)
“//本节将在C列中找到最底部形状的行。”
雷迪姆迈尔(1比1)
myArrCounter=0
对于ws.Shapes中的每个wshape
myArrCounter=myArrCounter+1
如果myArrCounter=1且wshape.TopLeftCell.Column=3,则
myArr(myArrCounter)=wshape.TopLeftCell.row
其他:
如果wshape.TopLeftCell.Column=3且wshape.TopLeftCell.row>myArr(UBound(myArr)),则
ReDim保留myArr(1到myArr计数器)
myArr(myArrCounter)=wshape.TopLeftCell.row
如果结束
如果结束
下一个形状
newrownub=myArr(UBound(myArr))+2'这将添加两行以放置新图片。将“2”更改为需要的行数
“~~>这是我当前的pic文件路径
ImgPath=“C:\Users\raphaelo\Downloads\test.gif”
与ws

W=30'我会尝试查找工作表上最后使用的行,在该数字上添加1或50(无论您需要多少行),然后粘贴到那里。搜索查找列中最后使用的行。将lastUsedRow放在一个变量中,并将代码更改为L=.Range(“c”&lastUsedRow+50)。Left和T=.Range(“c”+50)。top这听起来像是可行的!!很抱歉,我是VBA的新手。如果以我应该准确添加的方式将此内容提供给我,会不会有太多要求?你能用我现在的代码,把你建议的代码加进去吗?非常感谢。(我将根据您对如何添加以及在何处添加此内容的评论尝试找出答案。这是我根据您的建议添加到代码中的内容:在这行之后:Dim L As Long,T As Long
Dim lastUsedRow As Variant
,并将范围更改为:
With ws
W=30
,我收到以下错误:运行时错误“13”:类型不匹配,并突出显示以下代码行:
T=.Range(“c”+50)。Top
Wow!这是最接近工作状态的代码!有时它可以工作,当我更改为
newrownum=myArr(UBound(myArr))时+1
粘贴在同一个单元格中。然后,如果我将其更改回
+2
,它会工作,如果我将其更改回
+1
大约三到四次尝试,它会返回粘贴到同一单元格中。(因此,它似乎只适用于您最初给我的
+2
代码)。有时,即使有一个对象已经存在,它也会给我一个运行时错误1004:对象定义或应用程序定义的错误。在这个代码上:
如果myArrCounter=1和wshape.TopLeftCell.Column=3,那么
我做错了什么吗?理想情况下,就像我在第一篇文章中提到的那样,第一个InkPicture应该插入第3列3,从那里往下看,但这段代码从第1行开始,有解决办法吗?我真的很感激!不知道这是否与此有关,但在同一张表中,第1行和第2行是锁定/固定的标题,第1、2、4、5、6、7列有其他不同的公式,用于创建其他计算(这些都没有交互、利用或引用列C做任何事情)但我认为我应该共享以防万一。在调试突出显示的代码时,它识别出
myArrCounter=1
,并返回一个正计数,但随后它表示
wshape.TopLeftCell.column=3
返回此值:,
Private Sub CommandButton4_Click()

Dim ws As Worksheet
Dim ImgPath As String
Dim W As Double, H As Double
Dim L As Long, T As Long
Dim myArr() As Variant, myArrCounter As Long
Dim newRowNumb As Long

Set ws = ThisWorkbook.Sheets("Mobile POS Log Sheet")

'////////////////////////////////////////   This section will find the row of the bottom most shape in Column C
ReDim myArr(1 To 1)
myArrCounter = 0

For Each wshape In ws.Shapes
    myArrCounter = myArrCounter + 1
    If myArrCounter = 1 And wshape.TopLeftCell.Column = 3 Then
        myArr(myArrCounter) = wshape.TopLeftCell.row
    Else:
        If wshape.TopLeftCell.Column = 3 And wshape.TopLeftCell.row > myArr(UBound(myArr)) Then
            ReDim Preserve myArr(1 To myArrCounter)
            myArr(myArrCounter) = wshape.TopLeftCell.row
        End If
    End If
Next wshape
newRowNumb = myArr(UBound(myArr)) + 2 ' this adds two rows to place the new picure.  Change the "2" to how many rows you need



'~~> This is my current pic file path
ImgPath = "C:\Users\raphaelo\Downloads\test.gif"


    With ws
        W = 30                  '<~~ Width
        H = 11                  '<~~ Height
        L = .Range("c" & newRowNumb).Left   '<~~ This is what should be changing each time I run the command
        T = .Range("c" & newRowNumb).Top    '<~~ This is what should be changing each time I run the command
         '<~~ Both the L and T Range entries should change to the next cell (C3 to C4 to C5 and so on) One digit up every time I run the Command Code
         '<~~ Unless it's the Placement entry below?
        With .Pictures.Insert(ImgPath)
            With .ShapeRange
                .LockAspectRatio = msoTrue
                .Width = W
                .Height = H
            End With
            .Left = L
            .Top = T
            .Placement = 1 '<~~ Not sure if this is the one that should change one digit up each time I run the Command instead?
        End With
    End With

End Sub