简体   繁体   English

如果特定列名称下的单元格为空,如何删除整行的空白?

[英]How can I delete empty the entire row if the cell under a specific column name is empty?

I need to import several xml-files into Excel and I need to align the column names for all of the xml-tables that are pasted in Excel. 我需要将几个xml文件导入Excel,并且需要对齐Excel中粘贴的所有xml表的列名。 The problem is that some of the xml-files include one additional column, which I do not need. 问题是某些xml文件还包含一列,我不需要。 This column pops up in the middle of the table, and therefor makes it difficult to work with the data. 该列会在表格的中间弹出,因此很难处理数据。 In addition, I need to remove the entire row if the cells under the name "content" is blank. 此外,如果名称为“ content”的单元格为空,则需要删除整行。

So far I have managed to make a command button which imports the xml-files to Excel (see code). 到目前为止,我已经设法制作了一个命令按钮,该按钮将xml文件导入到Excel中(参见代码)。

Sub CommandButton1_Click()    
    Dim xWb As Workbook
    Dim xSWb As Workbook
    Dim xStrPath As String
    Dim xFileDialog As FileDialog
    Dim xFile As String
    Dim xCount As Long
    On Error GoTo ErrHandler
    Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
    xFileDialog.AllowMultiSelect = False
    xFileDialog.Title = "Select a folder [Kutools for Excel]"
    If xFileDialog.Show = -1 Then
        xStrPath = xFileDialog.SelectedItems(1)
    End If
    If xStrPath = "" Then Exit Sub
    Application.ScreenUpdating = False
    Set xSWb = ThisWorkbook
    xCount = 1
    xFile = Dir(xStrPath & "\*.xml")
    Do While xFile <> ""
        Set xWb = Workbooks.OpenXML(xStrPath & "\" & xFile)
        xWb.Sheets(1).UsedRange.Copy xSWb.Sheets(1).Cells(xCount, 1)
        xWb.Close False
        xCount = xSWb.Sheets(1).UsedRange.Rows.Count + 2
        xFile = Dir()
    Loop
    Application.ScreenUpdating = True
    xSWb.Save
    Exit Sub

ErrHandler:
    MsgBox "no files xml", , "Kutools for Excel"
End Sub

I hope to get 30 tables from 30 xml-files, with similar headers in the same columns. 我希望从30个xml文件中获得30个表,并且在同一列中具有相似的标题。 In addition, I need to remove the empty cells i referred to since the data sets are somewhat huge. 另外,由于数据集有些庞大,因此我需要删除我引用的空单元格。

Try to seek for that additional column using for example Instr() in the header row of opened workbook - only if You know the header name of this column. 仅在您知道此列的标题名称的情况下,才尝试使用打开的工作簿的标题行中的Instr()寻找该附加列。 Do this in xWb workbook and after corrections copy paste to your main workbook. 在xWb工作簿中执行此操作,并在更正后将粘贴复制到您的主工作簿中。 Referring to remove empty cells action, You can do this after pasting to main file. 关于删除空单元格操作,您可以在粘贴到主文件后执行此操作。 Just do the simple if() instruction checking if the cell in specified column is blank - "" (double quotes). 只需执行简单的if()指令,即可检查指定列中的单元格是否为空-“”(双引号)。 You can put this if() in loop from 2 to last row - that last row You can 'measure' in xWb using xWb.cells(rows.count,1).end(xlup).Row. 您可以将此if()从2循环到最后一行-最后一行您可以使用xWb.cells(rows.count,1).end(xlup).Row在xWb中“测量”。 Give me a sign if You managed to do anything. 如果你能做任何事,给我个手势。

This may help you: 这可以帮助您:

Option Explicit

Sub test()

    Dim ColumnName As String
    Dim LastColumn As Long, LastRow As Long
    Dim rngFoundContent As Range
    Dim rngFoundColumn As Range

    'Change sheet name if needed
    With ThisWorkbook.Worksheets("Sheet1")

        'Search for the word "Content"
        Set rngFoundContent = .UsedRange.Find("Content", LookIn:=xlValues)
        'If "Content" found
        If Not rngFoundContent Is Nothing Then
            'Delete the row below
            .Rows(rngFoundContent.Row + 1).EntireRow.Delete
        End If

        'Search for the Column Name (Lest us assume that column name is "Test"0
        ColumnName = "Test"
        'Let us assume that Headers appear in row 1, find the last column of row 1
        LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
        'Search for the word column Test
        Set rngFoundColumn = .Range(.Cells(1, 1), .Cells(1, LastColumn)).Find(ColumnName, LookIn:=xlValues)
        'If column found
        If Not rngFoundColumn Is Nothing Then
            'Delete whole column
            .Columns(rngFoundColumn.Column).EntireColumn.Delete
        End If

    End With

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果 D 列中的单元格为空,则 Excel VBA 删除整行 - Excel VBA delete entire row if cell in column D is empty 如何删除包含文本和数字的Excel工作表的任意行中具有空单元格的整行 - How to delete entire row which has empty cell in any column, of a excel sheet containing both texts and numbers 如果特定列中的单元格有数据,如何告诉 Excel 宏删除整行? - How do I tell an excel macro to delete the entire row if cell in a specific column have data? Excel删除第一个单元格为空的整行 - Excel delete entire row where is first cell empty 如何查找一行中第一个空单元格的列名? - How to find the column name of first empty cell in a row? 遍历列,当找到空单元格时,给整行着色? - Loop through column and when empty cell is found, color entire row? 如果特定范围内的单元格全部为空,则删除整行 - Delete entire row if cells in specific range are all empty 如果列为空,则删除行 - Delete a Row if a column is Empty 如何在一个范围内的列中找到第一个空单元格,并在后续公式中使用行引用? - How can I find the first empty cell in a column within a range, and use the row-reference in subsequent formulas? 如何使用Java从Excel工作表中获取特定列名(作为参数传递)的最后一个非空单元格的行索引? - How to get the row index of last non-empty cell for a specific column name(passed as an argument) from an excel sheet using java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM