在Excel范围中使用变量

时间:2014-11-07 00:21:08

标签: excel vba excel-vba csv

提前道歉我的问题的不明确性质。我绝对是VBA业余爱好者

我有一个相当长的宏,基本上打开一个csv,重新格式化一些行并保存为csv。

宏查找这样的列标题:

ValArray(1) = .Match("CH4", RawWs.Range("a1:iv1"), 0)

现在我公司的工作人员正在删除前导行,以便标题字段位于第1行。相反,我想查找标题开头的位置。标题始终以单词" ID"开头。在A栏中。

所以我测试了这段代码,找到了标题开头的行:

Sub findRowOne()
Dim SearchRange As Range
Dim FindRow As Range
Set SearchRange = Range("A1", Range("A65536").End(xlUp))
Set FindRow = SearchRange.Find("ID", LookIn:=xlValues, lookat:=xlWhole)
MsgBox FindRow.Row
End Sub

我试图获取此代码生成的数字,并将其添加到第一行代码中,如下所示:

 ValArray(1) = .Match("CH4", RawWs.Range("a" & FindRow.Row & ":" & "iv" & FindRow.Row), 0)

它似乎不适用于我更大的代码:

Sub gasExtraction()

Dim RawWbName As String
Dim RawWb As Workbook
Dim RawWs As Worksheet
Dim NewWb As Workbook
Dim NewWs As Worksheet
Dim ValArray(1 To 14) As Long
Dim Cel As Range
Dim r As Range
Dim DateTime As Date
Dim SearchRange As Range
Dim FindRow As Range
Dim firstRow As String




RawWbName = Application.GetOpenFilename("CSV Files (*.csv), *.csv")

Workbooks.Open RawWbName, local:=True
Set RawWb = ActiveWorkbook
Set RawWs = ActiveSheet
Set NewWb = Workbooks.Add
Set NewWs = ActiveSheet
Set SearchRange = Range("A1", Range("A65536").End(xlUp))
Set FindRow = SearchRange.Find("ID", LookIn:=xlValues, lookat:=xlWhole)

NewWs.Cells(1, 1) = RawWs.Cells(1, 1)


With Application.WorksheetFunction
    ValArray(1) = .Match("CH4", RawWs.Range("a" & FindRow.Row & ":" & "iv" & FindRow.Row), 0) 'change label
    ValArray(2) = .Match("CO2", RawWs.Range("a1:iv1"), 0) 'change label
    ValArray(3) = .Match("O2", RawWs.Range("a1:iv1"), 0) 'change label
    ValArray(4) = .Match("BALANCE", RawWs.Range("a1:iv1"), 0) 'change label
    ValArray(5) = .Match("CO", RawWs.Range("a1:iv1"), 0) 'change label
    ValArray(6) = .Match("INI-SP", RawWs.Range("a1:iv1"), 0) 'change label
    ValArray(7) = .Match("INI-DP", RawWs.Range("a1:iv1"), 0) 'change label
    ValArray(8) = .Match("INI-FLOW", RawWs.Range("a1:iv1"), 0) 'change label
    ValArray(9) = .Match("INI-POWER", RawWs.Range("a1:iv1"), 0) 'change label
    ValArray(10) = .Match("BARO", RawWs.Range("a1:iv1"), 0) 'change label
    ValArray(11) = .Match("ANSWER 1", RawWs.Range("a1:iv1"), 0) 'change label
    ValArray(12) = .Match("ANSWER 2", RawWs.Range("a1:iv1"), 0) 'change label
    ValArray(13) = .Match("INI-TEMP", RawWs.Range("a1:iv1"), 0) 'change label
    ValArray(14) = .Match("CHOSEN 1", RawWs.Range("a1:iv1"), 0) 'change label

End With

'do ID
RawWs.Range("a2:a65536").Copy
NewWs.Range("a2").Select
NewWs.Paste
Range("a1").Select
ActiveCell.FormulaR1C1 = "01 Asset ID"

'do DateTime
RawWs.Range("b2:b65536").Copy
NewWs.Range("b2").Select
NewWs.Paste
Columns("B:B").Select
Selection.NumberFormat = "dd-mm-yyyy h:mm"
Range("b1").Select
ActiveCell.FormulaR1C1 = "02 Date/Time"

'do Value1
RawWb.Activate
Range(RawWs.Cells(2, ValArray(1)), RawWs.Cells(65536, ValArray(1))).Select
Selection.Copy
NewWb.Activate
NewWs.Range("c2").Select
NewWs.Paste
Columns("C:C").Select
Selection.NumberFormat = "0.0"
Range("c1").Select
ActiveCell.FormulaR1C1 = "03 Methane"

'do Value2
Range(RawWs.Cells(2, ValArray(2)), RawWs.Cells(65536, ValArray(2))).Copy
NewWs.Range("d2").Select
NewWs.Paste
Columns("d:d").Select
Selection.NumberFormat = "0.0"
Range("d1").Select
ActiveCell.FormulaR1C1 = "04 Carbon Dioxide"

'do Value3
Range(RawWs.Cells(2, ValArray(3)), RawWs.Cells(65536, ValArray(3))).Copy
NewWs.Range("e2").Select
NewWs.Paste
Columns("e:e").Select
Selection.NumberFormat = "0.0"
Range("e1").Select
ActiveCell.FormulaR1C1 = "05 Oxygen"

'do Value4
Range(RawWs.Cells(2, ValArray(4)), RawWs.Cells(65536, ValArray(4))).Copy
NewWs.Range("f2").Select
NewWs.Paste
Set r = Intersect(NewWs.Range("f3:f65536"), NewWs.UsedRange)
If Not r Is Nothing Then
    For Each Cel In r.Cells
        If Cel < 0 Then
            Cel.Value = 0
        End If
    Next Cel
    End If
Columns("f:f").Select
Selection.NumberFormat = "0.0"
Range("f1").Select
ActiveCell.FormulaR1C1 = "06 Balance Gas"

'do Value5
Range(RawWs.Cells(2, ValArray(5)), RawWs.Cells(65536, ValArray(5))).Copy
NewWs.Range("g2").Select
NewWs.Paste
Range("g1").Select
ActiveCell.FormulaR1C1 = "07 Carbon Monoxide"

'do Value6
Range(RawWs.Cells(2, ValArray(6)), RawWs.Cells(65536, ValArray(6))).Copy
NewWs.Range("h2").Select
NewWs.Paste
Range("h1").Select
ActiveCell.FormulaR1C1 = "08 Pressure"

'do Value7
Range(RawWs.Cells(2, ValArray(7)), RawWs.Cells(65536, ValArray(7))).Copy
NewWs.Range("i2").Select
NewWs.Paste
Columns("i:i").Select
Selection.NumberFormat = "0.00"
Range("i1").Select
ActiveCell.FormulaR1C1 = "09 Diff Pressure"

'do Value8
Range(RawWs.Cells(2, ValArray(8)), RawWs.Cells(65536, ValArray(8))).Copy
NewWs.Range("j2").Select
NewWs.Paste
Columns("j:j").Select
Selection.NumberFormat = "0.0"
Range("j1").Select
ActiveCell.FormulaR1C1 = "10 Flow"

'do Value9
Range(RawWs.Cells(2, ValArray(9)), RawWs.Cells(65536, ValArray(9))).Copy
NewWs.Range("k2").Select
NewWs.Paste
Columns("k:k").Select
Selection.NumberFormat = "0.0"
Range("k1").Select
ActiveCell.FormulaR1C1 = "11 Energy"

'do Value10
Range(RawWs.Cells(2, ValArray(10)), RawWs.Cells(65536, ValArray(10))).Copy
NewWs.Range("l2").Select
NewWs.Paste
Range("l1").Select
ActiveCell.FormulaR1C1 = "12 Atmospheric Pressure"

'do Value11
Range(RawWs.Cells(2, ValArray(11)), RawWs.Cells(65536, ValArray(11))).Copy
NewWs.Range("m2").Select
NewWs.Paste
Range("m1").Select
ActiveCell.FormulaR1C1 = "13 Valve Arrive"

'do Value11
Range(RawWs.Cells(2, ValArray(12)), RawWs.Cells(65536, ValArray(12))).Copy
NewWs.Range("n2").Select
NewWs.Paste
Range("n1").Select
ActiveCell.FormulaR1C1 = "14 Valve Depart"

'do Value12
Range(RawWs.Cells(2, ValArray(13)), RawWs.Cells(65536, ValArray(13))).Copy
NewWs.Range("o2").Select
NewWs.Paste
Range("o1").Select
ActiveCell.FormulaR1C1 = "15 Temp"

'do Value13
Range(RawWs.Cells(2, ValArray(14)), RawWs.Cells(65536, ValArray(14))).Copy
NewWs.Range("p2").Select
NewWs.Paste
Range("p1").Select
ActiveCell.FormulaR1C1 = "16 Comment"

Rows("2:2").Select
Selection.Delete Shift:=xlUp

NewWb.SaveAs Filename:=RawWb.Path & "\Land_Gas Extraction " & RawWb.Name, FileFormat:=xlCSV
' NewWb.Close


RawWb.Close

End Sub

对不起,我觉得这个解释有点分散。

1 个答案:

答案 0 :(得分:0)

它不起作用的原因是FindRowNothing

当行

Set FindRow = SearchRange.Find("ID", LookIn:=xlValues, lookat:=xlWhole)
     

运行,NewWb仍处于活动状态,因此返回Nothing

如果用以下内容替换代码的第一行,它应该有效:

RawWbName = Application.GetOpenFilename("CSV Files (*.csv), *.csv")

Workbooks.Open RawWbName, local:=True
Set RawWb = ActiveWorkbook
Set RawWs = ActiveSheet
Set NewWb = Workbooks.Add
Set NewWs = ActiveSheet
RawWb.Activate
With RawWb.Sheets(RawWs.Name)
    Set SearchRange = .Range("A1", Range("A65536").End(xlUp))
    Set FindRow = SearchRange.Find("ID", LookIn:=xlValues, lookat:=xlWhole)
End With
NewWb.Sheets(NewWs.Name).Cells(1, 1) = RawWs.Cells(1, 1)