I’m new to VBA and need some help. After days of searching on internet and experimenting with code, I can't get it to work.
@brettdj, @ryguy7272 Thanks alot. Both your codes works perfectly, but I understand now I have not explained my problem correctly. You don't need to write full code, just show how to continue with second dynamic range. So if you bear with me, here is the full explanation:
I have eight dynamic ranges in six columns in sheet1 (48 ranges in total), to be copied to 48 static cells in sheet2.
For dynamic ranges: Col"A" have text as start and end value. The other 5 columns have text as start values and empty cell as end values.
Sheet1, col"A", find first occ. of (textstring) "ABC".
Sheet1, col"A", find first occ. of (textstring) "DEF*" ("*" means any character), after "ABC".
This dynamic range shall be copied to Sheet2, "A2"
Sheet1, col"B", find first occ. of (textstring) "GHI"
Sheet1, col"B", find first occ. of (textstring) "" (empty cell) after "GHI"
This dynamic range shall be copied to Sheet2, "C2"
etc
etc.
Below you can read code I have used so far, to do it by columns but I'm stuck when I shall start over at Col"A", and next occ. of "ABC", dynamicly to next occ. of "DEF*.
I.E.:
Sheet1, col"A", find second occ. of "ABC"
Sheet1, col"A", find second occ. of "DEF*", after "ABC"
This dynamic range shall be copied to Sheet2, "A22"
Sheet1, col"B", find second occ. of "GHI"
Sheet1, col"B", find second occ. of "" (empty cell) after "GHI"
This dynamic range shall be copied to Sheet2, "C22"
etc etc. (se code below)
Sheet1: rows= dynamic. Columns: 1,2,3,4,5,9
Sheet2: 8 static rows= 2,22,42,62,82,102,122,142. Columns: 1,3,6,7,9,18
Sub Module1()
Dim foundA As Range, _
foundB As Range
Dim newSht As Worksheet
Application.ScreenUpdating = False
On Error GoTo Terminate
With Sheets("Sheet1").Columns(1)
Set foundA = .Find("ABC")
Set foundB = .Find("DEF*", After:=foundA, SearchDirection:=xlNext)
End With
Range(foundA(2), foundB(0)).Copy
Set newSht = Sheets("Sheet2")
newSht.Range("A2").PasteSpecial
With Sheets("Sheet1").Columns(2)
Set foundA = .Find("GHI")
Set foundB = .Find("", After:=foundA, SearchDirection:=xlNext)
End With
Range(foundA(2), foundB(0)).Copy
Set newSht = Sheets("Sheet2")
newSht.Range("C2").PasteSpecial
With Sheets("Sheet1").Columns(3)
Set foundA = .Find("JKL")
Set foundB = .Find("", After:=foundA, SearchDirection:=xlNext)
End With
Range(foundA(2), foundB(0)).Copy
Set newSht = Sheets("Sheet2")
newSht.Range("F2").PasteSpecial
With Sheets("Sheet1").Columns(4)
Set foundA = .Find("MNO")
Set foundB = .Find("", After:=foundA, SearchDirection:=xlNext)
End With
Range(foundA(2), foundB(0)).Copy
Set newSht = Sheets("Sheet2")
newSht.Range("G2").PasteSpecial
With Sheets("Sheet1").Columns(5)
Set foundA = .Find("PQR")
Set foundB = .Find("", After:=foundA, SearchDirection:=xlNext)
End With
Range(foundA(2), foundB(0)).Copy
Set newSht = Sheets("Sheet2")
newSht.Range("I2").PasteSpecial
With Sheets("Sheet1").Columns(9)
Set foundA = .Find("STU")
Set foundB = .Find("", After:=foundA, SearchDirection:=xlNext)
End With
Range(foundA(2), foundB(0)).Copy
Set newSht = Sheets("Sheet2")
newSht.Range("R2").PasteSpecial
Exit Sub
Terminate:
MsgBox "Error in Code"
End
Application.ScreenUpdating = True
End Sub
I hope it's understandable. If not please ask for clarification. Any help will be greatly appreciated. Thanks!