Select sheet to work

Leandroarb

Board Regular
Joined
Oct 7, 2014
Messages
157
Is it normal for a code to work I have to insert a line to select the spreadsheet I want it to analyze?

VBA Code:
Sub tst()
Dim matriz()   As String
Dim intLinhas As Integer
Dim rngVrp      As Range
Dim cel         As Range
Dim cont      As Integer

Sheets("Executados").Select 'IF YOU DO NOT WRITE THIS LINE DOES NOT WORK
cont = 0
Set rngVrp = Range(Cells(2, 1), Cells(WorksheetFunction.CountA(Worksheets("Executados").Range("A:A")), 1))
intLinhas = Application.WorksheetFunction.CountIf(Worksheets("Executados").Range("B:B"), 1) 'Conta o número de linhas
ReDim matriz(intLinhas, 13)

For Each cel In rngVrp
    If cel.Offset(0, 1).Value = 1 Then
        cont = cont + 1
            For k = 1 To 13
                matriz(cont, k) = Worksheets("Executados").Cells(cel.Row, k).Value
            Next k
    End If
Next

        For linha = 1 To intLinhas
            For coluna = 1 To 13
                Worksheets("Print").Cells(linha, coluna).Value = matriz(linha, coluna)
            Next coluna
        Next linha
Sheets("Print").Select
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
There's very rarely any need to select anything in VBA, you can do it like
VBA Code:
Sub tst()
   Dim matriz()   As String
   Dim intLinhas As Integer
   Dim rngVrp      As Range
   Dim cel         As Range
   Dim cont      As Integer
  
   With Sheets("Executados")
      cont = 0
      Set rngVrp = .Range(.Cells(2, 1), .Cells(WorksheetFunction.CountA(.Range("A:A")), 1))
      intLinhas = Application.WorksheetFunction.CountIf(.Range("B:B"), 1) 'Conta o número de linhas
      ReDim matriz(intLinhas, 13)
     
      For Each cel In rngVrp
          If cel.Offset(0, 1).Value = 1 Then
              cont = cont + 1
                  For k = 1 To 13
                      matriz(cont, k) = .Cells(cel.Row, k).Value
                  Next k
          End If
      Next
     
              For linha = 1 To intLinhas
                  For coluna = 1 To 13
                      Worksheets("Print").Cells(linha, coluna).Value = matriz(linha, coluna)
                  Next coluna
              Next linha
   End With
   Sheets("Print").Select
End Sub
 
Upvote 0
There's very rarely any need to select anything in VBA, you can do it like
VBA Code:
Sub tst()
   Dim matriz()   As String
   Dim intLinhas As Integer
   Dim rngVrp      As Range
   Dim cel         As Range
   Dim cont      As Integer
 
   With Sheets("Executados")
      cont = 0
      Set rngVrp = .Range(.Cells(2, 1), .Cells(WorksheetFunction.CountA(.Range("A:A")), 1))
      intLinhas = Application.WorksheetFunction.CountIf(.Range("B:B"), 1) 'Conta o número de linhas
      ReDim matriz(intLinhas, 13)
    
      For Each cel In rngVrp
          If cel.Offset(0, 1).Value = 1 Then
              cont = cont + 1
                  For k = 1 To 13
                      matriz(cont, k) = .Cells(cel.Row, k).Value
                  Next k
          End If
      Next
    
              For linha = 1 To intLinhas
                  For coluna = 1 To 13
                      Worksheets("Print").Cells(linha, coluna).Value = matriz(linha, coluna)
                  Next coluna
              Next linha
   End With
   Sheets("Print").Select
End Sub

Mr. Pluff, thank you very much by more this help and lesson!
 
Upvote 0
Other question, but i think this is personal, or not.
This the better way to make a report with excel and vba?
Technically speaking and thinking about performance.
 
Upvote 0
Without knowing what your data is like & what you re trying to do, it's very difficult to tell.
Also IMO there is no "best" way to do things, it's all very subjective.
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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