Use array to search for CASE values Excel VBA -
i'm trying create array list of variable values within 1 cell.
for example: cell b3 contains values: 1a, 2b, 3a, 4a, 5c.
i want convert data that: myarray=("1a", "2b", "3a", "4a", "5c")
i.e.
myarray(1) = "1a" myarray(2) = "2b" myarray(3) = "3a" myarray(4) = "4a" myarray(5) = "5c"
then want use values of array perform case search compile running total testing.
my code far:
'create subroutine copy , total data worksheet 1 worksheet 2 private sub vts() 'establish variable case search dim valr string 'establish counter array dim myarray(1 170) myarray(1) = worksheets(2).range("a7").value myarray(2) = worksheets(2).range("a10").value 'dim valves() string 'dim thisvalue string valr = worksheets(1).range("b4").value 'valr = split(valvestring, ";") 'valves = valr 'for v = 1 ubound(valves) ' thisvalve = valves(v) select case valr case "1a" worksheets(2).range("c7").copy ' copy current total worksheets(2).range("a7").pastespecial ' move "previous total" sum total myarray(1) = worksheets(1).range("b3").value - worksheets(1).range("b2").value if myarray(1) < 0 myarray(1) = 1000000 + myarray(1) end if worksheets(2).range("b7").value = myarray(1) worksheets(2).range("c7").value = worksheets(2).range("a7").value + worksheets(2).range("b7").value worksheets(2).range("c7").copy worksheets(1).range("b10").pastespecial case "1b" worksheets(2).range("c10").copy worksheets(2).range("a10").pastespecial myarray(2) = worksheets(1).range("b3").value - worksheets(1).range("b2").value if myarray(2) < 0 myarray(2) = 1000000 + myarray(2) end if worksheets(2).range("b10").value = myarray(2) worksheets(2).range("c10").value = worksheets(2).range("a10").value + worksheets(2).range("b10").value worksheets(2).range("c10").copy worksheets(1).range("b10").pastespecial case else msgbox "wrong model entered / model not exist" end select 'next v end sub public sub call_vts() call vts end sub
to summarize, hope to:
1) build array varying cell data
2) of which, each string represents different position of array
3) run case search each position of array
i've been struggling couple of days. appreciated.
to first question, if cell b3 contains "1a, 2b, 3a, 4a, 5c" then
myarray = split(range("b3"),", ")
will give following:
myarray(0) = "1a" myarray(1) = "2b" myarray(2) = "3a" myarray(3) = "4a" myarray(4) = "5c"
if want run through array , evaluate each item, put each loop around select case:
for each in myarray select case case "1a" worksheets(2).range("c7").copy ' copy current total worksheets(2).range("a7").pastespecial ' move "previous total" sum total myarray(1) = worksheets(1).range("b3").value - worksheets(1).range("b2").value if myarray(1) < 0 myarray(1) = 1000000 + myarray(1) end if worksheets(2).range("b7").value = myarray(1) worksheets(2).range("c7").value = worksheets(2).range("a7").value + worksheets(2).range("b7").value worksheets(2).range("c7").copy worksheets(1).range("b10").pastespecial case "1b" worksheets(2).range("c10").copy worksheets(2).range("a10").pastespecial myarray(2) = worksheets(1).range("b3").value - worksheets(1).range("b2").value if myarray(2) < 0 myarray(2) = 1000000 + myarray(2) end if worksheets(2).range("b10").value = myarray(2) worksheets(2).range("c10").value = worksheets(2).range("a10").value + worksheets(2).range("b10").value worksheets(2).range("c10").copy worksheets(1).range("b10").pastespecial case else msgbox "wrong model entered / model not exist" end select next
Comments
Post a Comment