首先,我想自我介绍一下。 Iam Miguel和我最近开始学习VBA以改善和节省工作时间。我熟悉所有类型的公式,但是当转向VBA时,我有时会卡住。
我试图从Sheet“Aux”循环范围A2:A355并将每个值复制到工作表“CS”,并且每个值都应粘贴在A列:A中,但是在B2范围内给出的偏移量为:B355表“Aux”。例如,我给出了附例。 示例代码:
这是代码:
Sub cablexsection()
Dim s As Integer
Dim smax As Integer
smax = Sheets("Aux").Range("b1").Value
Sheets("CS").Activate
For s = 3 To smax
Sheets("CS").Cells(s, 1).Value = Sheets("Aux").Cells(s, 1).Value
'here I have to set the offset to down in order to paste cells given Sheets("Aux").Cells(s, 2) values
Next s
End Sub
在该链接下,您可以找到要处理的文件:
非常感谢,如果这个问题再次出现,我很抱歉。我试图浏览论坛,但也许我不知道该写些什么。
答案 0 :(得分:0)
试试这个
Option Explicit
Sub CableXsection()
Dim wsAux As Worksheet, wsCS As Worksheet
Dim s As Long, sMax As Long, offSetCell As Range
Set wsAux = ThisWorkbook.Worksheets("Aux")
Set wsCS = ThisWorkbook.Worksheets("CS")
sMax = wsAux.Range("B1").Value
Application.ScreenUpdating = False
For s = 3 To sMax
Set offSetCell = wsAux.Cells(s, 2) '2 is the offset column from the same row
If Not IsError(offSetCell) And IsNumeric(offSetCell) Then
wsCS.Cells(offSetCell.Value2 + s, 1).Value = wsAux.Cells(s, 1).Value
End If
Next
Application.ScreenUpdating = True
End Sub