修改程序代码,谢谢~
修改一下程序代码,谢谢!!!!
自动填充上一个单元格的公式,因为公式是vlookup,可能有空值,一旦遇到空值就无法继续填充了,请问怎么继续往下填充,谢谢!
Dim j As Integer
Dim thissheet As Worksheet
Dim sheetname As String
Dim i_flag As Integer
Dim columns_to_fill As String
Dim thiscolumn As String
i_flag = 0
sheetname = InputBox("Please input the name of the worksheet" & Chr(13) & Chr(10) & _
"where you want to fill in blank cells.", "")
If Trim(sheetname) = "" Then
Exit Sub
End If
'To check if the worksheet with this name exists
For Each thissheet In ThisWorkbook.Sheets
If UCase(thissheet.Name) = UCase(sheetname) Then
i_flag = 1
Exit For
End If
Next
If i_flag = 0 Then
MsgBox "There's no worksheet with name '" & sheetname & "' in this workbook!"
Exit Sub
End If
thissheet.Activate
columns_to_fill = InputBox("Please input column(s) in which you need to fill:" & _
Chr(13) & Chr(10) & "(if you have several columns, separate by "","" )", "", )
columns_to_fill = Trim(columns_to_fill)
If columns_to_fill = "" Then
Exit Sub
End If
'Loop with columns one by one
Do While columns_to_fill <> ""
If InStr(columns_to_fill, ",") > 0 Then
thiscolumn = Left(columns_to_fill, InStr(columns_to_fill, ",") - 1)
columns_to_fill = Mid(columns_to_fill, InStr(columns_to_fill, ",") + 1, 100)
Else
thiscolumn = columns_to_fill
columns_to_fill = ""
End If
thiscolumn = UCase(thiscolumn)
If (Len(thiscolumn) = 1 Or Len(thiscolumn) = 2) And _
Asc(Left(thiscolumn, 1)) >= Asc("A") And Asc(Left(thiscolumn, 1)) <= Asc("Z") And _
Asc(Right(thiscolumn, 1)) >= Asc("A") And Asc(Right(thiscolumn, 1)) <= Asc("Z") Then
On Error Resume Next
For j = 2 To thissheet.UsedRange.Rows.Count
If thissheet.Range(thiscolumn & j) = "" And thissheet.Range(thiscolumn & (j - 1)) <> "" Then
thissheet.Range(thiscolumn & (j - 1)).Copy
thissheet.Range(thiscolumn & j).Select
thissheet.Paste
End If
Next
thissheet.Columns(thiscolumn).Select
MsgBox "Blanks in column " & thiscolumn & " have been filled.", vbInformation
Else
MsgBox "Column name: '" & thiscolumn & "' is incorrect!", vbExclamation, "Success!"
End If
Loop
[解决办法]
你说的不到行数就结束有可能是thissheet.UsedRange.Rows.Count不到行数,其实找行数很简单,在Vlookup中就有要找的值,值所在的列就是行数,然后直接range("A2:A100").FormulaR1C1= ... 就做了,何必非要 For j = 2 To thissheet.UsedRange.Rows.Count 呢。