Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
Configuración de variables públicas en VBA Excel

Estoy creando variables públicas en mi programa para usarlas en varios subs del módulo.

 Public c As Range, wsSrc As Worksheet, wsDest As Worksheet, cDest As Range

Sin embargo, también quiero que estas variables tengan el siguiente valor en toda la función

 Set wsSrc = Workbooks("SourceBook").Worksheets("SourceSheet") Set wsDest = Workbooks("DestBook").Worksheets("DestSheet")

¿Hay alguna manera de que pueda configurarlos y que sus valores se trasladen a toda la función? También para c y cDest, los estoy cambiando en las funciones, por lo que no quiero que se definan globalmente. Solo wsSRC y wsDest . ¿Las variables en VBA son inmutables?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Esta es una cuestión de alcance. Para usar variables en un módulo, no es necesario configurarlas como públicas: Public se usa para hacer que las variables estén disponibles para otros módulos.

En este caso, solo necesita declarar variables fuera de la función.

 'Module begining Dim wsSrc As Worksheet, wsDest As Worksheet Function f1 Dim c As Range, cDest As Range '... wsSrc and wsDest are acessible, eg: Set c = wsSrc.Columns(2).Find(What:="Commercial Income", LookIn:=xlValues, _ LookAt:=xlPart, MatchCase:=False) End Function Function f2 '... wsSrc and wsDest are also acessible here End Function

Sin embargo, para evitar problemas de inicialización, puede usarlos como propiedades, por lo que cada vez que se llaman, el valor se devuelve correctamente:

 'Module begining Property Get wsSrc As Worksheet Set wsSrc = Workbooks("SourceBook").Worksheets("SourceSheet") End Property Property Get wsSrc As Worksheet Set wsDest = Workbooks("DestBook").Worksheets("DestSheet") End Property Function f1 Dim c As Range, cDest As Range '... wsSrc and wsDest are acessible End Function Function f2 '... wsSrc and wsDest are also acessible here End Function
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!