VBA在循环中处理大型数据集的IF / THEN语句(在多个工作表上)

时间:2016-07-21 05:29:42

标签: vba excel-vba macros excel

我对VBA很新,并且正在学习处理大型(10年以上)数据集以转换为合适的分析格式。数据集位于52张不同的纸张上,每张纸上都有相同的布局格式(但不同的范围 - 代表不同的观察数量)。

这些数据与大象的目击有关。目前,我在A列中有所有大象ID代码,后续列是不同日期的观察结果,1表示个人存在。

我需要将所有这些1替换为个人的ID代码(即来自A列的相应行文本)。我已经为用户定义的范围设置了一个输入,它可以解决不同纸张的不同范围大小的问题。我坚持的观点是弄清楚哪个论点适用于?????在我的下面的代码中:什么参数返回第一列该行的文本?

[我最初记录了一个带有if,然后= A2的宏,然后拖动并填充到整个范围。但是,当然这不适用于其他表格。

到目前为止我所拥有的是什么;

Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf 

The following groups are read: mysql client
The following options may be given as the first argument:
--print-defaults        Print the program argument list and exit.
--no-defaults           Don't read default options from any option file,
                        except for login file.
--defaults-file=#       Only read default options from the given file #.
--defaults-extra-file=# Read this file after the global files are read.
--defaults-group-suffix=#
                        Also read groups with concat(group, suffix)
--login-path=#          Read this path from the login file.

当然,任何其他可行的逻辑方法也会受到欢迎。

3 个答案:

答案 0 :(得分:0)

Array("Elephants", "Elephants (2)")中填入您要处理的工作表的名称。

Sub FillElephantIDs()
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    Dim lastColumn As Integer, lastRow As Long, x As Long
    Dim ws

    For Each ws In Array("Elephants", "Elephants (2)")
        With Worksheets(ws)
            lastColumn = .Cells(1, Columns.count).End(xlToLeft).Column
            lastRow = .Range("A" & Rows.count).End(xlUp).Row

            For x = 2 To lastRow

                .Range(.Cells(x, 2), .Cells(x, lastColumn)).Replace 1, .Cells(x, 1).Value

            Next

        End With

    Next

    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
End Sub

答案 1 :(得分:0)

您可以使用您正在查看的单元格的行号和A列中的列号来引用ID代码 - 所以缺少代码?????是

rng.Value = Cells(rng.Row, 1).Value

其中rng.Row是您正在查看的单元格(rng)的行索引," 1"是A栏。

'Next rng'

之后还需要'End If'

或者,您可以将InputRng设置为工作表上的已使用范围,这将仅对工作表上的所有已用单元格执行操作 - 请参阅第二个代码示例(保存您必须选择范围)。

希望这对你有用(这是我的第一个回答'永远 - 我以为是时候开始回馈了 - 所以随时发现问题或错误)。干杯,克莱尔

Sub ReplaceOneswithIDcode()
'
' replace all ones with ID code.
'
Dim rng As Range
Dim InputRng As Range

On Error GoTo Handler:
Set InputRng = Application.InputBox("Range :", xTitleId, Default:=Selection.Address, Type:=8)
For Each rng In InputRng
If rng.Value = 1 Then
     rng.Value = Cells(rng.Row, 1).Value
End If
Next rng
Handler:
End Sub

Sub ReplaceOneswithIDcode_usedrange()
'
' replace all ones with ID code in used range.
'
Dim rng As Range
Dim InputRng As Range

On Error GoTo Handler:
Set InputRng = ActiveSheet.UsedRange

For Each rng In InputRng
If rng.Value = 1 Then
     rng.Value = Cells(rng.Row, 1).Value
End If
Next rng
Handler:
End Sub

答案 2 :(得分:0)

你可以不经迭代地完成,如下所示

Sub ReplaceOneswithIDcode(ws As Worksheet)
    With ws
        With Intersect(.UsedRange, .Columns("B").Resize(, .UsedRange.Columns.Count - 1))
            .Replace what:=1, replacement:="=OFFSET($A$1,ROW()-1,0)", lookat:=xlWhole
            .Value = .Value
        End With
    End With
End Sub

您可以使用如下:

Option Explicit

Sub main()
    Dim ws As Worksheet

    For Each ws In ThisWorkbook.Worksheets '<--| loop through all worksheets of the workbook the macro resides in
        ReplaceOneswithIDcode ws
    Next ws
End Sub

或以任何其他方式循环工作表集合