嵌套循环Excel VBA

时间:2018-05-21 16:31:47

标签: excel vba excel-vba

在nexted VBA循环中我需要你的帮助。我有两列中的数据和行之间的空行。这个宏循环遍历一列,并查明它是否包含某些字符。如果它是空白,那么我希望它移动到下一行。如果它包含“Den”,则选择特定的工作表(“D-Temp”),否则选择(“M-Temp”)。 选择正确的工作表后,需要根据行号填充第二列数据的文本框。我到目前为止创建的代码是

Sub Template()

    Dim j As Long
    Dim c As Range, t As Range
    Dim ws As String

    j = 5
    With Sheets("Sample ")
        For Each c In .Range("I3", .Cells(.Rows.Count, "I").End(xlUp))
            If c.Value = "" Then
                Next ' `Not getting how to jump to next one`
            ElseIf c.Value = "DEN" Then
                ws = "D-Temp"
            Else
                ws = "M-Temp"
            End If

        For Each t In .Range("P3", .Cells(.Rows.Count, "P").End(xlUp))
            If t.Value <> "" Then
                j = j + 1
                Sheets("M-Temp").Copy after:=Sheets(Sheets.Count)
                ActiveSheet.Shapes("Textbox 1").TextFrame.Characters.Text = t.Value
                ActiveSheet.Shapes("textbox 2").TextFrame.Characters.Text = t.Offset(, -1).Value
            End If
        Next
    Next
    End With

任何帮助? 以下是我拥有的示例数据:

Type    Name 1  Name2
DEN     Suyi    Nick
                     'Blank row'
PX      Mac     Cruise

我想要Macro来识别Type&amp;选择模板工作表(D或M)并使用名称1和&amp ;;填充该模板上的文本框。名字2分别。

3 个答案:

答案 0 :(得分:1)

可能就在你之后:

Option Explicit

Sub Template()
    Dim c As Range

    With Sheets("Sample")
        For Each c In .Range("I3", .Cells(.Rows.Count, "I").End(xlUp)).SpecialCells(xlCellTypeConstants) ' loop through referenced sheet column C not empty cells form row 3 down to last not empty one
            Worksheets(IIf(c.Value = "DEN", "D-Temp", "M-Temp")).Copy after:=Sheets(Sheets.Count) ' create copy of proper template: it'll be the currently "active" sheet
            With ActiveSheet ' reference currently "active" sheet
                .Shapes("Textbox 1").TextFrame.Characters.Text = c.Offset(, 7).Value ' fill referenced sheet "TextBox 1" shape text with current cell (i.e. 'c') offset 7 columns (i.e. column "P") value
                .Shapes("Textbox 2").TextFrame.Characters.Text = c.Offset(, 6).Value ' fill referenced sheet "TextBox 2" shape text with current cell (i.e. 'c') offset 6 columns (i.e. column "O") value
            End With
        Next
    End With
End Sub

答案 1 :(得分:0)

如果我没有误解你当前的嵌套......

With Sheets("Sample ")
    For Each c In .Range("I3", .Cells(.Rows.Count, "I").End(xlUp))

        If c.Value <> "" Then
            If c.Value = "DEN" Then
                ws = "D-Temp"
            Else
                ws = "M-Temp"
            End If
            For Each t In .Range("P3", .Cells(.Rows.Count, "P").End(xlUp))
                If t.Value <> "" Then
                    j = j + 1
                    Sheets("M-Temp").Copy after:=Sheets(Sheets.Count)
                    ActiveSheet.Shapes("Textbox 1").TextFrame.Characters.Text = t.Value
                    ActiveSheet.Shapes("textbox 2").TextFrame.Characters.Text = t.Offset(, -1).Value
                End If
            Next
        End if 'not blank
    Next
End With

答案 2 :(得分:0)

如果我理解你的问题,你需要稍微改变你的if / then逻辑:

int ll_count = -1;    
            var caminho = Directory.GetCurrentDirectory();
            caminho += "\\telefones.txt";
            if (MessageBox.Show("Deseja mesmo deletar o numero?", "Usuario", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                string[] lines = File.ReadAllLines(caminho);
                using (StreamWriter writer = new StreamWriter(caminho))
                {

                    foreach (string line in lines)
                    {
                        ll_count++;

                        if (ll_count != listView1.SelectedIndices[0])
                        {
                            writer.WriteLine(line);
                        }
                    }
                }

                listView1.Items.RemoveAt(listView1.SelectedIndices[0]);

您可能希望添加代码以确保将ws设置为某些内容(并非所有列都为空)。

相关问题