Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB Excel无法复制和粘贴拖动单元格位置的宏_Excel_Vba - Fatal编程技术网

VB Excel无法复制和粘贴拖动单元格位置的宏

VB Excel无法复制和粘贴拖动单元格位置的宏,excel,vba,Excel,Vba,我有一张这样的数据表 第1页 顾客:约瑟夫 身份证号码:12345 我创建了一个包含客户和ID的表单,更容易手动创建。表格里有我的信息 我有一个插入按钮,可以将信息传递到页面内的特定单元格 Private Sub CommandButton1_Click() i = Cells(Rows.Count, 1).End(xlUp).Row + 23 'Get last empty cell in column A Range("D" & i).Value = TextBox1 'Range

我有一张这样的数据表

第1页

顾客:约瑟夫

身份证号码:12345

我创建了一个包含客户和ID的表单,更容易手动创建。表格里有我的信息

我有一个插入按钮,可以将信息传递到页面内的特定单元格

Private Sub CommandButton1_Click()

i = Cells(Rows.Count, 1).End(xlUp).Row + 23 'Get last empty cell in column A
Range("D" & i).Value = TextBox1
'Range("D1").Value = TextBox1
Range("D2").Value = TextBox4
Range("C4").Value = TextBox2
Range("C5").Value = TextBox3
    If OptionButton1.Value = True Then
       Range("A7").Value = OptionButton1.Caption
       ElseIf OptionButton2.Value = True Then
       Range("A7").Value = OptionButton2.Caption
       ElseIf OptionButton3.Value = True Then
       Range("A7").Value = OptionButton3.Caption
       Else
       Range("A7").Value = OptionButton4.Caption
     End If
Unload Me
End Sub   
我的问题是,我需要在工作表的第2页执行相同的操作,当我复制并粘贴宏以在第2页执行相同的操作时,我需要在第2页的相同位置重新设置值,但如果您查看我将Value=TextBox1设置为的代码,宏将在第1页的相同单元格中释放日期“D1,我需要一个公式,在复制宏并调用表单后,它将粘贴到活动工作表中,而不是像现在一样粘贴到第一页。 试着放置一个柜台,但这不起作用。任何帮助都将不胜感激

在这张图片上,您将看到第1页hoja1和第1页pagina 1以及装载表格datos del presupuesto

我也有图片2,第一页,但第2页第2页,在这一页我复制和粘贴相同的我在第1页宏和一切,我按下插入器按钮加载表单,当系统插入它在第1页,而不是在第2页


当我复制一个页面并粘贴宏时,它应该是一个公式,可以帮助我将所有单元格值拉到我要粘贴的页面上。如果要从特定工作表中获取范围,请在范围之前使用该工作表:

Sheet1.Range("D" & i).Value = TextBox1 'takes cell from first sheet.
Sheet2.Range..... 'takes cell from second sheet.


ActiveWorkbook.Worksheets("SheetName").Range.... 'you get sheets by their names
您还可以将工作表放入变量中,以便继续使用它,而不是重复整个工作表名称:

Dim TargetSheet as Worksheet
Set TargetSheet = ActiveWorkbook.Worksheets("SheetName")

TargetSheet.Range.....
TargetSheet.Range......
TargetSheet.Range......
因此,您的代码可以如下所示:

Private Sub CommandButton1_Click()

    'First thing: select your specific sheet:
    Dim MySheet as Worksheet
    Set MySheet = ActiveWorkbook.Worksheets("TheNameOfYourSheet")

    'Now, before every range or cell, take them FROM that specific sheet
    i = MySheet.Cells(Rows.Count, 1).End(xlUp).Row + 23 'Get last empty cell in column A

    'Select your pivot cell (don't forget MySheet)
    Dim PivotCell as Range
    Set PivotCell = MySheet.Range("D" & i)

    'Put the value in the pivot
    PivotCell.Value = TextBox1 

    'Now you can start using the offsets (it's really better, as you commented)
    PivotCell.Offset(0,1).Value = TextBox4 'That would be D2 if Pivot is D1

    'I'm not sure if you can use negative offsets, 
    'if not, take the top left cell of your target area as pivot -
    'But there surely will be an error if the offset falls to the left or above A1
    PivotCell.Offset(3, -1).Value = TextBox2 'That is C4 -         
    PivotCell.Offset(4,-1).Value = TextBox3 'C5

    If OptionButton1.Value = True Then
        PivotCell.Offset(6,-3).Value = OptionButton1.Caption ' A7
    ElseIf OptionButton2.Value = True Then
        PivotCell.Offset(6,-3).Value = OptionButton2.Caption
    ElseIf OptionButton3.Value = True Then
        PivotCell.Offset(6,-3).Value = OptionButton3.Caption
    Else
        PivotCell.Offset(6,-3).Value = OptionButton4.Caption
    End If

    Unload Me
End Sub   
ActiveSheet.Range("NamedCell")
更好的方法是命名单元格。在Excel屏幕的左上角,有一个文本框,其中显示选定的单元格坐标,如A1、B2等。如果在该框中键入名称,则可以通过如下代码使用该命名单元格:

Private Sub CommandButton1_Click()

    'First thing: select your specific sheet:
    Dim MySheet as Worksheet
    Set MySheet = ActiveWorkbook.Worksheets("TheNameOfYourSheet")

    'Now, before every range or cell, take them FROM that specific sheet
    i = MySheet.Cells(Rows.Count, 1).End(xlUp).Row + 23 'Get last empty cell in column A

    'Select your pivot cell (don't forget MySheet)
    Dim PivotCell as Range
    Set PivotCell = MySheet.Range("D" & i)

    'Put the value in the pivot
    PivotCell.Value = TextBox1 

    'Now you can start using the offsets (it's really better, as you commented)
    PivotCell.Offset(0,1).Value = TextBox4 'That would be D2 if Pivot is D1

    'I'm not sure if you can use negative offsets, 
    'if not, take the top left cell of your target area as pivot -
    'But there surely will be an error if the offset falls to the left or above A1
    PivotCell.Offset(3, -1).Value = TextBox2 'That is C4 -         
    PivotCell.Offset(4,-1).Value = TextBox3 'C5

    If OptionButton1.Value = True Then
        PivotCell.Offset(6,-3).Value = OptionButton1.Caption ' A7
    ElseIf OptionButton2.Value = True Then
        PivotCell.Offset(6,-3).Value = OptionButton2.Caption
    ElseIf OptionButton3.Value = True Then
        PivotCell.Offset(6,-3).Value = OptionButton3.Caption
    Else
        PivotCell.Offset(6,-3).Value = OptionButton4.Caption
    End If

    Unload Me
End Sub   
ActiveSheet.Range("NamedCell")

你把一个页面叫做什么???它是一张表吗?你在屏幕底部点击的一个命名选项卡???如果是这样,在你在代码中找到的所有范围之前,添加Sheet1或Sheet2。依此类推,表示你想要添加值的表。很难解释,但让我给你展示我拥有的图片。在这张图片上,你将看到ee sheet1 hoja1和page1 pagina 1以及加载表单datos del presupuestoi我发现ActiveCell.Offset1,0可以解决这一问题,但我仍然需要一个数据透视单元格来映射另一个单元格。每个单元格都是一个范围,ActiveCell也是一个范围。因此,您可以从喜欢的任何工作表中选择数据透视单元格,并对其进行偏移。请参阅我的答案中使用“编辑”那个