简体   繁体   English

Excel宏,要基于另一个单元格值复制和粘贴单元格值?

[英]Excel macro, to copy and paste a cell value based on another cell value?

Hi I shall try to explain clearly what I need to be able to do, here goes: 嗨,我将尽力清楚地说明我需要做的事情,这是:

I have an Excel spread sheet 1 with postcodes in column A and a number in column B . 我有一个Excel电子表格1,在A列中有邮政编码,在B列中A一个数字。 I need to create a macro/formula so that it will see the number (ie 3) and copy and paste the postcode that number of times into sheet 2 column a underneath each other. 我需要创建一个宏/公式,以便它将看到数字(即3),并将该次数的邮政编码复制并粘贴到彼此下面的工作表2列a中。 I need to do this for the next row down etc until it comes to a blank. 我需要对下一行等执行此操作,直到出现空白为止。

Sheet 1 工作表1

A       B
DE43PP  3
DE43PQ  8

Sheet 2 工作表2

A       B
DE43PP
DE43PP
DE43PP
DE43PQ
...

Thanks 谢谢

Try this: 尝试这个:

Sub copyPostcodes()
    Dim c As Range
    Dim x As Long
    Dim y As Long

    y = 0

    For Each c In Sheets("Sheet1").Range("A:A").Cells
        If Len(c) > 0 Then
            For x = 1 To c.Offset(0, 1)
                Sheets("Sheet2").Range("A1").Offset(y, 0) = c
                y = y + 1
            Next x
        End If
    Next c
End Sub

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

相关问题 Excel宏,根据另一个单元格值复制和粘贴多个单元格? - Excel macro, to copy and paste a multiple cells based on another cell value? 基于单元格值的Excel宏复制范围粘贴偏移量 - Excel Macro Copy Range Paste offset based on cell value 宏以基于单元格值复制范围和粘贴 - Macro to Copy Range and Paste Based on Cell Value 编写一个 Excel 宏来查找和赋值,复制该值并粘贴到单元格中 - Write an Excel macro to find and value, copy that value and paste into a Cell VBA 根据日期将工作表 1 中的单元格值复制/粘贴到工作表 2 的宏 - VBA Macro to copy/paste cell value from sheet 1 to 2 based on date 宏根据特定单元格的值向左重复复制粘贴 - Macro to repeat copy paste to the left based on the value of a particular cell Excel宏:如果单击了单元格A,则复制单元格B并粘贴为值,然后将位置返回到单元格A - Excel Macro: If Cell A is clicked, copy Cell B and paste as value, then return position to Cell A Excel VBA - 基于单元格值在VBA中复制和粘贴循环 - Excel VBA - Copy and Paste Loop in VBA based on cell value Excel宏和VBA,将单元格范围复制到另一个工作表(转置并基于另一个值) - Excel Macro & VBA, Copy Cell Range to another sheet (transposed and based on another value) 根据另一个单元格的值复制并粘贴一列 - Copy and paste down a column based on the value of another cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM