if goto inside for loop

gal12

New Member
Joined
Feb 3, 2021
Messages
10
Office Version
  1. 2019
Platform
  1. Windows
hello,

i'm trying to do an if go to fuction inside a for loop and i get an error.
please help me understand what i did wrong.

VBA Code:
Sub CopyToMaster()
Dim destination As Worksheet
Dim emptyColumn As Long
Dim j As Long
Dim cell As Range
Set destination = Sheets("MASTER")
ShtCount = ActiveWorkbook.Sheets.Count
 
For i = 3 To ShtCount
        Worksheets(i).Activate
 
        If Range("C2,A7:J31").SpecialCells(xlCellTypeConstants).Select Is Nothing Then
            GoTo line2
            Else: GoTo line1
        End If
line1:
        Range("C2,A7:J31").SpecialCells(xlCellTypeConstants).Select
        emptyColumn = destination.Cells(1, i - 2).Column
            j = 0
            For Each cell In Selection
                destination.Cells(emptyColumn).Offset(j).Value = cell.Value
                j = j + 1
            Next cell
line2:
Next i
Worksheets("MASTER").Activate
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
What are you trying to do with the code?
 
Upvote 0
Hi & welcome to MrExcel.
How about
VBA Code:
Sub CopyToMaster()
   Dim destination As Worksheet
   Dim emptyColumn As Long
   Dim j As Long
   Dim cell As Range, Rng As Range
   Set destination = Sheets("MASTER")
   ShtCount = ActiveWorkbook.Sheets.Count
   
   For i = 3 To ShtCount
      With Worksheets(i)
         On Error Resume Next
         Set Rng = Range("C2,A7:J31").SpecialCells(xlCellTypeConstants)
         On Error GoTo 0
         If Not Rng Is Nothing Then
            emptyColumn = destination.Cells(1, i - 2).Column
            j = 0
            For Each cell In Rng
               destination.Cells(emptyColumn).Offset(j).Value = cell.Value
               j = j + 1
            Next cell
         End If
      End With
   Next i
   Worksheets("MASTER").Activate
End Sub
 
Upvote 0
Solution
What are you trying to do with the code?
i have an excel with a lot of sheets that look the same, im trying to copy a matrix and paste it as a vector to one master spreadsheet each sheet as a new column
 
Upvote 0
Hi & welcome to MrExcel.
How about
VBA Code:
Sub CopyToMaster()
   Dim destination As Worksheet
   Dim emptyColumn As Long
   Dim j As Long
   Dim cell As Range, Rng As Range
   Set destination = Sheets("MASTER")
   ShtCount = ActiveWorkbook.Sheets.Count
  
   For i = 3 To ShtCount
      With Worksheets(i)
         On Error Resume Next
         Set Rng = Range("C2,A7:J31").SpecialCells(xlCellTypeConstants)
         On Error GoTo 0
         If Not Rng Is Nothing Then
            emptyColumn = destination.Cells(1, i - 2).Column
            j = 0
            For Each cell In Rng
               destination.Cells(emptyColumn).Offset(j).Value = cell.Value
               j = j + 1
            Next cell
         End If
      End With
   Next i
   Worksheets("MASTER").Activate
End Sub
didn't work unfortunately
 
Upvote 0
Oops, missed full stop, it should be
VBA Code:
         Set Rng = .Range("C2,A7:J31").SpecialCells(xlCellTypeConstants)
 
Upvote 0
i made this code and it ran fine until it found an empty spreadsheet in the middle. when i tried to add the if it stopped working.

VBA Code:
Sub CopyToMaster()
Dim destination As Worksheet
Dim emptyColumn As Long
Dim j As Long
Dim cell As Range
Set destination = Sheets("MASTER")
ShtCount = ActiveWorkbook.Sheets.Count
 
 For i = 3 To ShtCount
        Worksheets(i).Activate
 
        Range("C2,A7:J31").SpecialCells(xlCellTypeConstants).Select

        emptyColumn = destination.Cells(1, i - 2).Column
            j = 0
            For Each cell In Selection
                destination.Cells(emptyColumn).Offset(j).Value = cell.Value
                j = j + 1
            Next cell
 Next i
Worksheets("MASTER").Activate
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,214
Messages
6,123,666
Members
449,114
Latest member
aides

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top