[英]Set Range to Omit First Row in Excel VBA
The code (below) combines all the columns from an active sheet converts them to one Column in a sheet named Sheet3(Masterlist). 下面的代码将活动工作表中的所有列组合在一起,将它们转换为名为Sheet3(Masterlist)的工作表中的一列。
I need the to start at row 2 when all the columns are combined. 当所有列合并时,我需要从第2行开始。 This is because row 1 has the column name.
这是因为第1行具有列名。
Also, I would rather use Sheet1(Orders) not the Active Sheet. 另外,我宁愿使用Sheet1(Orders)而不是Active Sheet。
This code is modified as suggested by Yaegz. 该代码已根据Yaegz的建议进行了修改。 I am now getting Next without For on Line 26:
我现在在第26行上获得没有For的Next:
Sub ToArrayAndBack()
Dim arr As Variant, lLoop1 As Long, lLoop2 As Long
Dim arr2 As Variant, lIndex As Long
'turn off updates to speed up code execution
With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
.DisplayAlerts = False
End With
ReDim arr2(Sheet1.UsedRange.Cells.Count - Sheet1.UsedRange.SpecialCells(xlCellTypeBlanks).Count)
arr = Sheet1.UsedRange.Value
Set myRange = Worksheets("Orders").Range("A1:A" & _
Worksheets("Orders").Cells(Worksheets("Orders").Rows.Count, 1).End(xlUp).Row)
i = 2
Do While i <= myRange.Rows.Count
For lLoop1 = LBound(arr, 1) To UBound(arr, 1)
For lLoop2 = LBound(arr, 2) To UBound(arr, 2)
If Len(Trim(arr(lLoop1, lLoop2))) > 0 Then
arr2(lIndex) = arr(lLoop1, lLoop2)
lIndex = lIndex + 1
End If
Next
Next
i = i + i
Loop
Dim ws As Worksheet
Dim found As Boolean
found = False
For Each ws In ThisWorkbook.Sheets
If ws.Name = "MasterList" Then
found = True
Exit For
End If
Next
If Not found Then
Sheets.Add.Name = "MasterList"
End If
Set ws = ThisWorkbook.Sheets("MasterList")
With ws
.Range("A1").Resize(, lIndex + 1).Value = arr2
.Range("A1").Resize(, lIndex + 1).Copy
.Range("A2").Resize(lIndex + 1).PasteSpecial Transpose:=True
.Rows(1).Delete
End With
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
.DisplayAlerts = True
End With
End Sub
Sheet1 is the sheet code. Sheet1是工作表代码。 Orders is the sheet name.
订单是工作表名称。
If you want to point to a specific sheet instead of the active one use: Application.Worksheets("name of your sheet")
instead of ActiveSheet
如果要指向特定的工作表而不是活动工作表,请使用:
Application.Worksheets("name of your sheet")
代替ActiveSheet
The error message is because the do while
opens a loop and it closes with the loop
key word something like: 该错误消息是因为
do while
打开了一个循环,并且使用loop
关键字关闭了它,例如:
Do while i<10
Your Code
i=i+1
Loop
Assuming your code works, if you want a correct Do While loop then use the following code. 假设您的代码有效,如果您想要正确的Do While循环,请使用以下代码。 It sounds like you are trying to just initialize where your code should start.
听起来您正在尝试仅初始化代码应从何处开始。 If that is the case then a Do While loop is not the way to go.
如果真是这样,那么“ Do While”循环就不可行了。
arr = ActiveSheet.UsedRange.Value
Set myRange = Worksheets("Sheet1").Range("A1:A" & _
Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, 1).End(xlUp).Row)
i = 2
Do While i <= myRange.Rows.Count
For lLoop1 = LBound(arr, 1) To UBound(arr, 1)
For lLoop2 = LBound(arr, 2) To UBound(arr, 2)
If Len(Trim(arr(lLoop1, lLoop2))) > 0 Then
arr2(lIndex) = arr(lLoop1, lLoop2)
lIndex = lIndex + 1
End If
Next
Next
i = i + i
Loop
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.