使用范围对象作为循环的一部分

时间:2015-05-20 16:14:10

标签: excel excel-vba vba

我粘贴了下面的整个宏,但这是重要的部分。

var connection = $.hubConnection();
connection.url = "http://domain/signalr";

// Declare a proxy to reference the hub. 
var applicationHub = connection.createHubProxy('ApplicationHub');

//mock is just a dynamic type that recieves the message in the join group hub method
applicationHub.on('mock', function (msg)
{
  alert(msg);
});

它按原样工作,除了它创建不必要的数据,因为我不知道如何在范围对象中使用变量名。我的范围目前是硬编码的,例如(“A1:A1000”),当我希望它是类似的东西(“A1:A& LastRow)。

此外,我必须明确地调出要复制的列名,因为范围不会接受变量名称(“currentColumn& 1:currentColumn& LastRow”)。

有没有办法使用变量名作为范围对象的一部分,以便我们可以在循环中使用它们?

Range("B2:B10000").Value = Range("B2").Offset(-1, 1).Value    
Range("D2:D10000").Value = Range("D2").Offset(-1, 1).Value    
Range("F2:F10000").Value = Range("F2").Offset(-1, 1).Value    
Range("H2:H10000").Value = Range("H2").Offset(-1, 1).Value

End Sub

4 个答案:

答案 0 :(得分:3)

类似的东西:

Dim LastRow As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Range("B2:B" & LastRow).Value = Range("B2").Offset(-1, 1).Value
Range("D2:D" & LastRow).Value = Range("D2").Offset(-1, 1).Value
Range("F2:F" & LastRow).Value = Range("F2").Offset(-1, 1).Value
Range("H2:H" & LastRow).Value = Range("H2").Offset(-1, 1).Value

答案 1 :(得分:3)

假设您在此处添加的工作表中运行代码:

'copy the worksheet and rename it before editing
Sheets(1).Copy After:=Sheets(1)
ActiveSheet.Name = MySheetName

还不确定此代码的用途是什么,但仍然将其用于示例

Range("B2:B10000").Value = Range("B2").Offset(-1, 1).Value

试试这个:

Dim lLastRow As Long
lLastRow = wbk.Worksheets(MySheetName).UsedRange.SpecialCells(xlLastCell).Row

Rem This updates only columns B, D, F & H - adjust as needed
For colx = 2 To 8 Step 2
    With wbk.Worksheets(MySheetName)
        Rem Creates Range as Range(Cells(rIni,cIini), Cells(rEnd,cEnd))
        rem Corresponding code for "Range("B2:B10000").Value = Range("B2").Offset(-1, 1).Value" (see comment above)
        Range(.Cells(2, colx), .Cells(lLastRow, colx)) = .Cells(2, colx).Offset(-1, 1).Value
End With: Next

答案 2 :(得分:1)

虽然这个答案不适用于你的情况,但我觉得这可以帮助回答你在那里遇到的一些问题。

指定范围时,可以分隔列(字母)和行(数字)并使用自己的变量。 在for循环中,这看起来像

for i = 1 to 100
     Range("A" & i).Value = Range("A"&i).Offset(, 1).Value
next

您还可以使用以下方法确定所选单元格的行数:

dim RowNb as long
RowNb = (ActiveCell.Row)

这也适用于列,并且可以像我在开头提到的循环一样使用。

答案 3 :(得分:1)

在您的描述中缺少一个显而易见的事情是在工作表中提及数据的性质。您曾简要提到A1,但您的范围值分配从第2行开始,因此可以推断第1行包含列标题标签。

Sub prepareWorkbook()
    Dim wbk As Workbook, wks As Worksheet
    Dim colx As Long
    Dim lc As Long, lr As Long
    Dim MySheetName As String

    Set wbk = ThisWorkbook    'no idea what this does
    Set wks = wbk.ActiveSheet 'no idea what this does
    MySheetName = "Import"

    'no idea what this does or what sht is
    'LastRow = sht.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row

    'copy the worksheet and rename it before editing
    Sheets(1).Copy After:=Sheets(1)

    With Sheets(2)
        .Name = MySheetName

        If CBool(Application.CountIf(.Rows(1), "PartNumber")) Then
            colx = Application.Match("PartNumber", .Rows(1), 0)
        Else
            colx = .Range(Application.InputBox("Enter Letter of Id column") & 1).Column
        End If
        If .Columns(colx).Column > 1 Then
            'cut Id column from current location and insert it at column index 1
            .Columns(colx).Cut
            .Columns(1).Insert Shift:=xlToRight
        End If

        'quickest way to trim trailing spaces is with Text-to-Columns, Fixed Width
        With .Columns(1)
            .TextToColumns Destination:=.Cells(1), DataType:=xlFixedWidth, FieldInfo:=Array(0, 1)
        End With

        ' insert column every other column (working backwards toward A1)
        For lc = .Cells(1, Columns.Count).End(xlToLeft).Column To 2 Step -1
            .Columns(lc).Insert Shift:=xlToRight
        Next lc

        For lc = (.Cells(1, Columns.Count).End(xlToLeft).Column - 1) To 2 Step -2
            'let's put the row-by-row value in instead of a single value into all cells
            lr = .Cells(Rows.Count, lc + 1).End(xlUp).Row
            With .Cells(2, lc).Resize(lr - 1, 1)
                .Cells = .Offset(-1, 1).Value
                .EntireColumn.AutoFit
            End With
        Next lc

    End With

    Set wbk = Nothing
    Set wks = Nothing

End Sub

解释为代码中的注释。