Excel 两个单元格之间的填充值
我在宏中有一些代码,可以自动填充每行2个单元格之间的值。它只是将相同的值复制到不同的位置 代码运行得非常好,但在某些行中只有1个值,它会直接复制到行的末尾 我试图添加一个If语句,但我认为我没有得到正确的条件,而且它也不起作用Excel 两个单元格之间的填充值,excel,vba,Excel,Vba,我在宏中有一些代码,可以自动填充每行2个单元格之间的值。它只是将相同的值复制到不同的位置 代码运行得非常好,但在某些行中只有1个值,它会直接复制到行的末尾 我试图添加一个If语句,但我认为我没有得到正确的条件,而且它也不起作用 Dim wS As Worksheet Dim LastRow As Double Dim LastCol As Double Dim i As Double Dim j As Double Dim k As Double
Dim wS As Worksheet
Dim LastRow As Double
Dim LastCol As Double
Dim i As Double
Dim j As Double
Dim k As Double
Dim RowVal As String
Set wS = ThisWorkbook.Sheets("Overview")
LastRow = LastRow_1(wS)
LastCol = LastCol_1(wS)
For i = 7 To LastRow
For j = 8 To LastCol
With wS
If .Cells(i, j) <> vbNullString Then
'1st value of the row found
RowVal = .Cells(i, j).Value
k = 1
'Fill until next value of that row
Do While j + k <= LastCol And .Cells(i, j + k) = vbNullString
.Cells(i, j + k).Value = RowVal
k = k + 1
Loop
'Go to next row
Exit For
Else
End If
End With 'wS
Next j
Next i
Public Function LastCol_1(wS As Worksheet) As Double
With wS
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
LastCol_1 = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
Else
LastCol_1 = 1
End If
End With
End Function
Public Function LastRow_1(wS As Worksheet) As Double
With wS
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
LastRow_1 = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
LastRow_1 = 1
End If
End With
End Function
Dim wS As工作表
将最后一行变暗为双行
双色调暗
我是双人的
双倍调暗j
把k调成双色
作为字符串的Dim RowVal
设置wS=ThisWorkbook.Sheets(“概述”)
LastRow=LastRow_1(wS)
LastCol=LastCol_1(wS)
从i=7到最后一行
对于j=8到LastCol
与wS
如果.Cells(i,j)为空字符串,则
'找到的行的第一个值
RowVal=.Cells(i,j).Value
k=1
'填充到该行的下一个值
当j+k时,我倾向于将开始列和结束列保存在两个变量中,并使用单独的循环来填充值:
Sub Fill()
Dim wS As Worksheet
Dim LastRow As Double
Dim LastCol As Double
Dim i As Double
Dim j As Double
Dim k As Double
Dim RowVal As String
Dim startCol, endCol As Long
Set wS = ThisWorkbook.Sheets("Overview")
LastRow = LastRow_1(wS)
LastCol = LastCol_1(wS)
For i = 7 To LastRow
startCol = 0
endCol = 0
With wS
For j = 8 To LastCol
If .Cells(i, j) <> vbNullString And startCol = 0 Then
'First non-empty cell found
RowVal = .Cells(i, j).Value
startCol = j
Else
'Next non-empty cell found
If .Cells(i, j) <> vbNullString And startCol > 0 Then
endCol = j
Exit For
End If
End If
Next j
' Now fill values
For j = startCol + 1 To endCol - 1
.Cells(i, j).Value = RowVal
Next j
End With 'wS
Next i
End Sub
'LastRow_1 and LastCol_1 as before.
Sub-Fill()
将wS设置为工作表
将最后一行变暗为双行
双色调暗
我是双人的
双倍调暗j
把k调成双色
作为字符串的Dim RowVal
暗淡的startCol,与endCol一样长
设置wS=ThisWorkbook.Sheets(“概述”)
LastRow=LastRow_1(wS)
LastCol=LastCol_1(wS)
从i=7到最后一行
startCol=0
endCol=0
与wS
对于j=8到LastCol
如果.Cells(i,j)vbNullString和startCol=0,则
'找到第一个非空单元格
RowVal=.Cells(i,j).Value
startCol=j
其他的
'找到下一个非空单元格
如果.Cells(i,j)vbNullString和startCol>0,则
endCol=j
退出
如果结束
如果结束
下一个j
'现在填充值
对于j=startCol+1至endCol-1
.单元格(i,j).值=行值
下一个j
以“wS”结尾
接下来我
端接头
'LastRow_1和LastCol_1与前面一样。
我倾向于将开始列和结束列保存在两个变量中,并使用单独的循环来填充值:
Sub Fill()
Dim wS As Worksheet
Dim LastRow As Double
Dim LastCol As Double
Dim i As Double
Dim j As Double
Dim k As Double
Dim RowVal As String
Dim startCol, endCol As Long
Set wS = ThisWorkbook.Sheets("Overview")
LastRow = LastRow_1(wS)
LastCol = LastCol_1(wS)
For i = 7 To LastRow
startCol = 0
endCol = 0
With wS
For j = 8 To LastCol
If .Cells(i, j) <> vbNullString And startCol = 0 Then
'First non-empty cell found
RowVal = .Cells(i, j).Value
startCol = j
Else
'Next non-empty cell found
If .Cells(i, j) <> vbNullString And startCol > 0 Then
endCol = j
Exit For
End If
End If
Next j
' Now fill values
For j = startCol + 1 To endCol - 1
.Cells(i, j).Value = RowVal
Next j
End With 'wS
Next i
End Sub
'LastRow_1 and LastCol_1 as before.
Sub-Fill()
将wS设置为工作表
将最后一行变暗为双行
双色调暗
我是双人的
双倍调暗j
把k调成双色
作为字符串的Dim RowVal
暗淡的startCol,与endCol一样长
设置wS=ThisWorkbook.Sheets(“概述”)
LastRow=LastRow_1(wS)
LastCol=LastCol_1(wS)
从i=7到最后一行
startCol=0
endCol=0
与wS
对于j=8到LastCol
如果.Cells(i,j)vbNullString和startCol=0,则
'找到第一个非空单元格
RowVal=.Cells(i,j).Value
startCol=j
其他的
'找到下一个非空单元格
如果.Cells(i,j)vbNullString和startCol>0,则
endCol=j
退出
如果结束
如果结束
下一个j
'现在填充值
对于j=startCol+1至endCol-1
.单元格(i,j).值=行值
下一个j
以“wS”结尾
接下来我
端接头
'LastRow_1和LastCol_1与前面一样。
您可以包括LastCol\u 1()
和LastRow\u 1()
的代码吗?为什么不添加一个检查,确保行中有多个已填充的单元格?您可以包括LastCol\u 1()
和LastRow\u 1()
的代码吗?为什么不添加一个检查,确保行中有多个已填充的单元格?