简体   繁体   English

根据单元格值在VBA中导航多个工作表

[英]Navigate in multiple sheet in VBA depending on cell value

I am a fairly new VBA user and have doing few codes learning as I go and reading forums. 我是VBA的新用户,在阅读和阅读论坛时很少学习代码。

Now I want to code something and I couldn't find a way to do it/a hint in the forums so I am coming to ask for your kind help. 现在,我想编写一些代码,但找不到解决方法/在论坛中没有任何提示,因此,我来​​寻求您的帮助。

I have a lot of spreadsheet with names as title, representing expenses of those people. 我有很多以名称为标题的电子表格,代表这些人的费用。 My goal is to create a file in which I regroup the spreadsheets, then would like to extract the expense references in the "Home" spreadsheet. 我的目标是创建一个文件,在其中对电子表格进行重新分组,然后希望在“主页”电子表格中提取费用参考。 The relevant expanses will be marked "Y" in the same row. 相关的展开将在同一行中标记为“ Y”。

In the "Home" spreadsheet I have names of people on the first page eg "Paul", "Marie","Pierre" etc.... in the same column. 在“家庭”电子表格中,我在第一页的第一栏中输入了人名,例如“ Paul”,“ Marie”,“ Pierre”等。

I would like to create a code that could look at the names in the spreadsheet "Home" and copy data from the "Named" Spreadsheet back to the "Home" one. 我想创建一个代码,该代码可以查看电子表格“主页”中的名称,并将数据从“命名”电子表格复制回“主页”中。

Format would be the same for the "Named spreadsheet" so I am always looking at the same column. “命名电子表格”的格式将相同,因此我一直在查看同一列。

I have tried the following so far 到目前为止,我已经尝试了以下方法

Sub Expenses()

    Set wk=ThisWorkbok.sheets("Home")

    Dim Name as String
    Dim Expense as Integer
    line_newexpense = 9
    rowline = 10
    start_l =34

    Do While Name <> "#"
        Line = wk.Cells(start_l,3)
        Name = ThisWorkbook.Sheets("Home").Cells(line_newexpense,39).Value
        Expense = ThisWorkbook.Sheets(Name).Cells(rowline,3).Value

        For j=50 to 8
            If ThisWorkbook.Sheets(Name).Cells(25,j).Value="Y" Then
                Line = ThisWorkbook.Sheets(Name).Cells(3,j).Value
            End If
            rowline = rowline+1
        Next j

        line_newexpense = line_newexpense + 1
    Loop

The problem is when I try to run it I encounter an "Error 9 Subscript out of range", so not sure how I could counter the problem, 问题是,当我尝试运行它时,遇到“错误9下标超出范围”,因此不确定如何解决该问题,

Many thanks in advance for your hints! 预先非常感谢您的提示!

Kind Regards, 亲切的问候,

You do not need VBA for this. 您不需要VBA。 You can use INDIRECT function to achieve what you want. 您可以使用INDIRECT函数来实现所需的功能。

For example 例如

=INDIRECT(B1&"!$A$1")

Where B1 has the name of the worksheet, Say "Paul" and A1 is the cell which has the data that you need. 其中B1是工作表的名称,说“ Paul”,而A1是具有所需数据的单元格。

在此处输入图片说明

You can also factor the Y part in your above formula using IF function. 您也可以使用IF函数将上述公式中的Y部分IF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM