[英]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: 有没有办法:
get to / find the end of the data / rows, in column IE (not just stopping at the first null value) 在IE列中找到/找到数据/行的末尾(不仅止于第一个null值)
determine which of the 3 values is in each row of column IE 确定3个值中的哪个是IE列的每一行
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.