简体   繁体   English

Excel宏和VBA,将单元格范围复制到另一个工作表(转置并基于另一个值)

[英]Excel Macro & VBA, Copy Cell Range to another sheet (transposed and based on another value)

I have tried to frankenstein a solution from different threads but I am very much a newbie to VBA and programming, so it has not been going too well... 我曾尝试从不同的线程开始弗兰肯斯坦的解决方案,但我是VBA和编程的新手,因此进展不太顺利...

Here's some basic info: 以下是一些基本信息:

  • I have 2 Sheets within the same workbook (Database and Data entry) 我在同一工作簿中有2个工作表(数据库和数据条目)
  • They contain the same headers, but are transposed (Database has the headers in the columns while Data entry has them in the rows) 它们包含相同的标题,但是是转置的(数据库在列中包含标题,而数据条目在行中包含标题)

Now, I am looking for 3 things (ideally in one compact solution) 现在,我正在寻找3件事(最好是在一个紧凑的解决方案中)

  1. Have a Command Button that copies and transposes the most recent range (leftmost column) from Data Entry to Database. 有一个命令按钮,用于将最新范围(最左边的列)从数据输入复制并转置到数据库。 (This is done in the code below) (这在下面的代码中完成)

  2. This should be done depending on a certain cell value on the data entry sheet (ideally that cell could stay part of the copied range, however this is not crucial) 应当根据数据输入表上的某个单元格值执行此操作(理想情况下,该单元格可以保留复制范围的一部分,但这并不重要)

  3. Delete the original range from the data entry sheet. 从数据输入表中删除原始范围。

As I said I'm just starting to work with VBA so I am completely unsure how to go about this, I have attached what I gathered so far (excludes Nr.2 and feels very cumbersome overall). 正如我说过的那样,我刚开始使用VBA,所以我完全不确定该怎么做,我已经附加了到目前为止收集到的内容(不包括Nr.2,总体上感觉非常麻烦)。 Any help is very much appreciated! 很感谢任何形式的帮助!

Private Sub CommandButton1_Click()
    Application.ScreenUpdating = False
    Dim xSheet As Worksheet
    Set xSheet = ActiveSheet
        If xSheet.Name <> "Definitions" And xSheet.Name <> "fx" And xSheet.Name <> "Needs" Then
            xSheet.Range("E6:E200").Copy
            Worksheets("Sheet1").Range("E6:AZ6").PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
        End If

    Application.ScreenUpdating = True
End Sub

Check this one. 检查这一。 It checks the value of E10. 它检查E10的值。 If it's "Y", then data is copied and deleted from original place. 如果为“ Y”,则从原始位置复制和删除数据。 Otherwise, it shows a message to the user. 否则,它会向用户显示一条消息。

Private Sub CommandButton1_Click()
    Application.ScreenUpdating = False
    Dim xSheet As Worksheet
    Set xSheet = ActiveSheet
        If xSheet.Name <> "Definitions" And xSheet.Name <> "fx" And xSheet.Name <> "Needs" Then
            If xSheet.Range("E10")="Y"
                xSheet.Range("E6:E200").Copy
                Worksheets("Sheet1").Range("E6:AZ6").PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
                xSheet.Columns("E").Delete
            Else
                MsgBox("Data entry not ready!")
            End If
        End If

    Application.ScreenUpdating = True
End Sub

暂无
暂无

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

相关问题 Macro Excel可根据单元格匹配将范围单元格从一张纸复制到另一张纸,如果不匹配,则跳过单元格 - Macro Excel to copy range cells from one sheet to another based on cell match and skip cell if no match Excel宏复制单元格范围并将数据粘贴到另一个工作表 - Excel Macro Copy cell range & paste data one sheet to another 如果值已经存在,VBA 宏不会将单元格复制到另一个工作表 - VBA Macro to not Copy Cell over to Another Sheet if Value Already Exists Excel宏,要基于另一个单元格值复制和粘贴单元格值? - Excel macro, to copy and paste a cell value based on another cell value? Excel宏可根据单元格值将一行中的选定单元格从一张纸复制到另一张纸 - Excel Macro to copy select cells from a row from one sheet to another based upon a cell value Excel VBA代码将基于单元格引用的数据复制到另一个工作表 - Excel VBA Code to Copy Data based on a cell reference to another sheet Excel宏-根据值将单元格从一张纸复制到另一张纸 - Excel Macro - copy cells from one sheet into another based on value 如果单元格值大于0,excel vba将单元格复制到另一个工作表 - excel vba copy cells to another sheet if cell value is greater than 0 Excel VBA 根据第二列单元格值将列从一张表复制到另一张表 - Excel VBA to Copy Column from one sheet to another based on a second columns cell value 如何根据单元格值将行内的范围从一个 Excel 工作表复制到另一个 - How to copy a range within a row from one excel sheet to another based on a cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM