Macro to use tab that is in front

bklabel1

Board Regular
Joined
Feb 24, 2015
Messages
134
I have ordinary VBA code shown below. I'm only showing the code because it is just every day kind of stuff. It works on a specific sheet fine. I want to know how to have it run against which ever Tab/Sheet is in front.
I want the code to run on the active tab. Maybe its called the active sheet.

I tried searching the forum but I couldn't narrow it down.

Best and thank you,

Kevin


'Fill array with values. One letter per location.
For row = 2 To 300

rowContents = Cells(row, 1).Value
colContents = Cells(row, 2).Value
length = Cells(row, 3).Value
field = Cells(row, 4).Value
OutputText = Cells(row, 5).Value

'Truncate data according to length. Maybe it could change color on sheet as a warning.
OutputText = Mid(OutputText, 1, length)


For pos = 1 To Len(OutputText)
'Examine the current element in the array



I tried searching the board but too many results came back and I could not tell what I needed.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
I think you want
VBA Code:
Sub abc()
Dim Ws As Worksheet

Set Ws = ActiveSheet

'Fill array with values. One letter per location.
For Row = 2 To 300

rowContents = Ws.Cells(Row, 1).Value
colContents = Ws.Cells(Row, 2).Value
Length = Ws.Cells(Row, 3).Value
field = Ws.Cells(Row, 4).Value
OutputText = Ws.Cells(Row, 5).Value

'Truncate data according to length. Maybe it could change color on sheet as a warning.
OutputText = Mid(OutputText, 1, Length)


For pos = 1 To Len(OutputText)
'Examine the current element in the array
Next
Next

End Sub
 
Upvote 0
Thank you for reviewing the code, but that was not my question.

The answer I was looking for was that Excel assumes the front sheet is the activeSheet. To guarantee it in the code, put it in front such as:

Length = activeSheet.Ws.Cells(Row, 3).Value

I have not tried it yet, but this is what I was looking for.

Kevin
 
Upvote 0
Thank you for reviewing the code, but that was not my question.

The answer I was looking for was that Excel assumes the front sheet is the activeSheet. To guarantee it in the code, put it in front such as:

Length = activeSheet.Ws.Cells(Row, 3).Value

I have not tried it yet, but this is what I was looking for.

Kevin
That's basically what mine does. Though with a little more code you can block it setting on a sheet thats' you do not want
 
Upvote 0

Forum statistics

Threads
1,215,092
Messages
6,123,064
Members
449,090
Latest member
fragment

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