[英]How can I place a number X in Column C(if cell is empty) where X is equal to the same row in column B minus 1?
[英]How can I write in a cell that is X number of column away from the cell I am on (X is a value defined in another cell)
修改代码并尝试:
Option Explicit
Sub TEST()
Dim LastRow As Long, LastColumn As Long, LastColumn2 As Long, i As Long, y As Long
Dim arr As Variant
With ThisWorkbook.Worksheets("Sheet1")
arr = Array("A", "B", "C") '<- Create an array with the desirable Items
For i = LBound(arr) To UBound(arr) '<- Loop array
'Find Last column of row 12 in order to import Titles
LastColumn = .Cells(12, .Columns.Count).End(xlToLeft).Column
If LastColumn = 1 And .Cells(11, LastColumn).Value = "" Then
.Cells(11, LastColumn).Value = arr(i)
Else
.Cells(11, LastColumn + 1).Value = arr(i)
End If
'Find Last row of column B in order to create the loop range
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
'Loop column B from 1 to last row of column B
For y = 1 To LastRow
'Check if looping value is the same with array value
If .Range("B" & y).Value = arr(i) Then
'Find Last column of row 12 in order to import values
LastColumn2 = .Cells(12, .Columns.Count).End(xlToLeft).Column
If LastColumn2 = 1 And .Cells(12, LastColumn2).Value = "" Then
.Cells(12, 1).Value = .Range("A" & y).Value
Else
.Cells(12, LastColumn2 + 1).Value = .Range("A" & y).Value
End If
End If
Next y
Next i
End With
End Sub
结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.