如果Value小于X,则在Excel中删除行

时间:2017-08-14 13:10:26

标签: excel vba excel-vba

如果它符合B列中的值小于100的条件,我试图删除excel中的一行。我看到this并尝试按照它进行操作,但是我是不是那么了解VBA,所以我不知道该往哪里去。

修改 这是我迄今为止尝试过的代码,它不起作用。

Sub DeleteR()
 Dim myRow As Range
 Dim toDelete As Range

 For i = 2 To 5000
    If Worksheets("PalCountPLTZR").Cells(i, 2) < "99" Then
    Set myRow = Worksheets("PalCountPLTZR").Rows(i)
    If toDelete Is Nothing Then
        Set toDelete = myRow
    Else
        Set toDelete = Union(toDelete, myRow)
    End If
End If
Next i

End Sub

我不确定为什么它不起作用,但我想知道它所看到的细胞是否来自查询。

3 个答案:

答案 0 :(得分:2)

您可以使用此循环检查和删除列&#34; A&#34;:

中的行
Sub DeleteRows()
    'get last row in column A
    Last = Cells(Rows.Count, "A").End(xlUp).Row
    For i = Last To 1 Step -1
        'if cell value is less than 100
        If (Cells(i, "A").Value) < 100 Then
            'delete entire row
            Cells(i, "A").EntireRow.Delete
        End If
    Next i
End Sub

答案 1 :(得分:0)

Ed在你链接的帖子中向你展示了.Delete部分:

  

Chris Nielsen的解决方案很简单,效果很好。一点点   更短的选择是......

     

ws.Rows(RAND).Delete

除了这些知识之外,您还需要添加一个if语句来显示要删除的条件,例如:

If Cells(i,2)="x" Then
    Rows(i).Delete
    End If

这种方法会为i使用循环,所以你需要阅读它。

祝你好运,并且在未来,将你已经尝试过的代码,特别是如果你已经在网上找到其他代码,我们可以尝试帮助解决问题

答案 2 :(得分:0)

两件事,

public class ScoutGUI extends javax.swing.JFrame { /** * Creates new form ScoutGUI */ List<String> strings = Arrays.asList("Do you mind Clutter in Room?", "Do you mind alarm clocks?","Do you mind loud visitors?","Can you sleep with lights on?","Do you mind noise past Midnight?", "Do you consider yourself as an introvert?", "Do you consider yourself as an extrovert?","Do you like to go to parties?","Do you drink alcoholic beverages?(21+)", "DONE!"); ArrayList<Student> obj = new ArrayList<>(); String name , email , gender , major , year , language , building ; int id , i; public ScoutGUI() { initComponents(); } public ScoutGUI(int a) { i = a; initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { panel1 = new java.awt.Panel(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setFont(new java.awt.Font("Courier", 0, 13)); // NOI18N jButton1.setText("NO"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setFont(new java.awt.Font("Courier", 0, 13)); // NOI18N jButton2.setText("YES"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jLabel1.setFont(new java.awt.Font("Courier New", 1, 18)); // NOI18N jLabel1.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(204, 255, 153))); javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(panel1); panel1.setLayout(panel1Layout); panel1Layout.setHorizontalGroup( panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panel1Layout.createSequentialGroup() .addGap(71, 71, 71) .addComponent(jButton1) .addGap(94, 94, 94) .addComponent(jButton2) .addContainerGap(129, Short.MAX_VALUE)) .addGroup(panel1Layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) ); panel1Layout.setVerticalGroup( panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panel1Layout.createSequentialGroup() .addGap(26, 26, 26) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1) .addComponent(jButton2)) .addContainerGap(68, Short.MAX_VALUE)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(panel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(panel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); pack(); }// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: //skip and never buttonpressActionPerformed(); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: //match and update buttonpressActionPerformed(); } private void buttonpressActionPerformed() { // int i = 0; Iterator<String> iterator = strings.iterator(); //for (String strin : strings) while (iterator.hasNext()) { //if (jButton2.isSelected() || jButton1.isSelected()) //{ jLabel1.setText(iterator.next()); //} } } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(ScoutGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(ScoutGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(ScoutGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(ScoutGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new ScoutGUI().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private java.awt.Panel panel1; // End of variables declaration } 应该只是"99"

你最后不会删除。

99