Brand new to the forums here, so...greetings!
I am using Excel 2007 and sample of VBA code I found online, tailored to my needs. When running the corresponding macro, I get the following error:
"Run-time error '424': Object required"
I know what that means and what would cause it but can't figure out why the error is occurring.
Examining the Debug, the macro halts at the "Set s1 = Sheet6" line in the code below.
Public Sub TFImport()
Dim s1 As Worksheet
Dim s2 As Worksheet
Dim r1 As String
Dim r2(10) As String
Dim rng1 As Range
Dim rng2 As Range
Dim lRow As Long
Dim lRow2 As Long
Dim lGroup As Long
'// Change to suit
Set s1 = Sheet6
Set s2 = Sheet1
If s2.UsedRange.Rows.Count > 1 Then
MsgBox "Please *DELETE* all rows from sheet2... not just the contents.", vbExclamation, "Can't continue"
Exit Sub
End If
'// Placeholders
r1 = "A_:EH_"
r2(0) = "EI_:EP_"
r2(1) = "EQ_:EX_"
r2(2) = "EY_:FF_"
r2(3) = "FG_:FN_"
r2(4) = "FO_:FV_"
r2(5) = "FW_:GD_"
r2(6) = "GE_:GL_"
r2(7) = "GM_:GT_"
r2(8) = "GU_:HB_"
r2(9) = "HC_:HJ_"
Application.ScreenUpdating = False
'// Headers - Don't fall into the same schema as data so just copy
'// everything from A1 to EP1
s1.Range("A1:EP1").Copy s2.Range("A1")
'// For the remaining rows
For lRow = 2 To s1.UsedRange.Rows.Count
'// The primary data - Underscore replaced by Row ID (lRow)
Set rng1 = s1.Range(Replace(r1, "_", CStr(lRow)))
Debug.Print "Using range - " & rng1.Address
'// Loop through the array getting info from columns
lRow2 = s2.UsedRange.Rows.Count + 1
For lGroup = 0 To UBound(r2) - 1
'// Copy primary data for this row
rng1.Copy s2.Range("A" & CStr(lRow2 + lGroup))
'// Get 'secondary' data
Set rng2 = s1.Range(Replace(r2(lGroup), "_", CStr(lRow)))
Debug.Print "Copying from " & rng2.Address & " to " & s2.Range(Replace(r2(0), "_", CStr(lRow2 + lGroup))).Address
'// but copied to Cols EI-EP in s2.UsedRows.Count
rng2.Copy s2.Range(Replace(r2(0), "_", CStr(lRow2 + lGroup)))
Next
Next
'// Clean up
Set s1 = Nothing
Set s2 = Nothing
Set rng1 = Nothing
Set rng2 = Nothing
Application.ScreenUpdating = True
End Sub
The code takes chunks of data in separate contiguous columns on the same row and moves each chunk to its own new row, preceded by (repeating) the donor data (columns A:EH) also placed into those new rows. Each chunk to be moved represents a fund and payment to that fund. So, what was previously all on one row for the donor (A:EH, EI:EP, EQ:EX, EY:FF, etc.) will now look like this:
A:EH, EI:EP
A:EH, EQ:EX
A:EH, EY:FF
...etc...
Since I also know the difference between the sheet code names that Excel uses (e.g., Sheet1, Sheet2, ...Sheet6, etc.) and the sheet names that I gave them (e.g., I named Sheet1 as "Sheet1" and Sheet6 as "TeleFund"), the code name references in my code seem correct.
I need to know why the macro is halting and what is causing it (how to fix it).
Thanks in advance for any help/suggestions.
Bookmarks