简体   繁体   English

Excel VBA宏可基于其他工作表中的值将粘贴值复制到其他工作表

[英]Excel VBA Macro to copy paste values to other sheets based on value in the other sheet

I'm currently working on an expense monitoring excel. 我目前正在研究费用监控Excel。 The spreadsheet has many sheets with lots of data. 电子表格有很多工作表,其中包含大量数据。 But for simplicity, let's say I have only three worksheets: 但是为了简单起见,假设我只有三个工作表:

Sheet 1 - "INPUT" 表格1-“输入”

Sheet 2 - "ACCOUNT XX" (XX for short) 表格2-“帐户XX”(简称XX)

Sheet 3 - "ACCOUNT YY" (YY for short) 表格3-“ YYYY帐户”(简称YY)

The idea is that expenses data will be inputted in a certain table in the "INPUT" sheet. 想法是费用数据将输入到“输入”表中的某个表中。 The table has an "ACCOUNT" column to select which account the expense was made from. 该表具有“ ACCOUNT”列,用于选择从中进行费用支出的帐户。 The expense data would then, with a click on a macro button, be transferred to the respective other sheets (XX or YY) depending on the data inputted in the "ACCOUNT" column. 然后,通过单击宏按钮,费用数据将被转移到相应的其他工作表(XX或YY),具体取决于在“帐户”列中输入的数据。

For example: 例如:

Expense 1 ($50) - made from Account YY 费用1($ 50)-来自帐户YY

Expense 2 ($100) - made from Account XX 费用2($ 100)-来自帐户XX

Expense 3 ($150) - made from Account YY 费用3($ 150)-来自帐户YY

When the macro button is clicked, Expense 1 and Expense 3 would be copy pasted to Sheet "YY" while Expense 2 to Sheet "XX". 单击宏按钮时,费用1和费用3将被复制粘贴到工作表“ YY”,而费用2被粘贴到工作表“ XX”。

I tried making a macro button which supposedly would transfer the data inputted in the "INPUT" sheet to the other sheets, depending on the data inputted in the "ACCOUNT" column. 我尝试制作一个宏按钮,该按钮可能会将在“输入”表中输入的数据转移到其他表中,具体取决于在“帐户”列中输入的数据。 I tried to compare the "account" inputs for each expenses, with the header cell (say F1) of the other sheets, which contains the text/name of the particular account. 我试图将每个费用的“帐户”输入与其他工作表的页眉单元格(例如F1)进行比较,其中包含特定帐户的文本/名称。

For Example: 例如:

Expense 1 ($50) - made from Account YY ≠ Sheet XX Cell F1 Value: "Account XX" 费用1($ 50)-由帐户YY ≠表格XX的单元格F1值: “帐户XX”

Expense 1 ($50) - made from Account YY = Sheet YY Cell F1 Value: "Account YY" 费用1($ 50)-由帐户YY =表格YY单元格F1值: “帐户YY”

Therefore, copy to Sheet YY. 因此,复制到工作表YY。

But I can't make it work. 但是我不能使它工作。 Nothing is happening when I click the button. 当我单击按钮时,没有任何反应。 I don't know what exactly is wrong. 我不知道到底是哪里错了。 I tried researching on how to make excel VBA pick which sheet it will paste the value to, but still can't get it to work as it's supposed to. 我尝试研究如何使excel VBA选择将值粘贴到的工作表,但仍无法按预期工作。 Any ideas or suggestions? 有什么想法或建议吗? Thank you. 谢谢。

Please see code below for your reference. 请参阅下面的代码以供参考。

Sub EXPENSES()
Dim nextrow As Long
Dim rowcount As Long
Dim x As Long
For Each Worksheet In Worksheets
    rowcount = Worksheets("INPUT").Range("B19").Value
    For x = 1 To rowcount
        If Worksheet.Range("F1").Value = Worksheets("INPUT").Range("E" & 9 + x - 1).Value Then
        nextrow = Worksheet.Cells(Rows.Count, "G").End(xlUp).Row + 1
        If nextrow < 18 Then nextrow = 18
        Worksheet.Range("B" & nextrow + x - 1).Value = Worksheets("INPUT").Range("B" & 9 + x - 1).Value
        End If
    Next x
Next Worksheet
End Sub

Please see screenshots of the "INPUT" and "ACCOUNT XX" sheets. 请查看“输入”和“帐户XX”表的屏幕截图。 Sample data are also included for your reference. 还包括样本数据供您参考。

Input Sheet 输入表

Account XX Sheet 帐户XX工作表

Sincerely, 真诚的

Fritze 弗里兹

I have made some changes to your code. 我对您的代码进行了一些更改。 This code should work: 此代码应工作:

Sub EXPENSES()
Dim nextrow As Long
Dim rowcount As Long
Dim x As Long
Dim Worksheet As Object
For Each Worksheet In Worksheets
    rowcount = Worksheets("INPUT").Range("B19").Value
    For x = 1 To rowcount
        If Worksheet.Range("F1").Value = Worksheets("INPUT").Range("E" & 9 + x - 1).Value Then
            nextrow = Worksheet.Cells(Rows.Count, "B").End(xlUp).Row + 1
            If nextrow < 18 Then nextrow = 18
            Worksheet.Range("B" & nextrow).Value = Worksheets("INPUT").Range("B" & 9 + x - 1).Value
        End If
    Next x
Next Worksheet
End Sub

or alternative, shoret, faster solution: 或替代方案,更快的解决方案:

Sub EXPENSES_Alt()
Dim nextrow As Long
Dim rowcount As Long
Dim x As Long
Dim Worksheet_Name As String
rowcount = Worksheets("INPUT").Range("B19").Value
For x = 1 To rowcount
    Worksheet_Name = Right(Worksheets("INPUT").Range("E" & 8 + x).Value, 2)
    nextrow = Worksheets(Worksheet_Name).Cells(Rows.Count, "B").End(xlUp).Row + 1
    If nextrow < 18 Then nextrow = 18
    Worksheets(Worksheet_Name).Range("B" & nextrow).Value = Worksheets("INPUT").Range("B" & 8 + x).Value
Next x
End Sub

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

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