Я только начинаю работать с Excel VBA и пытаюсь запустить приведенный ниже сценарий для заполнения таблицы значениями из нескольких листов.
У меня проблемы с циклами с переменной dindx; на шаге 13 он сбрасывается до 0, что означает, что в конечном итоге данные в той же строке на листе «Dest 2» будут перезаписаны. м немного в тупик :)
Sub fixDataCBAttemptOnly()
'1. Establish where output goes
Set dest = Sheets("dest 2").Range("B6")
'2. Set counter for output rows
dindx = 0
'3. Cycle through all sheets
For Each ws In Sheets
'4. Test if current sheet is not "dest"
If ws.Name <> "Dest 2" Then
'5. True Case: Grab left 4 chars of name of sheet and call year
Yr = Left(ws.Name, 4)
'6. Grab from char 6 through end to set as region
Rgn = Right(ws.Name, Len(ws.Name) - 5)
'7. Set source anchor for active sheet
Set srce = ws.Range("C7")
'8. Loop from 0 to 11 for each column of months
For cindx = 0 To 11
'9. Month value set to counter +1 (i.e Jan is when cindx is 0, etc)
Mnth = cindx + 1
'10. Initialize row counter to zero
rindx = 0
'11. Loop as long as anchor cell down isn't blank
While srce.Offset(rindx, -1) <> ""
'12. Test if not empty; if not empty populate destination with data
If srce.Offset(rindx, cindx) <> "" Then
'13. True Case: Output as: Cust ID; Date; Value; Region; Year
dest.Offset(dindx, 0) = srce.Offset(rindx, -1)
dest.Offset(dindx, 1) = Yr & "-" & Format(Mnth, "00")
dest.Offset(dindx, 2) = srce.Offset(rindx, cindx)
dest.Offset(dindx, 3) = Rgn
dest.Offset(dindx, 4) = Yr
'14. Increment dest counter
dindx = dindx + 1
'15. End if statement from #12
End If
'16. Increment row indx
rindx = rindx + 1
'17. End while statement from #11
Wend
'18. Go to next month from #8
Next cindx
'19. End if from #4
End If
'20. Go to next sheet from For loop in #3
Next ws
End Sub