Excel VBA宏 - 将空间范围内的单元格内容分隔为行并捕获原始单元格位置

时间:2016-04-12 23:50:58

标签: excel vba excel-vba

我有一个库存电子表格,每个单元格有多个条目,用空格分隔。他们使用当前的单元格位置作为参考编号,因此我需要保留该信息。

例:
   电流

function findlineshv(I)

% Read Image
img = imread(I);

% Convert to black and white because
% edge function only works with BW imgs
bwImage = rgb2gray(img);

% figure(1),imshow(bwImage);

% find edges using edge function
b=edge(bwImage,'sobel');

% show edges
% figure(1),imshow(b);


% compute the Hough transform of the edges found
% by the edge function
[hou,theta,rho] = hough(b);

% define peaks, x and y
peaks = houghpeaks(hou,5,'threshold',ceil(0.3*max(hou(:))));

x = theta(peaks(:,2));
y = rho(peaks(:,1));


lines = houghlines(bwImage,theta,rho,peaks,'FillGap',5,'MinLength',7);

figure, imshow(bwImage), hold on

for k = 1:length(lines)
    xy = [lines(k).point1; lines(k).point2];
    plot(xy(:,1),xy(:,2),'LineWidth',3,'Color','red');
end

渴望

Cell A1 - PartA PartB PartC  
Cell A2 - PartD PartE

我已经完成了我的研究,最接近的VBA代码会执行拆分信息,但它会将其粘贴到下一行,对于一个范围,这对我没有好处。 SO: Comma Separated Rows

我已经尝试更改代码以使其适用于某个范围,但是遇到麻烦却无法确定是什么问题。我也很难弄清楚如何捕获原始单元格位置并输出到其他行。 (如果它不能从A1分到A& 1,我可以忍受,但这是理想的。)

提前感谢任何帮助指明方向!

当前代码:

Cell A1 - PartA  Cell B1 - A 'OriginalColumn Cell C1 - 1 'OriginalRow  
Cell A2 - PartB  Cell B2 - A 'OriginalColumn Cell C2 - 1 'OriginalRow  
Cell A3 - PartC  Cell B3 - A 'OriginalColumn Cell C3 - 1 'OriginalRow  
Cell A4 - PartD  Cell B4 - A 'OriginalColumn Cell C4 - 2 'OriginalRow  
Cell A5 - PartE  Cell B5 - A 'OriginalColumn Cell C5 - 2 'OriginalRow  

1 个答案:

答案 0 :(得分:0)

此代码应该按照您的意愿(希望如此)执行:

carry = sum / 2; // integer division
arr[i] = sum % 2; // modulo

它被设计为仅适用于具有多行的一列但不使用类似" A:A"的范围。 (只是包含值的范围)或者它可能会冻结excel一段时间;)

工作原理:

它检查范围内的每个单元格是否有任何内容(空白单元格将被跳过,单元格只包含空格)。
然后将每个单元格值拆分为一个数组(用空格分割)。此数组将附加到现有输出数组(同样,将跳过空白部分)。

由于Sub SplitIt() Dim rng As Range Set rng = Sheets("Sheet1").Range("A1:A10") 'change that as you need it (the data to be splited) Dim rngVal As Variant rngVal = rng.Value Dim xSpl As Variant, xSpl2 As Variant, xVal() As Variant, i As Long, j As Long, col As String j = rng.Row col = Split(Columns(rng.Column).Address(, 0), ":")(0) ReDim xVal(1 To 3, 0) For Each xSpl In rngVal If Len(xSpl) Then xSpl = Split(Trim(xSpl), " ") If UBound(xVal, 2) Then ReDim Preserve xVal(1 To 3, 1 To i + UBound(xSpl) + 1) Else ReDim xVal(1 To 3, 1 To UBound(xSpl) + 1) End If For Each xSpl2 In xSpl If Len(xSpl2) Then i = i + 1 xVal(1, i) = xSpl2 xVal(2, i) = col xVal(3, i) = j End If Next End If j = j + 1 Next Sheets("Sheet2").Range("A1").Resize(i, 3) = Application.Transpose(xVal) 'Change the sheet and the "A1" to the upper left cell to output to End Sub 的工作方式,我们得到一个转置数组,我们需要在结束时进行转置以进行输出。这就是全部......(列/行的部分应该是自我解释)

如果您还有任何疑问,请询问;)