Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.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
Vba 作为变量的工作表?_Vba_Excel - Fatal编程技术网

Vba 作为变量的工作表?

Vba 作为变量的工作表?,vba,excel,Vba,Excel,我创建了一个美化的复制粘贴宏,它将导入的信息从“模板”工作表复制到特定位置的工作表中 (因此我有名为location1 location2 location3 location4和template的工作表) 目前,我的代码是这样的:(但要长得多,重复得多) 我希望“location1”是一个工作表变量 我需要一种方法来改变,而不必改变每一个不同的模板的代码 据我所知,有两种解决办法 模板中有一个单元格,其中保存信息应该粘贴到的工作表名称的值。如果我可以将其设置为一个工作表变量,而不是“Locat

我创建了一个美化的复制粘贴宏,它将导入的信息从“模板”工作表复制到特定位置的工作表中
(因此我有名为location1 location2 location3 location4和template的工作表)

目前,我的代码是这样的:(但要长得多,重复得多)

我希望“location1”是一个工作表变量

我需要一种方法来改变,而不必改变每一个不同的模板的代码
据我所知,有两种解决办法

  • 模板中有一个单元格,其中保存信息应该粘贴到的工作表名称的值。如果我可以将其设置为一个工作表变量,而不是“Location1”
  • 让用户在运行宏时从列表中选择位置工作表

  • 我不知道如何做这两个,我不知道哪一个更好

    首先,删除
    select
    通常是一件好事:

    Sheets("Template").Range("P24:Q30").Copy Sheets("location1").Range("P24")
    
    如果选项卡名称位于图纸模板的单元格“B1”中,则可以使用:

    dim sTabName as String
    
    sTabName=Sheets("Template").[B1]
    Sheets("Template").Range("P24:Q30").Copy Sheets(sTabName).Range("P24")
    
    您还可以为工作表创建一个实变量,例如

    dim shtDest as Worksheet
    
    set shtDest =sheets(Sheets("Template").[B1])
    Sheets("Template").Range("P24:Q30").Copy shtDest.Range("P24")
    

    您可以通过以下方式从VBA工作表中的单元格中为变量赋值:

    Sub example()
        'Let vba know that you have a variable and it's type is string
        Dim myVariable as string
    
        'Assign the value from cell B5 in Worksheet "Template" to the variable
        myVariable = Sheets("Template").Range("B5").value
    End sub()
    
    您可以在一系列单元格中循环并获取它们的值

    Sum example()
        Dim myVariable as string
    
        'Let VBA know that we will store a range in variable "MyCell"
        Dim myCell as Range
    
        'loop through every cell in a range
        For each myCell in Sheets("Template").Range("B5:B100")
    
            'copy data from the template to the worksheet stored in the cell we are on
            Sheets(myCell.Value).Range("P24:Q30") = Sheets("Template").Range("P24:Q30")
    
        Next myCell
    End Example()
    

    纳奇,谢谢你的帮助。选择的东西肯定更好。我尝试了你的两种解决方案,但在这两种解决方案上都不断出现错误。对于工作表,我得到运行时错误13“类型不匹配”与Set shtDest行。对于字符串,我得到运行时错误9-下标超出范围。很抱歉,我在这方面很新…如果在模板工作表中,单元格B1不包含有效的工作表名称,则会出现此错误。谢谢您的帮助。我在myVariable行中得到运行时错误9。思想?另外,在单元格中循环的目的是什么?对于@jnevil的代码,如果在模板工作表中,单元格B5不包含有效的工作表名称,则会出现此错误
    Sum example()
        Dim myVariable as string
    
        'Let VBA know that we will store a range in variable "MyCell"
        Dim myCell as Range
    
        'loop through every cell in a range
        For each myCell in Sheets("Template").Range("B5:B100")
    
            'copy data from the template to the worksheet stored in the cell we are on
            Sheets(myCell.Value).Range("P24:Q30") = Sheets("Template").Range("P24:Q30")
    
        Next myCell
    End Example()