Shift Worksheet Tab

tzinn1

New Member
Joined
Oct 8, 2017
Messages
2
I am very new to VBA, but I am writing code to populate a spreadsheet with data retrieved from a barcode scanner. The overall purpose is to manifest all products placed on a trailer. I have written code to create a worksheet for every PO #. I would like to shift or move between the worksheet tabs by scanning a barcode with string "LEFT" or "RIGHT"

The PO numbers are not sequential or would utilize them. Not sure what to do. Any help is greatly appreciated.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
What code do you currently have?
 
Upvote 0
.
This following macro is from resource : http://www.vbaexpress.com/kb/getarticle.php?kb_id=536
There is a ZIP file containing a complete workbook example available for download.


Code:
Option Explicit


Sub GoForth()
    MoveSheet (1)
End Sub
 
Sub GoBack()
    MoveSheet (-1)
End Sub
 
Sub MoveSheet(iMove As Integer)
    Dim iSheetNum As Integer
     
    iSheetNum = ActiveSheet.Index
    On Error Resume Next
    Do
        Err.Clear
        iSheetNum = iSheetNum + iMove
        If iMove > 0 Then
            If iSheetNum > Sheets.Count Then iSheetNum = 1
        Else
            If iSheetNum < 1 Then iSheetNum = Sheets.Count
        End If
        Sheets(iSheetNum).Select
    Loop Until Err.Number = 0
     
End Sub

Have your scanner activate the GoForth or GoBack sub as required.

I would caution you using the terms LEFT and RIGHT as a variable. These are key words used by EXCEL VBA and may cause a conflict in your code. Having a command button with the caption LEFT or RIGHT ... or a label with the same caption should not cause an issue though.
 
Last edited:
Upvote 0
.
This following macro is from resource : http://www.vbaexpress.com/kb/getarticle.php?kb_id=536
There is a ZIP file containing a complete workbook example available for download.


Code:
Option Explicit


Sub GoForth()
    MoveSheet (1)
End Sub
 
Sub GoBack()
    MoveSheet (-1)
End Sub
 
Sub MoveSheet(iMove As Integer)
    Dim iSheetNum As Integer
     
    iSheetNum = ActiveSheet.Index
    On Error Resume Next
    Do
        Err.Clear
        iSheetNum = iSheetNum + iMove
        If iMove > 0 Then
            If iSheetNum > Sheets.Count Then iSheetNum = 1
        Else
            If iSheetNum < 1 Then iSheetNum = Sheets.Count
        End If
        Sheets(iSheetNum).Select
    Loop Until Err.Number = 0
     
End Sub

Have your scanner activate the GoForth or GoBack sub as required.

I would caution you using the terms LEFT and RIGHT as a variable. These are key words used by EXCEL VBA and may cause a conflict in your code. Having a command button with the caption LEFT or RIGHT ... or a label with the same caption should not cause an issue though.




I got it working thanks to your help. I did not know about the index. Make sense. I am using the following code. I am not a structured programmer ...yet... but I will get there. I have not programmed since the 80s...I only knew basic and the code I am writing needs work....but its working. I did get into trouble with using the string left and right so I change to "left move" and right move". Thanks

If Target.Address(False, False) = "A1" And a = "LEFT MOVE" Then
Range("A1") = ""
idx = idx - 1
If idx < 1 Then idx = idx + 1
Sheets(idx).Select
End If
If Target.Address(False, False) = "A1" And a = "RIGHT MOVE" Then
Range("A1") = ""
idx = idx + 1
If idx > Sheets.Count - 1 Then idx = Sheets.Count - 1
Sheets(idx).Select
End If
 
Upvote 0
.
Glad to know you found the answer. Wonderful !

Please don't forget to mark this thread SOLVED (up top, THREAD TOOLS) if this does indeed solve your question.

Cheers.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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