简体   繁体   English

将不同的电池堆叠在一列中

[英]Stack different cells in one column

I just started learning VBA but I can't really figure this out thing out. 我刚刚开始学习VBA,但我真的无法弄清楚。 I have a column with both positive and negative integers and what I want to do is to take the values that are positive and put them in a new column, and do the same for the negative values. 我有一列同时包含正整数和负整数,我想做的是将正值取到新的列中,并对负值做同样的事情。 I tried making an if-statement but I only know how to shift their spots horizontally, so if I have a positive value in row 1,5,7,22 and 24 they'll appear in these rows in the next column instead of being in row 1,2,3,4 and 5. I did that like this: 我尝试过执行if语句,但是我只知道如何水平移动它们的位置,因此,如果我在行1、5、7、22和24中具有正值,它们将出现在下一行的这些行中,而不是在第1,2,3,4和5行中,我这样做是这样的:

For i = 0 To NoofOb 
    If Range("D3").Offset(i + 1) > 0 Then
        Range("G3").Offset(i) = Range("D3").Offset(i + 1)

    ElseIf Range("D3").Offset(i + 1) < 0 Then
        Range("J3").Offset(i) = Range("D3").Offset(i + 1)
    End If
Next i

Could someone give me a hint or anything? 有人可以给我一个提示吗? I've been looking at this for hours and can't find an answer. 我已经看了几个小时了,找不到答案。 Thanks in advance! 提前致谢!

Try this: 尝试这个:

j = 1
k = 1

For i = 3 To NoofOb 
    If Range("D" & i).Value > 0 Then
        Range("G" & j).Value = Range("D" & i).Value
        j = j + 1
    ElseIf Range("D" & i).Value < 0 Then
        Range("J" & k).Value = Range("D" & i).Value
        k = k + 1
    End If
Next i

note: I did not test this code and you may need to adjust the starting points of each column ( i , j and k ) to suit your needs 注意:我没有测试此代码,您可能需要调整每列的起点( ijk )以适合您的需求

Another option, assuming columns G and J are empty before starting the macro 另一种选择,假设在启动宏之前G和J列为空

For i = 3 To NoofOb 
    With Range("D" & i) ‘ reference column D current i row 
        Select Case .Value2 ‘check referenced range value and sct accordingly 
            Case Is > 0 ‘ if it’s positive
                Range("G" & Worksheetfunction.Count(Range("G:G")) + 1.Value2 = .Value2 ‘ fill column G next empty row 
            Case Is < 0 ‘ if it’s negative 
                Range("J" & Worksheetfunction.Count(Range("J:J")) + 1.Value2 = .Value2 ‘ fill column J next empty row 
        End Select
    End With
Next

暂无
暂无

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

相关问题 将不同的列堆叠到不同工作表上的一列中 - Stack different columns into one column on a different worksheet 如果单元格值在另一列的列表中,则突出显示一列中的文本单元格 - Highlight text cells in one column if cell value is in a list in a different column 比较来自两个不同工作表的大量单元格和一列 - Comparing huge range of cells and one column from two different worksheets 一列中的相同值和相邻列的单元格具有不同的值,如何将相邻列的不同值放入单个行中? - Same value in one column and the adjacent column cells have different values, How to put different values of the adjacent column in to a single row? 将一列单元格与另一列单元格进行比较 - Comparing one column of cells to another 如何在跳过空白单元格的同时将一张工作表中的一列复制到另一张工作表中的另一列? - How do you copy one column in one sheet to another column in a different sheet, while skipping over blank cells? Xlsxwriter将不同的格式转换为同一列中的不同单元格 - Xlsxwriter different formats to different cells in same column 复制过滤单元格的列并粘贴到不同的列 - Copy column of filtered cells and paste to different column 搜索一列字符串并将值添加到同一行的四个不同单元格 - Search one column for string and add values to four different cells in same row 匹配不同工作表中的 2 列,并将第三列从一张工作表复制到另一张工作表中以匹配单元格 - Match 2 columns in different sheets and copy 3rd column from one sheet into another for matched cells
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM