循环查找VBA列中的字母

时间:2017-04-12 20:34:33

标签: excel vba excel-vba

我的宏是为某些客户发送电子邮件。只有在B列的某一行有X时才会发送电子邮件,然后通过创建一个附加到电子邮件客户端的新工作表将整行发送到客户端。 问题是我需要创建一个循环来计算包含字母X的所有列,因为这个宏只计算包含字母X的第一行,然后其他的不会转到新的表格,因此它没有发给客户。尽管有反击,我的宏观中的一切都还可以。当我运行它时,只有包含X的第一行转到客户端,而其他行则没有。你能帮助我吗?

代码:

LinhaInicio = 1
While Workbooks(planilha).Sheets("Boletas").Cells(LinhaInicio, 2) <> "X"
    LinhaInicio = LinhaInicio + 1
Wend
LinhaFim = LinhaInicio
While Workbooks(planilha).Sheets("Boletas").Cells(LinhaFim, 2) = "X"
    LinhaFim = LinhaFim + 1
Wend
LinhaFim = LinhaFim - 1
RangeInicio = Workbooks(planilha).Sheets("Boletas").Cells(LinhaInicio, 26).Address
RangeFim = Workbooks(planilha).Sheets("Boletas").Cells(LinhaFim, 34).Address
Set RangeCopiar = ActiveSheet.Range(RangeInicio, RangeFim)

3 个答案:

答案 0 :(得分:0)

也许尝试这样的事情:

Dim vArr As Variant
Dim LinhaFim As Long
Sub mainLoop()
    'put all values into variant array vArr
    vArr = Workbooks(planilha).Sheets("Boletas").UsedRange
    For i = LBound(vArr) To UBound(vArr)

        If vArr(i, 2) = "X" Then
            Nestedloops (i) 'initiate nested loops
            i = LinhaFim    'artificially increment counter
        End If

    Next i
End Sub


Sub Nestedloops(LinhaIncio As Long)
    With Workbooks(planilha).Sheets("Boletas")
        LinhaFim = linhaInicio
        While vArr(LinhaFim, 2) = "X"
            LinhaFim = LinhaFim + 1
        Wend
        LinhaFim = LinhaFim - 1
        RangeInicio = .Cells(LinhaInicio, 26).Address
        RangeFim = .Cells(LinhaFim, 34).Address
        Set RangeCopiar = ActiveSheet.Range(RangeInicio, RangeFim)

        'email code goes here

    End With
End Sub
蒂姆可能想出更好的东西,虽然可能想看看他想出的东西

答案 1 :(得分:0)

你想搜索X字母到列或行,抱歉我不明白你想说什么。您想创建一个循环来计算每一行和每列的所有X字母,还是只计算一列中的每一行?

答案 2 :(得分:0)

这是你如何定义一个数组:

Dim timelinessSheet, o As Variant
timelinessSheet = Worksheets("your_sheet_name").Range("A2:X" & Worksheets("your_sheet_name").Cells(Worksheets("your_sheet_name").Rows.Count, "B").End(xlUp).Row).Value

然后是你的代码

dim i as long
for i=2 to ubound (timelinessSheet,1)
 if  timelinessSheet(i,2)= X then 
   Your_condition 
 end if 
next I