美好的一天,
我非常擅长使用vba宏进行编码和全新编码。在excel中发现vba的有用性之后,我现在正在努力学习更多有关它的知识。非常感谢任何帮助或帮助。
这是我目前使用的代码:
Option Explicit
Const initrow As Integer = 3
Const ENDROW As Long = 65536
Const PrimaryLengthCol As Integer = 1 '"A"
Sub Test()
Dim lastrow As Double
Dim i As Double
Dim irow As Double
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Application.ScreenUpdating = False
irow = 0
i = 0
For i = 0 To lastrow
If Cells(initrow + irow, PrimaryLengthCol + 2) = "BLANK" Then
Continue For
Cells(initrow + i, PrimaryLengthCol + 3).Value = Cells(initrow + irow, PrimaryLengthCol + 2).Value
End If
Next
End Sub
基本上我遇到的问题如下:
我在“A”栏(输入栏)中有以下内容:
我需要通过每个单元格的LOOP的vba代码,如果单元格等于“BLANK”(文本字符串),则相应的单元格将采用后续数字。另外,如果列“A”的值等于零,则输出等于“B”列中的“void”。
因此,“B”列(输出列)中所需的输出需要为:
最后一个标准是,如果列“A”中等于“BLANK”的单元格在前一个单元格中的前面是零值,那么“BLANK”也将等于输出列中的“void”值“ B“:
所以如果列“A”有这种情况:
输出栏“B”必须是:
我不确定如何应用Continue For因为我希望循环“跳过”“BLANK”单元格以进行下一次迭代,但仍然将相应的值填入“B”列并随后进行限定值。我宁愿通过vba完成这个,因为我再一次尝试学习这门语言,所以我强迫自己增加对它的接触。
对此事的任何协助都将再次受到高度赞赏。
谢谢!
答案 0 :(得分:0)
这是一种有点不同的方法,可以更容易使用和修改(未经测试):
Dim cell As Range
Set cell = Cells(Rows.Count, "A").End(xlUp) ' last cell
While cell.Row > 2 ' loop until row 3
If cell = 0 Then
cell(, 2) = "Void" ' cell(, 2) is the cell on the right of cell
Else If cell = "BLANK" Then
cell(, 2) = cell(2) ' value from the cell below
Else
cell(, 2) = cell ' else just use the same value
End If
Set cell = cell(0) ' move to the cell above
Wend
使用Excel R1C1公式的更高级方法(也未经过测试):
Dim colB As Range
Set colB = ThisWorkbook.Worksheets("Sheet1").Range("A3").CurrentRegion.Offset(,1).Resize(,1)
colB.FormulaR1C1 = "=IF(RC[-1]=0, ""Void"", IF(RC[-1]=""BLANK"", R[1]C[-1], RC[-1]))"
colB.Value2 = colB.Value2 ' optional to convert the formulas to values
.CurrentRegion
得到一个由空单元格围绕的矩形范围(类似于单击单元格A3
并按Ctrl + A),然后.Offset(,1)
将范围移动到B列,并且如果列B不为空,.Resize(,1)
是可选的,可以将范围调整为一列。
答案 1 :(得分:0)
因此,基于Slai的提示,我非常感谢,结合我已经玩过的代码,问题已经通过以下代码解决了。我不得不将它分成两个函数来完成我所有的原始标准。如果某人有更有效的方法来解决问题,我会非常乐意了解它。
Option Explicit
Const initrow As Integer = 3
Const ENDROW As Long = 65536
Const PrimaryLengthCol As Integer = 1 '"A"
Sub FirstIter()
' initial iteration that exacts value from the adjustment column
Dim i As Double
Dim irow As Double
Worksheets(MatchMLWorksheet).Activate
irow = 0
While Not (IsEmpty(Cells(initrow + irow, PrimaryLengthCol + 2))) ' loop until empty cell
If Cells(initrow + irow, PrimaryLengthCol + 2).Value = 0 Then
Cells(initrow + irow, PrimaryLengthCol + 2).Offset(, 1) = "Void" ' cell.Offset(, 1) is the cell on the right
ElseIf Cells(initrow + irow, PrimaryLengthCol + 2).Value = "BLANK" Then
i = irow ' sets the count to where cell iteration is
Do
i = i + 1 ' increments the Do Until loop untils
' it hits the first cell with "BLANK"
Loop Until Cells(initrow + i, PrimaryLengthCol + 2).Value <> "BLANK" Or Cells(initrow + i, PrimaryLengthCol + 2).Value <> 0
Cells(initrow + irow, PrimaryLengthCol + 2).Offset(, 1) = Cells(initrow + i, PrimaryLengthCol + 2).Value
' Overall counter is at the iteration of "blank"
' resets counter to match overall loop
Else
Cells(initrow + irow, PrimaryLengthCol + 2).Offset(, 1) = Cells(initrow + irow, PrimaryLengthCol + 2).Value ' else just use the same value
End If
irow = irow + 1 ' move to the cell below
Wend
End Sub
Sub FinalIter()
'Checks entire column to see if it contains any "BLANK"
Worksheets(MatchMLWorksheet).Activate
Dim num As Double
num = 0
Dim cell As Range
Dim iMsg As Integer
Dim b As Double
Columns("D:D").Select
Set cell = Selection.Find(What:="BLANK", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If cell Is Nothing Then
' There are no more "BLANK"
iMsg = MsgBox("There are no more BLANK values!", vbOKOnly)
Else
While Not (IsEmpty(Cells(initrow + num, PrimaryLengthCol + 3))) ' loop until empty cell
If Cells(initrow + num, PrimaryLengthCol + 3).Value = "BLANK" Then
b = num ' sets the count to where cell iteration is
Do
b = b + 1 ' increments the Do Until loop untils
' it hits the first cell with "BLANK"
Loop Until Cells(initrow + b, PrimaryLengthCol + 3).Value <> "BLANK"
Cells(initrow + num, PrimaryLengthCol + 3) = Cells(initrow + b, PrimaryLengthCol + 3).Value
' Overall counter is at the iteration of "blank"
' resets counter to match overall loop
End If
num = num + 1 ' move to the cell below
Wend
End If
End Sub