简体   繁体   English

在选定的单元格中运行宏

[英]Run macro in selected cells

I need to create a simple macro to clean my worksheets.我需要创建一个简单的宏来清理我的工作表。 Basically, if there are multiple orders on 1 shipment, I need those orders to be displayed vertically instead of horizontally example:基本上,如果 1 批货物有多个订单,我需要垂直显示这些订单而不是水平显示示例:

excel example例子

在此处输入图像描述

I created a macro that will copy/paste the 1st row into the row below it and then change the 2nd order with another copy/paste.我创建了一个宏,它将第一行复制/粘贴到它下面的行中,然后用另一个复制/粘贴更改第二个顺序。

Pretty simple.很简单。 My problem is the macro is binded to the ranges I created the macro in.我的问题是宏绑定到我在其中创建宏的范围。

How can I make it so I can run this macro on selected ranges.我怎样才能做到这一点,以便我可以在选定的范围内运行这个宏。 Rather than manually copy and pasting every row with multiple orders, I'd rather highlight the rows with multiple orders and run the macro.与其手动复制和粘贴具有多个订单的每一行,我宁愿突出显示具有多个订单的行并运行宏。

This is the code:这是代码:

ActiveCell.Range("A1:M1").Select
Selection.Copy
ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select
Selection.Insert Shift:=xlDown
ActiveCell.Offset(0, 3).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
ActiveCell.Offset(0, -1).Range("A1").Select
ActiveSheet.Paste
ActiveCell.Offset(-1, 1).Range("A1:A2").Select
Application.CutCopyMode = False
Selection.ClearContents

vba code vba代码

If I understand you correctly, maybe something like this ?如果我理解正确,也许是这样的?

need those orders to be displayed vertically instead of horizontally需要垂直显示这些订单而不是水平显示

Sub test1()
Dim rg As Range
Dim cc As Range

Set rg = Range("C2")

Do
    If rg.Offset(0, 1).Value <> "" Then
        Set cc = Range(rg, rg.End(xlToRight))
        Rows(rg.Row).Copy
        Rows(rg.Row + 1 & ":" & rg.Row + cc.Columns.Count - 1).Insert Shift:=xlDown
        rg.Resize(cc.Columns.Count, 1).Value = Application.Transpose(cc)
        rg.Offset(0, 1).Resize(cc.Columns.Count, cc.Columns.Count - 1).ClearContents
        Set rg = rg.Offset(cc.Columns.Count, 0)
    Else
        Set rg = rg.Offset(1, 0)
    End If
Loop Until rg.Value = ""

End Sub

eSub 测试1

How can I make it so I can run this macro on selected ranges我怎样才能做到这一点,以便我可以在选定的范围内运行这个宏

Sub test2()
Dim rg As Range
Set rg = Application.InputBox("Select a certain row starts from column C", Type:=8)
Rows(rg.Row).Copy
Rows(rg.Row + 1 & ":" & rg.Row + rg.Columns.Count - 1).Insert Shift:=xlDown
rg.Resize(rg.Columns.Count, 1).Value = Application.Transpose(rg)
rg.Offset(0, 1).Resize(rg.Columns.Count, rg.Columns.Count - 1).ClearContents
End Sub

子测试2

For sub test1对于子测试1
The code assumed that the delivery number will start in cell C2 which defined as variable rg then do a loop代码假定交货编号将从定义为变量 rg 的单元格 C2 开始,然后执行循环

If the the cell to the right of the rg is not empty,如果 rg 右边的单元格不为空,
then it define a range from the rg to the last column of the rg row which has value as cc variable.然后它定义从 rg 到 rg 行的最后一列的范围,其值为 cc 变量。
Then it copy insert as many as the columns are there inside cc.然后它复制插入尽可能多的列在 cc 中。
Then it transpose the cc value from column to row.然后它将 cc 值从列转置到行。
Then it delete the uneeded value.然后它删除不需要的值。

If the the cell to the right of the rg is empty,如果 rg 右侧的单元格为空,
then it doesn't do a process, it just reset the rg to the cell below.然后它不执行任何过程,它只是将 rg 重置为下面的单元格。

For sub test2对于子测试2
It ask a user to select a range, starts from column C to whatever last column (with value) within the same row.它要求用户选择一个范围,从 C 列开始到同一行中的最后一列(带有值)。 Then do the similar process like in test1.然后像在 test1 中一样执行类似的过程。

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

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