简体   繁体   English

查找具有值的列中的最后一个单元格,然后将值移动到新的列/单元格中

[英]Find last cell in a column with a value and move values to new columns/cells

I have a macro I'm working on for my job, and I'm stumped. 我有一个宏正在为工作而工作,但我感到很沮丧。

In my Excel sheet, I have a column (column IE) which will have either "Monthly", "Bimonthly", or "Quarterly" in the cells in that column, depending on the account in that row (each row is a different account, and that account could be paying in any one of the 3 different ways). 在我的Excel工作表中,我有一列(列IE),该列的单元格中将具有“每月”,“每两个月”或“每季度”,具体取决于该行中的帐户(每行是一个不同的帐户) ,并且该帐户可以使用3种不同方式中的任何一种进行付款)。 There can possibly be null values in some of the cells, and each time I run this macro, there will be a different amount of rows with values. 在某些单元格中可能有空值,并且每次我运行此宏时,都会有不同数量的带有值的行。

I need to move the cell values from column IE into corresponding "Monthly" (column B), "Bimonthly" (column C) and "Quarterly" (column D) columns, in the same row. 我需要将IE列中的单元格值移到同一行中相应的“月”(B列),“双月”(C列)和“季度”(D列)列中。

Is there a way to: 有没有办法:

  1. get to / find the end of the data / rows, in column IE (not just stopping at the first null value) 在IE列中找到/找到数据/行的末尾(不仅止于第一个null值)

  2. determine which of the 3 values is in each row of column IE 确定3个值中的哪个是IE列的每一行

  3. move those values to the correct column of that same row, depending on the value (Monthly - column B, Bimonthly - column C, or Quarterly - column D)? 将这些值移动到同一行的正确列中,具体取决于值(每月-B列,每两月-C列或每季度-D列)?

Could I do something like: 我可以做些什么:

Dim lastRow As Long

lastRow = ActiveSheet.Range("IE" & Rows.Count).End(xlUp).Row
Do While ActiveCell.Row <=lastRow

And then add my code to do the "cell value movement" part? 然后添加我的代码来执行“单元值移动”部分?

You could just put an IF statement into each of the columns looking at column IE, such as in B2: 您可以将IF语句放入查看IE列的每个列中,例如B2中:

=IF($IE2 = "Monthly",$IE2,"")

Then you can modify this in C and D for the other two values. 然后,您可以在C和D中为其他两个值修改它。

try something like this: 尝试这样的事情:

Sub yourcode()

LastRow = Range("IE50000").End(xlUp).Row

'I assume values begin at row 2

For i = 1 To LastRow
    Select Case True

    Case IsError(Cells(1 + i, "IE"))
     Nerrors = Nerrors + 1
    Case Cells(1 + i, "IE") = ""
     NBlanks = NBlanks + 1

    Case Cells(1 + i, "IE").Value = "monthly"
        m = m + 1
        Cells(1 + m, "B") = Cells(1 + i, "IE")

    Case Cells(1 + i, "IE").Value = "bimonthly"
         b = b + 1
         Cells(1 + b, "C") = Cells(1 + i, "IE")

    Case Cells(1 + i, "IE").Value = "quarter"
        q = q + 1
        Cells(1 + q, "D") = Cells(1 + i, "IE")

    End Select

Next i

End Sub

I think that's what you wanted. 我想这就是你想要的。

Run the following with the sheet in question active. 在有问题的工作表上运行以下命令。

Private Sub Process()

    Dim v As Variant, vOut As Variant
    Dim i As Long, j As Long, lLastRow As Long

    Const COL = 239 'IE

    With ActiveSheet
        lLastRow = .Cells(.Rows.Count, COL).End(xlUp).Row
        v = .[a1:ie1].Resize(lLastRow).Value2
        ReDim vOut(1 To lLastRow, 1 To 3)
        For i = 1 To UBound(v)
            If Len(v(i, 2)) Then vOut(i, 1) = v(i, 2)
            If Len(v(i, 3)) Then vOut(i, 2) = v(i, 3)
            If Len(v(i, 4)) Then vOut(i, 3) = v(i, 4)
            If Len(v(i, COL)) Then
                Select Case LCase$(v(i, COL))
                    Case "monthly":   vOut(i, 1) = v(i, COL)
                    Case "bimonthly": vOut(i, 2) = v(i, COL)
                    Case "quarterly": vOut(i, 3) = v(i, COL)
                End Select
            End If
        Next
        .[a1].Offset(, 1).Resize(lLastRow, 3) = vOut
    End With

End Sub

暂无
暂无

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

相关问题 按列循环遍历单元格,然后通过函数(Excel VBA)将每个当前单元格值转换为新的单元格值 - Looping through cells by column and converting each current cell value to new cell values through a function (Excel VBA) 比较列中的单元格并将值写入新列 - Comparing Cells in Columns and Writing Values to a New Column 在列中查找最后一个空单元格,然后求和以上所有单元格 - Find Last Empty Cell in a Column & then Sum all the Cells Above 在特定列中找到最后一个具有值的单元格 - Find the last cell with a value in specific column 根据列标题在最后一个单元格中查找值 - Find value in the last cell based on column header 在与值匹配的列中查找最后一个单元格 - Find last cell in a column that matches the value 如何遍历列中具有相同值的单元格,直到最后一个单元格 - how to loop through cells with same value in a column until last cell 如果其他两列的值匹配,则将值从一张工作表中的单元格复制到另一张工作表中最后使用的列中的单元格 - Copy value from a cell in one sheet to a cell in the last used Column in another sheet, if values in two other Columns' values match Excel公式将单元格可能包含多个值的列拆分为另一列,其中每个单元格仅包含一个值 - Excel Formula to split columns where cells may contain multiple values into another column, where each cell only contains a single value 在一个列中查找多个单元格,从相邻列中选择单元格,然后分别为每个单元格调用一个模块 - Find multiple cells in a column, select cells from adjacent columns and call a module for each cell separately
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM