PDA

View Full Version : VBA Help copying and inserting into specific sheets & sections based on conditions



oksox
06-09-2017, 07:53 AM
Hi,

First off I have very little understanding of VBA, and hoping that someone is willing to help me. I put together a very small worksheet to try and better understand vba for a larger project that I'm working on and have attached it to the thread. What I'm trying to do is copy information from one worksheet and insert into the corresponding worksheets. On the corresponding worksheets there are specific sections that the data needs to fall under. Is this possible with VBA?

offthelip
06-09-2017, 04:26 PM
Yes this is very easy with VBA
try this:

Sub movedata()
Dim Baseball10 As Range
Dim Baseball8 As Range
Dim Football10 As Range
Dim Football8 As Range


With Worksheets("Players")
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
inarr = .Range(Cells(3, 1), Cells(lastrow, 6))
End With


With Worksheets("Baseball")
Set Baseball10 = .Range(.Cells(4, 1), .Cells(19, 5))
Set Baseball8 = .Range(.Cells(22, 1), .Cells(37, 5))
End With
With Worksheets("Football")
Set Football10 = .Range(.Cells(4, 1), .Cells(19, 5))
Set Football8 = .Range(.Cells(22, 1), .Cells(37, 5))
End With
b10 = 1
b8 = 1
f10 = 1
f8 = 1


For i = 1 To lastrow - 2
If inarr(i, 1) = "Baseball" Then
If inarr(i, 4) = "10" Then
For k = 1 To 5
Baseball10(b10, k) = inarr(i, 1 + k)
Next k
b10 = b10 + 1
Else
For k = 1 To 5
Baseball8(b8, k) = inarr(i, 1 + k)
Next k
b8 = b8 + 1
End If
Else
If inarr(i, 1) = "Football" Then
If inarr(i, 4) = "10" Then
For k = 1 To 5
Football10(f10, k) = inarr(i, 1 + k)
Next k
f10 = f10 + 1
Else
For k = 1 To 5
Football8(f8, k) = inarr(i, 1 + k)
Next k
f8 = f8 + 1
End If
End If
End If
Next i

End Sub

oksox
06-10-2017, 09:07 PM
Thank you this definitely helps me get started.