2014-06-18 1 views
0

다섯 번째 워크 시트의 열 F가 3이면 3의 왼쪽에 값을 붙여 넣습니다 (열 A에서 E까지) 다른 시트 (시트 1)에 넣습니다. 나는 시작할 수없는 것 같습니다. 매크로를 실행할 때 아무 일도 일어나지 않습니다. 나는 바보 같은 실수를 저질렀다.X = True 일 경우 하나의 워크 시트에서 다른 값 붙여 넣기

미리 도움 주셔서 감사합니다.

Sub Movevalues() 
     Dim q As Integer, w As Integer 
    w = 7 
    For q = 1 To 1000 
     If ActiveWorkbook.Worksheets("Taxable Accounts Import").Cells(q, 6).Value = 3 Then 
      Range(Cells(q, 1), Cells(q, 5)).Select 
      Selection.Copy 
      Worksheets(1).Select 
      Range(22, w).Select 
      Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
       :=False, Transpose:=False 
      Worksheets(5).Select 
      w = w + 1 
     End If 
    Next q 
End Sub 
+1

빠른 질문 : 'Worksheets ("과세 계정 가져 오기")'와 같은가요? –

답변

1

여기에 문제가 있다고 생각합니다. Worksheets(5)에서 연속으로 5 개의 셀을 복사하지만 각 루프마다 1 씩 증가하여 w입니다. 목표는 정말 Worksheets(1)에 동일한 행에 추가 할 경우, 당신은 좋은, 쉽게 수정 하하이다 ... 대신 5 w를 증가해야합니다 :

말했다되고 그건
w = w + 5 

, 당신은 반복하고 1000 번입니다. 즉, 잠재적으로 1,000 개의 일치 항목이 있으며 1000 개의 항목이 채워집니다 (내 by-5-correction이 정확하다면 열). 그게 많아! 귀하의 의도가 행 7, 열 22에서 시작하여 거기에서 행을 증가시키는 것이라면 다음 전략을 사용할 수 있습니다. (크게 무슨 일이 일어 났는지 설명하는 데 댓글을 달았습니다 ...)

Option Explicit 
Sub MoveValuesRev2() 

Dim q As Long, w As Long 
Dim TAI As Worksheet, SheetOne As Worksheet, _ 
    SheetFive As Worksheet 
Dim Source As Range, Target As Range 

'set references up-front 
w = 7 
Set TAI = ThisWorkbook.Worksheets("Taxable Accounts Import") 
Set SheetOne = ThisWorkbook.Worksheets(1) 
Set SheetFive = ThisWorkbook.Worksheets(5) 

'loop through the cells in question 
For q = 1 To 1000 
    If TAI.Cells(q, 6).Value = 3 Then 
     'store the left-of-the-found-value-3 cells in a range 
     With SheetFive 
      Set Source = .Range(.Cells(q, 1), .Cells(q, 5)) 
     End With 
     'set the target range in row w, col 22 
     With SheetOne 
      Set Target = .Cells(w, 22) 
     End With 
     'the next two lines are the copy and paste step 
     Source.Copy 
     Target.PasteSpecial (xlPasteValues) 
     'increment w 
     w = w + 1 
    End If 
Next q 

End Sub 
+0

고마워요! 이것은 내가 찾고 있었던 바로 그 것이었다. 분명히 나는 ​​많은 신인 선수들의 실수를 저질렀다. 이것은 실제로 깨달음이었습니다. – Jack

1

나는 명시 적 참조보다는 몇 가지 변수를 사용하는 것이 더 쉬울 것이라 생각합니다. 이렇게하면 시트를 앞뒤로 "선택"하지 않아도됩니다.

나는 내가하고있는 일에 대해 당신이 이해할 수 있도록 논평하려고 노력할 것이다.

테스트되지 않았으므로 문제가 있는지 알려 주시기 바랍니다.

Sub Movevalues() 
    Dim q As Integer, w As Integer 
    Dim wsSource as Worksheet  'represents the SOURCE worksheet 
    Dim wsDest as Worksheet  'represents the DESTINATION worksheet 
    Dim copyRange as Range   'represents the range we want to COPY 
    Dim destRange as Range   'represents the destination range 

    'Initialize some variables 
    w = 7 
    Set wsSource = ActiveWorkbook.Worksheets("Taxable Accounts Import") 
    Set wsDest = ActiveWorkbook.Worksheets(1) 

    For q = 1 To 1000 
     With wsSource 
     If .Cells(q, 6).Value = 3 Then 

      'Define the range to be "copied" 
      Set copyRange = .Range(.Cells(q,1), .Cells(q, 5)) 

      'Define the destination range using the Resize method: 
      Set destRange = wsDest.Range(22, w).Resize(1,copyRange.Columns.Count) 

      'Here, we don't need to select or even "copy" anything, we can write directly 
      ' to one range, from another. 
      destRange.Value = copyRange.Value 

      'ensure that w identifies the next column and does not overwrite the values 
      ' that we just transferred, above. 
      w = w + copyRange.Columns.Count 
     End If 
    Next q 
End Sub 

참고 :이은 행 (22)를 사용하여 예에 따라, 대상 시트에 단일 행의 데이터 을 복사하고 그것을 모든을 넣어 당신의 의도 (있다고 가정, 열 w를 붙여 넣기 대상으로 사용).

+0

참조 변수를 잘 사용하고 복사되는 범위의 크기로 'w'를 증가시키는 +1 –

+0

건배 @DanWagner 나는 그 마지막'w' 증분을 거의 놓쳤다. :) –

관련 문제