Dim as long but shorter code

Fredric Andersson

New Member
Joined
Sep 30, 2021
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hi guys,
Decription of the problem:
I have a running code that works, but it will be like 200 miles long.
following code below works but i want to shorten it.

Dim n1 As Long, n2 As Long, n3 As Long, n4 As Long, n5 As Long, n6 As Long

n1 = Worksheets("riskanalys").Range("a2").Text
riskanalys_dashboard.n1 = n1
n2 = Worksheets("riskanalys").Range("a3").Text
riskanalys_dashboard.n2 = n2
n3 = Worksheets("riskanalys").Range("a4").Text
riskanalys_dashboard.n3 = n3
n4 = Worksheets("riskanalys").Range("a5").Text
riskanalys_dashboard.n4 = n4

The problem is that i have 6 columns with data i want to place in label in userform.
the exemple i have above will go up to 30 in range. so its a lof of single code.

Explaning everything:
N1 - n4 is a label in a userform.
Worksheet Riskanalys RAnge A1-a4

what i want and hope you guys can help me with is a code that help me place values from Range A:A to label "n"

Best regards
Fredric
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Something like this:

VBA Code:
Dim n As Long
for n = 1 to 4
    riskanalys_dashboard.Controls("n" & n).Caption = Worksheets("riskanalys").Cells(n + 1, "a").Text
next n
 
Upvote 0
Something like this:

VBA Code:
Dim n As Long
for n = 1 to 4
    riskanalys_dashboard.Controls("n" & n).Caption = Worksheets("riskanalys").Cells(n + 1, "a").Text
next n
That worked perfectly for a number serie,
This is how the spreadsheet looks, From range B:F is textvalue with free text

1728975135640.png


It didnt work for colum B
 
Upvote 0

Forum statistics

Threads
1,223,101
Messages
6,170,116
Members
452,302
Latest member
TaMere

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