【问题标题】:If is cell empty, get data function return / (slash)如果单元格为空,则获取数据函数返回/(斜线)
【发布时间】:2014-03-16 21:21:21
【问题描述】:

我从已关闭的工作簿中的特定单元格获取数据,但如果单元格为空,我将获得空单元格。我需要改进获取数据功能,所以如果我要从中获取数据的单元格为空,则获取数据函数返回“/”或其他字符。

非常感谢!

Sub Recurse()
Dim FSO As New FileSystemObject
Dim myFolder As Scripting.Folder, mySubFolder As Scripting.Folder
Dim myFile As File
Dim sPath$: sPath = "C:\Users\Marek\Desktop\skuska\"
Dim R$
R = Join(Application.Transpose(Sheets("Sheet2").UsedRange), "|")
Set myFolder = FSO.GetFolder(sPath)
For Each mySubFolder In myFolder.SubFolders
For Each myFile In mySubFolder.Files
    DoEvents
    If Not (InStr(1, R, myFile.Path) > 0) Then
        GetData myFile, "Sheet1", "A1:A2",     Sheets("Sheet1").Range(Sheets(1).Cells(Sheet1.Cells(Rows.Count, 1).End(xlUp).Row + 1, 1), Sheets(1).Cells(Sheet1.Cells(Rows.Count, 1).End(xlUp).Row + 1, 1)), True, False
        GetData myFile, "Sheet1", "B1:B2", Sheets("Sheet1").Range(Sheets(1).Cells(Sheet1.Cells(Rows.Count, 2).End(xlUp).Row + 1, 2), Sheets(1).Cells(Sheet1.Cells(Rows.Count, 2).End(xlUp).Row + 1, 2)), True, False
        GetData myFile, "Sheet1", "C1:C2", Sheets("Sheet1").Range(Sheets(1).Cells(Sheet1.Cells(Rows.Count, 3).End(xlUp).Row + 1, 3), Sheets(1).Cells(Sheet1.Cells(Rows.Count, 3).End(xlUp).Row + 1, 3)), True, False
        Sheets("Sheet2").Cells(Sheets("Sheet2").UsedRange.Rows.Count + 1, 1).Value = myFile.Path
        R = R & myFile.Path & "|"
    End If
Next
Next
 Set FSO = Nothing
Set myFolder = Nothing
Set mySubFolder = Nothing
Set myFile = Nothing

结束子

Option Explicit


Public Sub GetData(SourceFile As Variant, SourceSheet As String, _
               SourceRange As String, TargetRange As Range, Header As Boolean,     UseHeaderRow As Boolean)
' 30-Dec-2007, working in Excel 2000-2007
Dim rsCon As Object
Dim rsData As Object
Dim szConnect As String
Dim szSQL As String
Dim lCount As Long

' Create the connection string.
If Header = False Then
    If Val(Application.Version) < 12 Then
        szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                    "Data Source=" & SourceFile & ";" & _
                    "Extended Properties=""Excel 8.0;HDR=No"";"
    Else
        szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                    "Data Source=" & SourceFile & ";" & _
                    "Extended Properties=""Excel 12.0;HDR=No"";"
    End If
Else
    If Val(Application.Version) < 12 Then
        szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                    "Data Source=" & SourceFile & ";" & _
                    "Extended Properties=""Excel 8.0;HDR=Yes"";"
    Else
        szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                    "Data Source=" & SourceFile & ";" & _
                    "Extended Properties=""Excel 12.0;HDR=Yes"";"
    End If
End If

If SourceSheet = "" Then
    ' workbook level name
    szSQL = "SELECT * FROM " & SourceRange$ & ";"
Else
    ' worksheet level name or range
    szSQL = "SELECT * FROM [" & SourceSheet$ & "$" & SourceRange$ & "];"
End If

On Error GoTo SomethingWrong

Set rsCon = CreateObject("ADODB.Connection")
Set rsData = CreateObject("ADODB.Recordset")

rsCon.Open szConnect
rsData.Open szSQL, rsCon, 0, 1, 1

' Check to make sure we received data and copy the data
If Not rsData.EOF Then

    If Header = False Then
        TargetRange.Cells(1, 1).CopyFromRecordset rsData
    Else
        'Add the header cell in each column if the last argument is True
        If UseHeaderRow Then
            For lCount = 0 To rsData.Fields.Count - 1
                TargetRange.Cells(1, 1 + lCount).Value = _
                rsData.Fields(lCount).Name
            Next lCount
            TargetRange.Cells(2, 1).CopyFromRecordset rsData
        Else
            TargetRange.Cells(1, 1).CopyFromRecordset rsData
        End If
    End If

Else
    MsgBox "No records returned from : " & SourceFile, vbCritical
End If

' Clean up our Recordset object.
rsData.Close
Set rsData = Nothing
rsCon.Close
Set rsCon = Nothing
Exit Sub

SomethingWrong:
MsgBox "The file name, Sheet name or Range is invalid of : " & SourceFile, _
       vbExclamation, "Error"
On Error GoTo 0

End Sub 

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    您需要逐个单元格地遍历您收到的数据。粘贴数据后,使用循环遍历目标范围(Excel 工作表上的数据范围)。比如:

    for i = TargetRangeStartRow to numRowsInTargetRange
        for j = TargetRangeStartCol to numColsInTargetRange
            if Cells(i,j).formulaR1C1 = "" then
                Cells(i,j).formulaR1C1 = "/"
            end if
        next
    next
    

    显然您需要使用目标源中的第一行和第一列,并且您还需要获取目标范围内的行数和列数。我说使用目标范围是因为(我假设)是数据被粘贴到 Excel 中的范围。

    在 Excel 中没有太多方法(我不认为?)来查看访问权限并事先查看是否有任何数据丢失。无论如何,您仍然需要遍历整个内容,因此在将数据粘贴到 Excel 工作表后执行此操作也一样好。

    【讨论】:

    • 这看起来不错。但我的问题是我需要在添加每个新行后执行此循环。并且每次它都需要是表格的最后一行。我编辑代码并添加递归函数。我认为循环可能在这一行之后: R = R & myFile.Path & "|" .我需要检查已添加的最后一行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 2022-07-01
    • 2018-03-22
    • 1970-01-01
    • 2020-07-12
    相关资源
    最近更新 更多