需要使用Macro连接不同数量的单元格 [英] Need to concatenate varying number of cells using Macro
问题描述
我需要根据上一个单元格中的一个变量来连接单元格列。这将继续,直到指定的变量更改。例如:
I need to concatenate a column of cells based on a variable in a previous cell. This will continue on until the specified variable changes. For example:
A B C D E
1 x @1 @1+@2+@3
2 x @2
3 x @3
4 y %1 %1+%2+%3
5 y %2
6 y %3
etc.
我需要宏来查看A1,如果是x,则在E1中开始连接的字符串。然后移动到A2,如果它是将E1添加到E1中的级联值,然后移动到A3,如果它是将D3中的值添加到E1中的级联值,一旦它在列A(y)中命中了一个新变量)过程重新开始。这是可能的吗非常感谢您的帮助!!
I need the macro to look at A1 and if it's x then begin a concatenated string in E1. Then move to A2, if it's x add D2 to the concatenated value in E1, then move to A3, if it's x add the value in D3 to the concatenated value in E1, etc. Once it hits a new variable in column A (y) the process starts over. Is this at all possible? Thanks very much for your help!!
推荐答案
这是一些快速而脏的代码,但它的功能是:
This is some quick-and-dirty code, but it functions:
Dim i As Integer
Dim j As Integer
i = 1
j = 1
Dim initialValue As String
initialValue = Cells(i, 1).Value
Do While Cells(i, 1).Value <> ""
Cells(j, 5).Value = ""
Do While Cells(i, 1).Value = initialValue
Cells(j, 5).Value = Cells(j, 5).Value & Cells(i, 4).Value
i = i + 1
Loop
initialValue = Cells(i, 1).Value
j = j + 1
Loop
它假定活动工作表是具有列的活动工作表。列编号是硬编码的,您从第1行开始。
It assumes that the active sheet is the one with your columns. And the column numbers are hard-coded, and you are starting in row 1.
这篇关于需要使用Macro连接不同数量的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!