ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Adding a counter to a variable name (https://www.excelbanter.com/excel-programming/415017-adding-counter-variable-name.html)

Rich

Adding a counter to a variable name
 
I will be collecting values from multiple sheets.

How do I create a variable name with a counter attached to it, and how do I
declare that variable.

For example, I want variable names to be "DkType1", "DkType2", etc. I don't
know how many "DkType"s I will have.

Thanks!!!

Jim Thomlinson

Adding a counter to a variable name
 
You probably want an array...

Sub stuff()
Dim DKType() As Variant
Dim lng As Long

ReDim DKType(lng)
DKType(lng) = Range("A1").Value
lng = lng + 1
ReDim Preserve DKType(lng)
DKType(lng) = Range("A2").Value
lng = lng + 1
ReDim Preserve DKType(lng)
DKType(lng) = Range("A3").Value

For lng = LBound(DKType) To UBound(DKType)
MsgBox DKType(lng)
Next lng

Range("C1:C3").Value = Application.Transpose(DKType) 'write the array
End Sub

--
HTH...

Jim Thomlinson


"Rich" wrote:

I will be collecting values from multiple sheets.

How do I create a variable name with a counter attached to it, and how do I
declare that variable.

For example, I want variable names to be "DkType1", "DkType2", etc. I don't
know how many "DkType"s I will have.

Thanks!!!



All times are GMT +1. The time now is 11:26 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com