简体   繁体   English

如何将宏应用于整个列?

[英]How do I apply a macro to an entire column?

I have a macro that, when run it separates values into different cells within the row.我有一个宏,运行时它将值分隔到行内的不同单元格中。 The delimiter is two spaces or more.分隔符为两个或更多空格。 This is an example, all of this is in one cell.这是一个例子,所有这些都在一个单元格中。

One Space    Two  Spaces    Three   Spaces

Will become:会变成:

One Space Two Spaces Three Space

With each word being in its own cell.每个单词都在自己的单元格中。

I have one whole column with about 300,000 rows with data similar to that, I would like how know how can I apply the following macro to the entire column.我有一整列大约有 300,000 行的数据与此类似,我想知道如何将以下宏应用于整列。

Dim rng As Range
Dim txt As String
Dim FullName() As String
Dim i As Long, colOffset As Long

Set rng = ActiveCell
txt = rng.Value2

FullName = Split(txt, String(2, " "))

For i = LBound(FullName) To UBound(FullName)
    If Not WorksheetFunction.Trim(FullName(i)) = vbNullString Then
        Debug.Print WorksheetFunction.Trim(FullName(i))
        colOffset = colOffset + 1
        rng.Offset(0, colOffset).Value2 = WorksheetFunction.Trim(FullName(i))
    End If
Next i

This will split the selected cells by spaces, into the columns to the right.这将按空格将选定的单元格拆分为右侧的列。

For Each c In Selection.Cells
    cellvalues = Split(WorksheetFunction.Trim(c.Value), " ")
    c.Offset(0, 1).Resize(1, UBound(cellvalues)).Value = cellvalues
Next

Just remove the Offset(0,1).只需删除Offset(0,1). part to overwrite the values.部分覆盖值。 Instead of Selection , you could use a dynamic range or one identified by code.您可以使用动态范围或由代码标识的范围,而不是Selection

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

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